From 7aeb1ba1d57b6635eb5dc1bfbd1a5371656e8677 Mon Sep 17 00:00:00 2001 From: Gusted Date: Wed, 27 Nov 2024 17:57:02 +0100 Subject: [PATCH] fix: normalize guessed languages from enry - In the case that Enry correctly recognized the language, but returns the language name in a way that isn't recognized by enry. Although overkill I've added a map such that new entries should be easier to add. - Resolves #6077 - Added unit test --- modules/highlight/highlight.go | 9 +++++++++ modules/highlight/highlight_test.go | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index bd6137dc85..243aa2b566 100644 --- a/modules/highlight/highlight.go +++ b/modules/highlight/highlight.go @@ -134,6 +134,12 @@ func CodeFromLexer(lexer chroma.Lexer, code string) template.HTML { return template.HTML(strings.TrimSuffix(htmlbuf.String(), "\n")) } +// For the case where Enry recognizes the language, but doesn't use the naming +// that Chroma expects. +var normalizeEnryToChroma = map[string]string{ + "F#": "FSharp", +} + // File returns a slice of chroma syntax highlighted HTML lines of code and the matched lexer name func File(fileName, language string, code []byte) ([]template.HTML, string, error) { NewContext() @@ -162,6 +168,9 @@ func File(fileName, language string, code []byte) ([]template.HTML, string, erro if lexer == nil { guessLanguage := analyze.GetCodeLanguage(fileName, code) + if normalizedGuessLanguage, ok := normalizeEnryToChroma[guessLanguage]; ok { + guessLanguage = normalizedGuessLanguage + } lexer = lexers.Get(guessLanguage) if lexer == nil { diff --git a/modules/highlight/highlight_test.go b/modules/highlight/highlight_test.go index 03db4d5090..0127854c8e 100644 --- a/modules/highlight/highlight_test.go +++ b/modules/highlight/highlight_test.go @@ -115,6 +115,12 @@ c=2 want: lines(""), lexerName: "ObjectPascal", }, + { + name: "test.fs", + code: "module Crypt = let generateCryptTable: array =", + want: lines(`module Crypt = let generateCryptTable: array<uint32> =`), + lexerName: "FSharp", + }, } for _, tt := range tests {