From 078d2fbd4c386d0cf37378078443f5f8dbe89f37 Mon Sep 17 00:00:00 2001 From: mrsdizzie Date: Tue, 7 Jul 2020 18:30:21 -0400 Subject: Remove newline when highlighting random chunks of code (#12180) * Remove newline when highlighting random chunks of code Somewhere when tokenizing a newline gets added to code formatted by chroma. This breaks the case of 'added-code' inside of an 'added-line' in a diff. Just remove any newline when processing chunks of code since we don't need it. Fixes #12172 * don't process empty lines * This is the proper way to fix this by telling chroma not to add the newline in the first place --- modules/highlight/highlight.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'modules/highlight/highlight.go') diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index 90590d220b..5d66e4c32d 100644 --- a/modules/highlight/highlight.go +++ b/modules/highlight/highlight.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "github.com/alecthomas/chroma" "github.com/alecthomas/chroma/formatters/html" "github.com/alecthomas/chroma/lexers" "github.com/alecthomas/chroma/styles" @@ -68,7 +69,7 @@ func Code(fileName, code string) string { lexer = lexers.Fallback } - iterator, err := lexer.Tokenise(nil, string(code)) + iterator, err := lexer.Tokenise(&chroma.TokeniseOptions{State: "root", Nested: true}, string(code)) if err != nil { log.Error("Can't tokenize code: %v", err) return code -- cgit v1.2.3