]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
markup/goldmark/codeblocks: Simplify codeblcok hook code
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 5 Aug 2024 14:36:36 +0000 (16:36 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 7 Aug 2024 16:28:23 +0000 (18:28 +0200)
markup/goldmark/codeblocks/render.go
markup/goldmark/codeblocks/transform.go [deleted file]

index 15f968f4643ce7e2193247d15bcd1c2828362d39..51f3b20f861acb74c159ef46d1473a3eefc0d7ca 100644 (file)
@@ -44,11 +44,6 @@ func New() goldmark.Extender {
 }
 
 func (e *codeBlocksExtension) Extend(m goldmark.Markdown) {
-       m.Parser().AddOptions(
-               parser.WithASTTransformers(
-                       util.Prioritized(&Transformer{}, 100),
-               ),
-       )
        m.Renderer().AddOptions(renderer.WithNodeRenderers(
                util.Prioritized(newHTMLRenderer(), 100),
        ))
@@ -60,7 +55,7 @@ func newHTMLRenderer() renderer.NodeRenderer {
 }
 
 func (r *htmlRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
-       reg.Register(KindCodeBlock, r.renderCodeBlock)
+       reg.Register(ast.KindFencedCodeBlock, r.renderCodeBlock)
 }
 
 func (r *htmlRenderer) renderCodeBlock(w util.BufWriter, src []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
@@ -70,28 +65,29 @@ func (r *htmlRenderer) renderCodeBlock(w util.BufWriter, src []byte, node ast.No
                return ast.WalkContinue, nil
        }
 
-       n := node.(*codeBlock)
-       lang := getLang(n.b, src)
+       n := node.(*ast.FencedCodeBlock)
+
+       lang := getLang(n, src)
        renderer := ctx.RenderContext().GetRenderer(hooks.CodeBlockRendererType, lang)
        if renderer == nil {
                return ast.WalkStop, fmt.Errorf("no code renderer found for %q", lang)
        }
 
-       ordinal := n.ordinal
+       ordinal := ctx.GetAndIncrementOrdinal(ast.KindFencedCodeBlock)
 
        var buff bytes.Buffer
 
-       l := n.b.Lines().Len()
+       l := n.Lines().Len()
        for i := 0; i < l; i++ {
-               line := n.b.Lines().At(i)
+               line := n.Lines().At(i)
                buff.Write(line.Value(src))
        }
 
        s := htext.Chomp(buff.String())
 
        var info []byte
-       if n.b.Info != nil {
-               info = n.b.Info.Segment.Value(src)
+       if n.Info != nil {
+               info = n.Info.Segment.Value(src)
        }
 
        attrtp := attributes.AttributesOwnerCodeBlockCustom
@@ -101,7 +97,7 @@ func (r *htmlRenderer) renderCodeBlock(w util.BufWriter, src []byte, node ast.No
                attrtp = attributes.AttributesOwnerCodeBlockChroma
        }
 
-       attrs, attrStr, err := getAttributes(n.b, info)
+       attrs, attrStr, err := getAttributes(n, info)
        if err != nil {
                return ast.WalkStop, &herrors.TextSegmentError{Err: err, Segment: attrStr}
        }
diff --git a/markup/goldmark/codeblocks/transform.go b/markup/goldmark/codeblocks/transform.go
deleted file mode 100644 (file)
index 829dfcc..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-package codeblocks
-
-import (
-       "github.com/yuin/goldmark/ast"
-       "github.com/yuin/goldmark/parser"
-       "github.com/yuin/goldmark/text"
-)
-
-// KindCodeBlock is the kind of an Hugo code block.
-var KindCodeBlock = ast.NewNodeKind("HugoCodeBlock")
-
-// Its raw contents are the plain text of the code block.
-type codeBlock struct {
-       ast.BaseBlock
-       ordinal int
-       b       *ast.FencedCodeBlock
-}
-
-func (*codeBlock) Kind() ast.NodeKind { return KindCodeBlock }
-
-func (*codeBlock) IsRaw() bool { return true }
-
-func (b *codeBlock) Dump(src []byte, level int) {
-}
-
-type Transformer struct{}
-
-// Transform transforms the provided Markdown AST.
-func (*Transformer) Transform(doc *ast.Document, reader text.Reader, pctx parser.Context) {
-       var codeBlocks []*ast.FencedCodeBlock
-
-       ast.Walk(doc, func(node ast.Node, enter bool) (ast.WalkStatus, error) {
-               if !enter {
-                       return ast.WalkContinue, nil
-               }
-
-               cb, ok := node.(*ast.FencedCodeBlock)
-               if !ok {
-                       return ast.WalkContinue, nil
-               }
-
-               codeBlocks = append(codeBlocks, cb)
-
-               return ast.WalkContinue, nil
-       })
-
-       for i, cb := range codeBlocks {
-               b := &codeBlock{b: cb, ordinal: i}
-               parent := cb.Parent()
-               if parent != nil {
-                       parent.ReplaceChild(parent, cb, b)
-               }
-       }
-}