diff options
author | satotake <doublequotation@gmail.com> | 2020-02-23 02:06:30 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-22 18:06:30 +0100 |
commit | ca68abf0bc2fa003c2052143218f7b2ab195a46e (patch) | |
tree | da1732c53edd4ba003e458a74efc0f82bdefc612 /markup/goldmark/toc.go | |
parent | a524124beb0e7ca226c207ea48a90cea2cbef76e (diff) |
Fix goldmark toc rendering
Previously gordmark-based TOC renderes only `KindText` and `KindString`
This commit expands target node with Goldmark's renderer
I am not sure of what are expected results as TOC contents in some (rare) cases
but Blackfriday's behaviours are fundamentally respected.
For example,
- image `[image text](link)` is rendered as `<img>` tag
- GFM AutoLink `gohugo.io` is rendered as text
* Render AutoLink as <a> tag as Blackfriday does
Fixes #6736
Fixes #6809
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))) } |