mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-16 20:53:59 +02:00
markup/goldmark: Apply Hugo Goldmark Extras when rendering TOC
Closes #12605
This commit is contained in:
committed by
Bjørn Erik Pedersen
parent
04ee1b9784
commit
61482cfab6
2
go.mod
2
go.mod
@@ -38,7 +38,7 @@ require (
|
|||||||
github.com/gohugoio/go-i18n/v2 v2.1.3-0.20230805085216-e63c13218d0e
|
github.com/gohugoio/go-i18n/v2 v2.1.3-0.20230805085216-e63c13218d0e
|
||||||
github.com/gohugoio/hashstructure v0.5.0
|
github.com/gohugoio/hashstructure v0.5.0
|
||||||
github.com/gohugoio/httpcache v0.7.0
|
github.com/gohugoio/httpcache v0.7.0
|
||||||
github.com/gohugoio/hugo-goldmark-extensions/extras v0.3.0
|
github.com/gohugoio/hugo-goldmark-extensions/extras v0.5.0
|
||||||
github.com/gohugoio/hugo-goldmark-extensions/passthrough v0.3.1
|
github.com/gohugoio/hugo-goldmark-extensions/passthrough v0.3.1
|
||||||
github.com/gohugoio/locales v0.14.0
|
github.com/gohugoio/locales v0.14.0
|
||||||
github.com/gohugoio/localescompressed v1.0.1
|
github.com/gohugoio/localescompressed v1.0.1
|
||||||
|
4
go.sum
4
go.sum
@@ -261,8 +261,8 @@ github.com/gohugoio/hashstructure v0.5.0 h1:G2fjSBU36RdwEJBWJ+919ERvOVqAg9tfcYp4
|
|||||||
github.com/gohugoio/hashstructure v0.5.0/go.mod h1:Ser0TniXuu/eauYmrwM4o64EBvySxNzITEOLlm4igec=
|
github.com/gohugoio/hashstructure v0.5.0/go.mod h1:Ser0TniXuu/eauYmrwM4o64EBvySxNzITEOLlm4igec=
|
||||||
github.com/gohugoio/httpcache v0.7.0 h1:ukPnn04Rgvx48JIinZvZetBfHaWE7I01JR2Q2RrQ3Vs=
|
github.com/gohugoio/httpcache v0.7.0 h1:ukPnn04Rgvx48JIinZvZetBfHaWE7I01JR2Q2RrQ3Vs=
|
||||||
github.com/gohugoio/httpcache v0.7.0/go.mod h1:fMlPrdY/vVJhAriLZnrF5QpN3BNAcoBClgAyQd+lGFI=
|
github.com/gohugoio/httpcache v0.7.0/go.mod h1:fMlPrdY/vVJhAriLZnrF5QpN3BNAcoBClgAyQd+lGFI=
|
||||||
github.com/gohugoio/hugo-goldmark-extensions/extras v0.3.0 h1:gj49kTR5Z4Hnm0ZaQrgPVazL3DUkppw+x6XhHCmh+Wk=
|
github.com/gohugoio/hugo-goldmark-extensions/extras v0.5.0 h1:dco+7YiOryRoPOMXwwaf+kktZSCtlFtreNdiJbETvYE=
|
||||||
github.com/gohugoio/hugo-goldmark-extensions/extras v0.3.0/go.mod h1:IMMj7xiUbLt1YNJ6m7AM4cnsX4cFnnfkleO/lBHGzUg=
|
github.com/gohugoio/hugo-goldmark-extensions/extras v0.5.0/go.mod h1:CRrxQTKeM3imw+UoS4EHKyrqB7Zp6sAJiqHit+aMGTE=
|
||||||
github.com/gohugoio/hugo-goldmark-extensions/passthrough v0.3.1 h1:nUzXfRTszLliZuN0JTKeunXTRaiFX6ksaWP0puLLYAY=
|
github.com/gohugoio/hugo-goldmark-extensions/passthrough v0.3.1 h1:nUzXfRTszLliZuN0JTKeunXTRaiFX6ksaWP0puLLYAY=
|
||||||
github.com/gohugoio/hugo-goldmark-extensions/passthrough v0.3.1/go.mod h1:Wy8ThAA8p2/w1DY05vEzq6EIeI2mzDjvHsu7ULBVwog=
|
github.com/gohugoio/hugo-goldmark-extensions/passthrough v0.3.1/go.mod h1:Wy8ThAA8p2/w1DY05vEzq6EIeI2mzDjvHsu7ULBVwog=
|
||||||
github.com/gohugoio/locales v0.14.0 h1:Q0gpsZwfv7ATHMbcTNepFd59H7GoykzWJIxi113XGDc=
|
github.com/gohugoio/locales v0.14.0 h1:Q0gpsZwfv7ATHMbcTNepFd59H7GoykzWJIxi113XGDc=
|
||||||
|
@@ -102,9 +102,26 @@ func newMarkdown(pcfg converter.ProviderConfig) goldmark.Markdown {
|
|||||||
if rendererOptions != nil {
|
if rendererOptions != nil {
|
||||||
copy(tocRendererOptions, rendererOptions)
|
copy(tocRendererOptions, rendererOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
tocRendererOptions = append(tocRendererOptions,
|
tocRendererOptions = append(tocRendererOptions,
|
||||||
renderer.WithNodeRenderers(util.Prioritized(extension.NewStrikethroughHTMLRenderer(), 500)),
|
renderer.WithNodeRenderers(util.Prioritized(extension.NewStrikethroughHTMLRenderer(), 500)),
|
||||||
renderer.WithNodeRenderers(util.Prioritized(emoji.NewHTMLRenderer(), 200)))
|
renderer.WithNodeRenderers(util.Prioritized(emoji.NewHTMLRenderer(), 200)),
|
||||||
|
)
|
||||||
|
|
||||||
|
inlineTags := []extras.InlineTag{
|
||||||
|
extras.DeleteTag,
|
||||||
|
extras.InsertTag,
|
||||||
|
extras.MarkTag,
|
||||||
|
extras.SubscriptTag,
|
||||||
|
extras.SuperscriptTag,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tag := range inlineTags {
|
||||||
|
tocRendererOptions = append(tocRendererOptions,
|
||||||
|
renderer.WithNodeRenderers(util.Prioritized(extras.NewInlineTagHTMLRenderer(tag), tag.RenderPriority)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
extensions = []goldmark.Extender{
|
extensions = []goldmark.Extender{
|
||||||
hugocontext.New(pcfg.Logger),
|
hugocontext.New(pcfg.Logger),
|
||||||
|
@@ -23,6 +23,7 @@ import (
|
|||||||
|
|
||||||
emojiAst "github.com/yuin/goldmark-emoji/ast"
|
emojiAst "github.com/yuin/goldmark-emoji/ast"
|
||||||
|
|
||||||
|
"github.com/gohugoio/hugo-goldmark-extensions/extras"
|
||||||
"github.com/gohugoio/hugo/markup/tableofcontents"
|
"github.com/gohugoio/hugo/markup/tableofcontents"
|
||||||
|
|
||||||
"github.com/yuin/goldmark"
|
"github.com/yuin/goldmark"
|
||||||
@@ -99,7 +100,12 @@ func (t *tocTransformer) Transform(n *ast.Document, reader text.Reader, pc parse
|
|||||||
ast.KindImage,
|
ast.KindImage,
|
||||||
ast.KindEmphasis,
|
ast.KindEmphasis,
|
||||||
strikethroughAst.KindStrikethrough,
|
strikethroughAst.KindStrikethrough,
|
||||||
emojiAst.KindEmoji:
|
emojiAst.KindEmoji,
|
||||||
|
extras.KindDelete,
|
||||||
|
extras.KindInsert,
|
||||||
|
extras.KindMark,
|
||||||
|
extras.KindSubscript,
|
||||||
|
extras.KindSuperscript:
|
||||||
err := t.r.Render(&headingText, reader.Source(), n)
|
err := t.r.Render(&headingText, reader.Source(), n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return s, err
|
return s, err
|
||||||
|
@@ -14,6 +14,7 @@
|
|||||||
package goldmark_test
|
package goldmark_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/hugolib"
|
"github.com/gohugoio/hugo/hugolib"
|
||||||
@@ -303,3 +304,63 @@ title: home
|
|||||||
|
|
||||||
b.AssertFileExists("public/index.html", true)
|
b.AssertFileExists("public/index.html", true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue 12605
|
||||||
|
func TestTableOfContentsWithGoldmarkExtras(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
files := `
|
||||||
|
-- hugo.toml --
|
||||||
|
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
|
||||||
|
[markup.goldmark.extensions]
|
||||||
|
strikethrough = false
|
||||||
|
[markup.goldmark.extensions.extras.delete]
|
||||||
|
enable = true
|
||||||
|
[markup.goldmark.extensions.extras.insert]
|
||||||
|
enable = true
|
||||||
|
[markup.goldmark.extensions.extras.mark]
|
||||||
|
enable = true
|
||||||
|
[markup.goldmark.extensions.extras.subscript]
|
||||||
|
enable = true
|
||||||
|
[markup.goldmark.extensions.extras.superscript]
|
||||||
|
enable = true
|
||||||
|
|
||||||
|
-- content/_index.md --
|
||||||
|
---
|
||||||
|
title: home
|
||||||
|
---
|
||||||
|
## ~~deleted~~
|
||||||
|
|
||||||
|
## ++inserted++
|
||||||
|
|
||||||
|
## ==marked==
|
||||||
|
|
||||||
|
## H~2~O
|
||||||
|
|
||||||
|
## 1^st^
|
||||||
|
-- layouts/home.html --
|
||||||
|
{{ .TableOfContents }}
|
||||||
|
`
|
||||||
|
|
||||||
|
b := hugolib.Test(t, files)
|
||||||
|
|
||||||
|
b.AssertFileContent("public/index.html",
|
||||||
|
`<li><a href="#deleted"><del>deleted</del></a></li>`,
|
||||||
|
`<li><a href="#inserted"><ins>inserted</ins></a></li>`,
|
||||||
|
`<li><a href="#marked"><mark>marked</mark></a></li>`,
|
||||||
|
`<li><a href="#h2o">H<sub>2</sub>O</a></li>`,
|
||||||
|
`<li><a href="#1st">1<sup>st</sup></a></li>`,
|
||||||
|
)
|
||||||
|
|
||||||
|
files = strings.ReplaceAll(files, "enable = true", "enable = false")
|
||||||
|
|
||||||
|
b = hugolib.Test(t, files)
|
||||||
|
|
||||||
|
b.AssertFileContent("public/index.html",
|
||||||
|
`<li><a href="#deleted">~~deleted~~</a></li>`,
|
||||||
|
`<li><a href="#inserted">++inserted++</a></li>`,
|
||||||
|
`<li><a href="#marked">==marked==</a></li>`,
|
||||||
|
`<li><a href="#h2o">H~2~O</a></li>`,
|
||||||
|
`<li><a href="#1st">1^st^</a></li>`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user