diff options
Diffstat (limited to 'markup/goldmark/toc.go')
-rw-r--r-- | markup/goldmark/toc.go | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/markup/goldmark/toc.go b/markup/goldmark/toc.go index 1753ede1b..4e3e5aec2 100644 --- a/markup/goldmark/toc.go +++ b/markup/goldmark/toc.go @@ -21,6 +21,7 @@ import ( "github.com/yuin/goldmark" "github.com/yuin/goldmark/ast" "github.com/yuin/goldmark/parser" + "github.com/yuin/goldmark/renderer" "github.com/yuin/goldmark/text" "github.com/yuin/goldmark/util" ) @@ -31,6 +32,7 @@ var ( ) type tocTransformer struct { + r renderer.Renderer } func (t *tocTransformer) Transform(n *ast.Document, reader text.Reader, pc parser.Context) { @@ -79,8 +81,26 @@ func (t *tocTransformer) Transform(n *ast.Document, reader text.Reader, pc parse if found { header.ID = string(id.([]byte)) } - case ast.KindText, ast.KindString: - headingText.Write(n.Text(reader.Source())) + case + ast.KindCodeSpan, + ast.KindLink, + ast.KindImage, + ast.KindEmphasis: + err := t.r.Render(&headingText, reader.Source(), n) + if err != nil { + return s, err + } + + return ast.WalkSkipChildren, nil + case + ast.KindAutoLink, + ast.KindRawHTML, + ast.KindText, + ast.KindString: + err := t.r.Render(&headingText, reader.Source(), n) + if err != nil { + return s, err + } } return s, nil @@ -90,12 +110,19 @@ func (t *tocTransformer) Transform(n *ast.Document, reader text.Reader, pc parse } type tocExtension struct { + options []renderer.Option } -func newTocExtension() goldmark.Extender { - return &tocExtension{} +func newTocExtension(options []renderer.Option) goldmark.Extender { + return &tocExtension{ + options: options, + } } func (e *tocExtension) Extend(m goldmark.Markdown) { - m.Parser().AddOptions(parser.WithASTTransformers(util.Prioritized(&tocTransformer{}, 10))) + r := goldmark.DefaultRenderer() + r.AddOptions(e.options...) + m.Parser().AddOptions(parser.WithASTTransformers(util.Prioritized(&tocTransformer{ + r: r, + }, 10))) } |