Skip to content

Commit 6280af0

Browse files
mrsdizziezeripathlafrikstechknowlogick
authored
Use existing analyzer module for language detection for highlighting (go-gitea#13522)
* Use existing analyzer module for language detction for highlighting Thanks @lafriks for pointing out we can reuse existing code for more reliable language detection here. * Update modules/highlight/highlight.go Co-authored-by: Lauris BH <[email protected]> Co-authored-by: zeripath <[email protected]> Co-authored-by: Lauris BH <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 3cd00c3 commit 6280af0

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

modules/highlight/highlight.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strings"
1414
"sync"
1515

16+
"code.gitea.io/gitea/modules/analyze"
1617
"code.gitea.io/gitea/modules/log"
1718
"code.gitea.io/gitea/modules/setting"
1819
"github.com/alecthomas/chroma/formatters/html"
@@ -117,9 +118,11 @@ func File(numLines int, fileName string, code []byte) map[int]string {
117118
fileName = "test." + val
118119
}
119120

120-
lexer := lexers.Match(fileName)
121+
language := analyze.GetCodeLanguage(fileName, code)
122+
123+
lexer := lexers.Get(language)
121124
if lexer == nil {
122-
lexer = lexers.Analyse(string(code))
125+
lexer = lexers.Match(fileName)
123126
if lexer == nil {
124127
lexer = lexers.Fallback
125128
}

0 commit comments

Comments
 (0)