markup/highlight: Fix chroma highlight

* Use chroma.Coalesce
* Escape code strings if lexer is nil

Fixes #6877
Fixes #6856
This commit is contained in:
satotake
2020-02-17 22:59:26 +09:00
committed by GitHub
parent 54bdcaacae
commit 3c568ad013
2 changed files with 31 additions and 2 deletions

View File

@@ -15,6 +15,7 @@ package highlight
import (
"fmt"
gohtml "html"
"io"
"strings"
@@ -63,7 +64,7 @@ func highlight(code, lang string, cfg Config) (string, error) {
if lexer == nil {
wrapper := getPreWrapper(lang)
fmt.Fprint(w, wrapper.Start(true, ""))
fmt.Fprint(w, code)
fmt.Fprint(w, gohtml.EscapeString(code))
fmt.Fprint(w, wrapper.End(true))
return w.String(), nil
}
@@ -72,6 +73,7 @@ func highlight(code, lang string, cfg Config) (string, error) {
if style == nil {
style = styles.Fallback
}
lexer = chroma.Coalesce(lexer)
iterator, err := lexer.Tokenise(nil, code)
if err != nil {