From b4d18dae19679816829d619fac2e211e08b98a2a Mon Sep 17 00:00:00 2001 From: mrsdizzie Date: Fri, 13 Nov 2020 18:05:51 -0500 Subject: [PATCH] Use existing analyzer module for language detection for highlighting (#13522) (#13551) * 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 Co-authored-by: zeripath Co-authored-by: Lauris BH Co-authored-by: techknowlogick Co-authored-by: zeripath Co-authored-by: Lauris BH Co-authored-by: techknowlogick --- modules/highlight/highlight.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index 1a6410813..914ba8210 100644 --- a/modules/highlight/highlight.go +++ b/modules/highlight/highlight.go @@ -13,6 +13,7 @@ import ( "strings" "sync" + "code.gitea.io/gitea/modules/analyze" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "github.com/alecthomas/chroma/formatters/html" @@ -117,9 +118,11 @@ func File(numLines int, fileName string, code []byte) map[int]string { fileName = "test." + val } - lexer := lexers.Match(fileName) + language := analyze.GetCodeLanguage(fileName, code) + + lexer := lexers.Get(language) if lexer == nil { - lexer = lexers.Analyse(string(code)) + lexer = lexers.Match(fileName) if lexer == nil { lexer = lexers.Fallback }