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_test.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_test.go')
-rw-r--r-- | markup/goldmark/toc_test.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/markup/goldmark/toc_test.go b/markup/goldmark/toc_test.go index 19928dd8e..464441335 100644 --- a/markup/goldmark/toc_test.go +++ b/markup/goldmark/toc_test.go @@ -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 < B & C > D</a></li> + <li><a href="#a--b--c--d-divfoodiv">A < B & C > 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 < B & C > D</a></li> + <li><a href="#a--b--c--d-divfoodiv">A < B & C > 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)) +} |