mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
markup/goldmark/codeblocks: Simplify codeblcok hook code
This commit is contained in:
parent
c6227f1d85
commit
4c162deb03
2 changed files with 10 additions and 68 deletions
|
@ -44,11 +44,6 @@ func New() goldmark.Extender {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *codeBlocksExtension) Extend(m goldmark.Markdown) {
|
func (e *codeBlocksExtension) Extend(m goldmark.Markdown) {
|
||||||
m.Parser().AddOptions(
|
|
||||||
parser.WithASTTransformers(
|
|
||||||
util.Prioritized(&Transformer{}, 100),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
m.Renderer().AddOptions(renderer.WithNodeRenderers(
|
m.Renderer().AddOptions(renderer.WithNodeRenderers(
|
||||||
util.Prioritized(newHTMLRenderer(), 100),
|
util.Prioritized(newHTMLRenderer(), 100),
|
||||||
))
|
))
|
||||||
|
@ -60,7 +55,7 @@ func newHTMLRenderer() renderer.NodeRenderer {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *htmlRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
|
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) {
|
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
|
return ast.WalkContinue, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
n := node.(*codeBlock)
|
n := node.(*ast.FencedCodeBlock)
|
||||||
lang := getLang(n.b, src)
|
|
||||||
|
lang := getLang(n, src)
|
||||||
renderer := ctx.RenderContext().GetRenderer(hooks.CodeBlockRendererType, lang)
|
renderer := ctx.RenderContext().GetRenderer(hooks.CodeBlockRendererType, lang)
|
||||||
if renderer == nil {
|
if renderer == nil {
|
||||||
return ast.WalkStop, fmt.Errorf("no code renderer found for %q", lang)
|
return ast.WalkStop, fmt.Errorf("no code renderer found for %q", lang)
|
||||||
}
|
}
|
||||||
|
|
||||||
ordinal := n.ordinal
|
ordinal := ctx.GetAndIncrementOrdinal(ast.KindFencedCodeBlock)
|
||||||
|
|
||||||
var buff bytes.Buffer
|
var buff bytes.Buffer
|
||||||
|
|
||||||
l := n.b.Lines().Len()
|
l := n.Lines().Len()
|
||||||
for i := 0; i < l; i++ {
|
for i := 0; i < l; i++ {
|
||||||
line := n.b.Lines().At(i)
|
line := n.Lines().At(i)
|
||||||
buff.Write(line.Value(src))
|
buff.Write(line.Value(src))
|
||||||
}
|
}
|
||||||
|
|
||||||
s := htext.Chomp(buff.String())
|
s := htext.Chomp(buff.String())
|
||||||
|
|
||||||
var info []byte
|
var info []byte
|
||||||
if n.b.Info != nil {
|
if n.Info != nil {
|
||||||
info = n.b.Info.Segment.Value(src)
|
info = n.Info.Segment.Value(src)
|
||||||
}
|
}
|
||||||
|
|
||||||
attrtp := attributes.AttributesOwnerCodeBlockCustom
|
attrtp := attributes.AttributesOwnerCodeBlockCustom
|
||||||
|
@ -101,7 +97,7 @@ func (r *htmlRenderer) renderCodeBlock(w util.BufWriter, src []byte, node ast.No
|
||||||
attrtp = attributes.AttributesOwnerCodeBlockChroma
|
attrtp = attributes.AttributesOwnerCodeBlockChroma
|
||||||
}
|
}
|
||||||
|
|
||||||
attrs, attrStr, err := getAttributes(n.b, info)
|
attrs, attrStr, err := getAttributes(n, info)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ast.WalkStop, &herrors.TextSegmentError{Err: err, Segment: attrStr}
|
return ast.WalkStop, &herrors.TextSegmentError{Err: err, Segment: attrStr}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue