Skip to content

Commit

Permalink
markup/goldmark: Add strikethrough extension to toc renderer
Browse files Browse the repository at this point in the history
Configure the TOC (TableOfContents, toc.go) goldmark renderer to always
enable the Strikethrough extension. This allows handling ast.KindStrikethrough
within an AST node.

The last test in TestEscapeToc() is corrected to not expect <div>
elements to be included in the TOC renderer output.

Fixes #7169
Fixes #8087
Fixes #11783
  • Loading branch information
lyind committed Feb 9, 2024
1 parent 5186e76 commit c6cc57b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
18 changes: 15 additions & 3 deletions markup/goldmark/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package goldmark
import (
"bytes"

"github.com/yuin/goldmark/util"

"github.com/gohugoio/hugo/identity"

"github.com/gohugoio/hugo/markup/goldmark/codeblocks"
Expand All @@ -25,8 +27,6 @@ import (
"github.com/gohugoio/hugo/markup/goldmark/internal/extensions/attributes"
"github.com/gohugoio/hugo/markup/goldmark/internal/render"

"github.com/gohugoio/hugo/markup/converter"
"github.com/gohugoio/hugo/markup/tableofcontents"
"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
"github.com/yuin/goldmark/ast"
Expand All @@ -35,6 +35,9 @@ import (
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/text"

"github.com/gohugoio/hugo/markup/converter"
"github.com/gohugoio/hugo/markup/tableofcontents"
)

const (
Expand Down Expand Up @@ -79,23 +82,32 @@ func newMarkdown(pcfg converter.ProviderConfig) goldmark.Markdown {
mcfg := pcfg.MarkupConfig()
cfg := mcfg.Goldmark
var rendererOptions []renderer.Option
var htmlOptions []html.Option

if cfg.Renderer.HardWraps {
rendererOptions = append(rendererOptions, html.WithHardWraps())
htmlOptions = append(htmlOptions, html.WithHardWraps())
}

if cfg.Renderer.XHTML {
rendererOptions = append(rendererOptions, html.WithXHTML())
htmlOptions = append(htmlOptions, html.WithXHTML())
}

if cfg.Renderer.Unsafe {
rendererOptions = append(rendererOptions, html.WithUnsafe())
htmlOptions = append(htmlOptions, html.WithUnsafe())
}

tocRendererOptions := make([]renderer.Option, 0)
if rendererOptions != nil {
copy(tocRendererOptions, rendererOptions)
}
tocRendererOptions = append(tocRendererOptions, renderer.WithNodeRenderers(util.Prioritized(extension.NewStrikethroughHTMLRenderer(htmlOptions...), 100)))
var (
extensions = []goldmark.Extender{
newLinks(cfg),
newTocExtension(rendererOptions),
newTocExtension(tocRendererOptions),
}
parserOptions []parser.Option
)
Expand Down
2 changes: 1 addition & 1 deletion markup/goldmark/toc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestEscapeToc(t *testing.T) {
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="#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>
Expand Down

0 comments on commit c6cc57b

Please sign in to comment.