diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-02-27 17:09:11 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-02-27 19:51:40 +0100 |
commit | 39261b689e47116de5cfd3bee6b6e3af57deb97c (patch) | |
tree | 04993cb42a2f6b4f8afb4be24066b017de1cece2 | |
parent | cff14144a6306b9a60c4fc56d1dce17f0b3778e9 (diff) |
tpl/transform: Add CanHighlight
Closes #9573
-rw-r--r-- | tpl/transform/transform.go | 6 | ||||
-rw-r--r-- | tpl/transform/transform_test.go | 10 |
2 files changed, 16 insertions, 0 deletions
diff --git a/tpl/transform/transform.go b/tpl/transform/transform.go index dc7cc0342..48cfaffff 100644 --- a/tpl/transform/transform.go +++ b/tpl/transform/transform.go @@ -18,6 +18,7 @@ import ( "html" "html/template" + "github.com/alecthomas/chroma/lexers" "github.com/gohugoio/hugo/cache/namedmemcache" "github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/markup/converter/hooks" @@ -90,6 +91,11 @@ func (ns *Namespace) HighlightCodeBlock(ctx hooks.CodeblockContext, opts ...inte return hl.HighlightCodeBlock(ctx, optsv) } +// CanHighlight returns whether the given language is supported by the Chroma highlighter. +func (ns *Namespace) CanHighlight(lang string) bool { + return lexers.Get(lang) != nil +} + // HTMLEscape returns a copy of s with reserved HTML characters escaped. func (ns *Namespace) HTMLEscape(s interface{}) (string, error) { ss, err := cast.ToStringE(s) diff --git a/tpl/transform/transform_test.go b/tpl/transform/transform_test.go index 3ccf1a270..289674bf1 100644 --- a/tpl/transform/transform_test.go +++ b/tpl/transform/transform_test.go @@ -95,6 +95,16 @@ func TestHighlight(t *testing.T) { } } +func TestCanHighlight(t *testing.T) { + t.Parallel() + + c := qt.New(t) + ns := &transform.Namespace{} + + c.Assert(ns.CanHighlight("go"), qt.Equals, true) + c.Assert(ns.CanHighlight("foo"), qt.Equals, false) +} + func TestHTMLEscape(t *testing.T) { t.Parallel() b := hugolib.NewIntegrationTestBuilder( |