Skip to content

Commit

Permalink
tpl/transform: Add CanHighlight
Browse files Browse the repository at this point in the history
Closes #9573
  • Loading branch information
bep committed Feb 27, 2022
1 parent cff1414 commit 39261b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tpl/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions tpl/transform/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 39261b6

Please sign in to comment.