aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2024-08-05 16:36:36 +0200
committerBjørn Erik Pedersen <[email protected]>2024-08-07 18:28:23 +0200
commit4c162deb036a1f65769c12e6078ac79b70f5901b (patch)
tree61ffa62e16cc98aec2919a00a4cc19102ea7c302
parentc6227f1d8597f053986c13eac131ae5122a68444 (diff)
downloadhugo-4c162deb036a1f65769c12e6078ac79b70f5901b.tar.gz
hugo-4c162deb036a1f65769c12e6078ac79b70f5901b.zip
markup/goldmark/codeblocks: Simplify codeblcok hook code
-rw-r--r--markup/goldmark/codeblocks/render.go24
-rw-r--r--markup/goldmark/codeblocks/transform.go54
2 files changed, 10 insertions, 68 deletions
diff --git a/markup/goldmark/codeblocks/render.go b/markup/goldmark/codeblocks/render.go
index 15f968f46..51f3b20f8 100644
--- a/markup/goldmark/codeblocks/render.go
+++ b/markup/goldmark/codeblocks/render.go
@@ -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
index 829dfcc19..000000000
--- a/markup/goldmark/codeblocks/transform.go
+++ /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)
- }
- }
-}