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
This commit is contained in:
satotake
2020-02-23 02:06:30 +09:00
committed by GitHub
parent a524124beb
commit ca68abf0bc
4 changed files with 99 additions and 15 deletions

View File

@@ -15,6 +15,7 @@
package goldmark
import (
"strings"
"testing"
"github.com/gohugoio/hugo/markup/markup_config"
@@ -74,3 +75,59 @@ And then some.
</ul>
</nav>`, qt.Commentf(got))
}
func TestEscapeToc(t *testing.T) {
c := qt.New(t)
defaultConfig := markup_config.Default
safeConfig := defaultConfig
unsafeConfig := defaultConfig
safeConfig.Goldmark.Renderer.Unsafe = false
unsafeConfig.Goldmark.Renderer.Unsafe = true
safeP, _ := Provider.New(
converter.ProviderConfig{
MarkupConfig: safeConfig,
Logger: loggers.NewErrorLogger(),
})
unsafeP, _ := Provider.New(
converter.ProviderConfig{
MarkupConfig: unsafeConfig,
Logger: loggers.NewErrorLogger(),
})
safeConv, _ := safeP.New(converter.DocumentContext{})
unsafeConv, _ := unsafeP.New(converter.DocumentContext{})
content := strings.Join([]string{
"# A < B & C > D",
"# A < B & C > D <div>foo</div>",
"# *EMPHASIS*",
"# `echo codeblock`",
}, "\n")
// content := ""
b, err := safeConv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true})
c.Assert(err, qt.IsNil)
got := b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(1, 2, false)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul>
<li><a href="#a--b--c--d">A &lt; B &amp; C &gt; D</a></li>
<li><a href="#a--b--c--d-divfoodiv">A &lt; B &amp; C &gt; D <!-- raw HTML omitted -->foo<!-- raw HTML omitted --></a></li>
<li><a href="#emphasis"><em>EMPHASIS</em></a></li>
<li><a href="#echo-codeblock"><code>echo codeblock</code></a></li>
</ul>
</nav>`, qt.Commentf(got))
b, err = unsafeConv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true})
c.Assert(err, qt.IsNil)
got = b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(1, 2, false)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul>
<li><a href="#a--b--c--d">A &lt; B &amp; C &gt; D</a></li>
<li><a href="#a--b--c--d-divfoodiv">A &lt; B &amp; C &gt; D <div>foo</div></a></li>
<li><a href="#emphasis"><em>EMPHASIS</em></a></li>
<li><a href="#echo-codeblock"><code>echo codeblock</code></a></li>
</ul>
</nav>`, qt.Commentf(got))
}