summaryrefslogtreecommitdiffstats
path: root/modules/highlight/highlight.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2021-09-24 14:29:32 +0100
committerGitHub <noreply@github.com>2021-09-24 14:29:32 +0100
commit623d2dd411b6a84a01bff3ca8046f1bd01773ffb (patch)
treeb0a006bc910255246756c0105e6d6c3d4c149f04 /modules/highlight/highlight.go
parent5842a55b3103d3f09751eb7b3b049415197debad (diff)
downloadgitea-623d2dd411b6a84a01bff3ca8046f1bd01773ffb.tar.gz
gitea-623d2dd411b6a84a01bff3ca8046f1bd01773ffb.zip
Prevent panic in Org mode HighlightCodeBlock (#17140)
When rendering source in org mode there is a mistake in the highlight code that causes a panic. This PR fixes this. Fix #17139 Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'modules/highlight/highlight.go')
-rw-r--r--modules/highlight/highlight.go23
1 files changed, 12 insertions, 11 deletions
diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go
index 079f7a44bd..6684fbe842 100644
--- a/modules/highlight/highlight.go
+++ b/modules/highlight/highlight.go
@@ -66,17 +66,6 @@ func Code(fileName, code string) string {
if len(code) > sizeLimit {
return code
}
- formatter := html.New(html.WithClasses(true),
- html.WithLineNumbers(false),
- html.PreventSurroundingPre(true),
- )
- if formatter == nil {
- log.Error("Couldn't create chroma formatter")
- return code
- }
-
- htmlbuf := bytes.Buffer{}
- htmlw := bufio.NewWriter(&htmlbuf)
var lexer chroma.Lexer
if val, ok := highlightMapping[filepath.Ext(fileName)]; ok {
@@ -97,6 +86,18 @@ func Code(fileName, code string) string {
}
cache.Add(fileName, lexer)
}
+ return CodeFromLexer(lexer, code)
+}
+
+// CodeFromLexer returns a HTML version of code string with chroma syntax highlighting classes
+func CodeFromLexer(lexer chroma.Lexer, code string) string {
+ formatter := html.New(html.WithClasses(true),
+ html.WithLineNumbers(false),
+ html.PreventSurroundingPre(true),
+ )
+
+ htmlbuf := bytes.Buffer{}
+ htmlw := bufio.NewWriter(&htmlbuf)
iterator, err := lexer.Tokenise(nil, string(code))
if err != nil {