diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-02-26 17:24:10 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-02-26 21:54:36 +0100 |
commit | 579ff9b6524ed4e55c191ab94251f5a2fe315f68 (patch) | |
tree | 0249f3ae723241c4cc2fef7e9ad9ddf06e4bc523 /markup/goldmark | |
parent | 928a89696273b1a5defa0e85115c9bd0e167cbf0 (diff) |
markup/goldmark: Improve attributes vs options
Fixes #9571
Diffstat (limited to 'markup/goldmark')
-rw-r--r-- | markup/goldmark/codeblocks/integration_test.go | 29 | ||||
-rw-r--r-- | markup/goldmark/codeblocks/render.go | 17 |
2 files changed, 29 insertions, 17 deletions
diff --git a/markup/goldmark/codeblocks/integration_test.go b/markup/goldmark/codeblocks/integration_test.go index 68f6b809e..c33ea31e3 100644 --- a/markup/goldmark/codeblocks/integration_test.go +++ b/markup/goldmark/codeblocks/integration_test.go @@ -14,6 +14,7 @@ package codeblocks_test import ( + "strings" "testing" "github.com/gohugoio/hugo/hugolib" @@ -176,7 +177,7 @@ Position: {{ .Position | safeHTML }} } // Issue 9571 -func TestOptionsNonChroma(t *testing.T) { +func TestAttributesChroma(t *testing.T) { t.Parallel() files := ` @@ -188,23 +189,27 @@ title: "p1" ## Code -§§§bash {style=monokai} +§§§LANGUAGE {style=monokai} echo "p1"; §§§ -- layouts/_default/single.html -- {{ .Content }} -- layouts/_default/_markup/render-codeblock.html -- -Style: {{ .Attributes }}| +Attributes: {{ .Attributes }}|Options: {{ .Options }}| ` - - b := hugolib.NewIntegrationTestBuilder( - hugolib.IntegrationTestConfig{ - T: t, - TxtarString: files, - }, - ).Build() - - b.AssertFileContent("public/p1/index.html", "asdfadf") + testLanguage := func(language, expect string) { + b := hugolib.NewIntegrationTestBuilder( + hugolib.IntegrationTestConfig{ + T: t, + TxtarString: strings.ReplaceAll(files, "LANGUAGE", language), + }, + ).Build() + + b.AssertFileContent("public/p1/index.html", expect) + } + + testLanguage("bash", "Attributes: map[]|Options: map[style:monokai]|") + testLanguage("hugo", "Attributes: map[style:monokai]|Options: map[]|") } diff --git a/markup/goldmark/codeblocks/render.go b/markup/goldmark/codeblocks/render.go index bbf15bef3..27252fc27 100644 --- a/markup/goldmark/codeblocks/render.go +++ b/markup/goldmark/codeblocks/render.go @@ -18,7 +18,7 @@ import ( "fmt" "sync" - "github.com/gohugoio/hugo/common/herrors" + "github.com/alecthomas/chroma/lexers" htext "github.com/gohugoio/hugo/common/text" "github.com/gohugoio/hugo/markup/converter/hooks" "github.com/gohugoio/hugo/markup/goldmark/internal/render" @@ -61,8 +61,6 @@ func (r *htmlRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { } func (r *htmlRenderer) renderCodeBlock(w util.BufWriter, src []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { - defer herrors.Recover() - ctx := w.(*render.Context) if entering { @@ -92,17 +90,26 @@ func (r *htmlRenderer) renderCodeBlock(w util.BufWriter, src []byte, node ast.No if n.b.Info != nil { info = n.b.Info.Segment.Value(src) } + + attrtp := attributes.AttributesOwnerCodeBlockCustom + if isd, ok := renderer.(hooks.IsDefaultCodeBlockRendererProvider); (ok && isd.IsDefaultCodeBlockRenderer()) || lexers.Get(lang) != nil { + // We say that this is a Chroma code block if it's the default code block renderer + // or if the language is supported by Chroma. + attrtp = attributes.AttributesOwnerCodeBlockChroma + } + + // IsDefaultCodeBlockRendererProvider attrs := getAttributes(n.b, info) cbctx := &codeBlockContext{ page: ctx.DocumentContext().Document, lang: lang, code: s, ordinal: ordinal, - AttributesHolder: attributes.New(attrs, attributes.AttributesOwnerCodeBlock), + AttributesHolder: attributes.New(attrs, attrtp), } cbctx.createPos = func() htext.Position { - if resolver, ok := renderer.(hooks.ElementPositionRevolver); ok { + if resolver, ok := renderer.(hooks.ElementPositionResolver); ok { return resolver.ResolvePosition(cbctx) } return htext.Position{ |