feat: custom lexer registry

Signed-off-by: jolheiser <git@jolheiser.com>
main
jolheiser 2024-07-31 22:18:17 -05:00
parent df7ace7fae
commit 590777dea0
No known key found for this signature in database
1 changed files with 8 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package markup
import ( import (
"io" "io"
"path/filepath"
"github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/formatters/html" "github.com/alecthomas/chroma/v2/formatters/html"
@ -26,10 +27,17 @@ var (
type code struct{} type code struct{}
var customReg = map[string]string{
".hujson": "json",
}
func setup(source []byte, fileName string) (chroma.Iterator, *chroma.Style, error) { func setup(source []byte, fileName string) (chroma.Iterator, *chroma.Style, error) {
lexer := lexers.Match(fileName) lexer := lexers.Match(fileName)
if lexer == nil { if lexer == nil {
lexer = lexers.Fallback lexer = lexers.Fallback
if name, ok := customReg[filepath.Ext(fileName)]; ok {
lexer = lexers.Get(name)
}
} }
lexer = chroma.Coalesce(lexer) lexer = chroma.Coalesce(lexer)