]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
markup/goldmark: TOC: render strikethrough, emojis
authorJonas Zeiger <jonas.zeiger@talpidae.net>
Tue, 5 Dec 2023 23:29:03 +0000 (00:29 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 7 Mar 2024 13:08:29 +0000 (14:08 +0100)
Configure the TOC (TableOfContents, toc.go) goldmark renderer to always
enable the Strikethrough and Emoji extensions. This allows handling
ast.KindStrikethrough and ast.KindEmoji AST nodes when rendering the TOC.

Fixes #7169
Fixes #11783
Fixes #12022

markup/goldmark/convert.go
markup/goldmark/toc.go
markup/goldmark/toc_integration_test.go

index de06bedffed7d8ba0a20fcb624535a8647f30f0a..d835d17cfc3c8dca910cab91d62b0da2b59eacbd 100644 (file)
@@ -18,14 +18,14 @@ import (
        "bytes"
 
        "github.com/gohugoio/hugo-goldmark-extensions/passthrough"
+       "github.com/yuin/goldmark/util"
+
        "github.com/gohugoio/hugo/markup/goldmark/codeblocks"
        "github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
        "github.com/gohugoio/hugo/markup/goldmark/images"
        "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"
@@ -34,6 +34,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 (
@@ -91,10 +94,17 @@ func newMarkdown(pcfg converter.ProviderConfig) goldmark.Markdown {
                rendererOptions = append(rendererOptions, html.WithUnsafe())
        }
 
+       tocRendererOptions := make([]renderer.Option, len(rendererOptions))
+       if rendererOptions != nil {
+               copy(tocRendererOptions, rendererOptions)
+       }
+       tocRendererOptions = append(tocRendererOptions,
+               renderer.WithNodeRenderers(util.Prioritized(extension.NewStrikethroughHTMLRenderer(), 500)),
+               renderer.WithNodeRenderers(util.Prioritized(emoji.NewHTMLRenderer(), 200)))
        var (
                extensions = []goldmark.Extender{
                        newLinks(cfg),
-                       newTocExtension(rendererOptions),
+                       newTocExtension(tocRendererOptions),
                }
                parserOptions []parser.Option
        )
index bc209adf233d83b3004480ec7cde16a2bcd2295d..b0f7e703fc5eab65aa2b30aed2ddfabbe2e71254 100644 (file)
@@ -16,6 +16,10 @@ package goldmark
 import (
        "bytes"
 
+       strikethroughAst "github.com/yuin/goldmark/extension/ast"
+
+       emojiAst "github.com/yuin/goldmark-emoji/ast"
+
        "github.com/gohugoio/hugo/markup/tableofcontents"
 
        "github.com/yuin/goldmark"
@@ -86,7 +90,9 @@ func (t *tocTransformer) Transform(n *ast.Document, reader text.Reader, pc parse
                        ast.KindCodeSpan,
                        ast.KindLink,
                        ast.KindImage,
-                       ast.KindEmphasis:
+                       ast.KindEmphasis,
+                       strikethroughAst.KindStrikethrough,
+                       emojiAst.KindEmoji:
                        err := t.r.Render(&headingText, reader.Source(), n)
                        if err != nil {
                                return s, err
index f58f91f1c6d7b2df10f39bd3b51e9fe67d57dce5..3b48dac6cfc398b967bea5aaa3c9b93fba814a57 100644 (file)
@@ -253,14 +253,12 @@ title: p7 (emoji)
 `)
 
        // strikethrough
-       // TODO failing test: Issue #8087
-       //      b.AssertFileContent("public/p6/index.html", `
-       // <li><a href="#ome-deleted-text">Some <del>deleted</del> text</a></li>
-       //      `)
+       b.AssertFileContent("public/p6/index.html", `
+<li><a href="#some-deleted-text">Some <del>deleted</del> text</a></li>
+`)
 
        // emoji
-       // TODO failing test: Issue #12022
-       //      b.AssertFileContent("public/p7/index.html", `
-       // <li><a href="#a-snake-emoji">A &#x1f40d; emoji</a></li>
-       //              `)
+       b.AssertFileContent("public/p7/index.html", `
+<li><a href="#a-snake-emoji">A &#x1f40d; emoji</a></li>
+`)
 }