Skip to content

Commit 61482cf

Browse files
jmooringbep
authored andcommitted
markup/goldmark: Apply Hugo Goldmark Extras when rendering TOC
Closes #12605
1 parent 04ee1b9 commit 61482cf

File tree

5 files changed

+89
-5
lines changed

5 files changed

+89
-5
lines changed

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ require (
3838
github.com/gohugoio/go-i18n/v2 v2.1.3-0.20230805085216-e63c13218d0e
3939
github.com/gohugoio/hashstructure v0.5.0
4040
github.com/gohugoio/httpcache v0.7.0
41-
github.com/gohugoio/hugo-goldmark-extensions/extras v0.3.0
41+
github.com/gohugoio/hugo-goldmark-extensions/extras v0.5.0
4242
github.com/gohugoio/hugo-goldmark-extensions/passthrough v0.3.1
4343
github.com/gohugoio/locales v0.14.0
4444
github.com/gohugoio/localescompressed v1.0.1

‎go.sum‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ github.com/gohugoio/hashstructure v0.5.0 h1:G2fjSBU36RdwEJBWJ+919ERvOVqAg9tfcYp4
261261
github.com/gohugoio/hashstructure v0.5.0/go.mod h1:Ser0TniXuu/eauYmrwM4o64EBvySxNzITEOLlm4igec=
262262
github.com/gohugoio/httpcache v0.7.0 h1:ukPnn04Rgvx48JIinZvZetBfHaWE7I01JR2Q2RrQ3Vs=
263263
github.com/gohugoio/httpcache v0.7.0/go.mod h1:fMlPrdY/vVJhAriLZnrF5QpN3BNAcoBClgAyQd+lGFI=
264-
github.com/gohugoio/hugo-goldmark-extensions/extras v0.3.0 h1:gj49kTR5Z4Hnm0ZaQrgPVazL3DUkppw+x6XhHCmh+Wk=
265-
github.com/gohugoio/hugo-goldmark-extensions/extras v0.3.0/go.mod h1:IMMj7xiUbLt1YNJ6m7AM4cnsX4cFnnfkleO/lBHGzUg=
264+
github.com/gohugoio/hugo-goldmark-extensions/extras v0.5.0 h1:dco+7YiOryRoPOMXwwaf+kktZSCtlFtreNdiJbETvYE=
265+
github.com/gohugoio/hugo-goldmark-extensions/extras v0.5.0/go.mod h1:CRrxQTKeM3imw+UoS4EHKyrqB7Zp6sAJiqHit+aMGTE=
266266
github.com/gohugoio/hugo-goldmark-extensions/passthrough v0.3.1 h1:nUzXfRTszLliZuN0JTKeunXTRaiFX6ksaWP0puLLYAY=
267267
github.com/gohugoio/hugo-goldmark-extensions/passthrough v0.3.1/go.mod h1:Wy8ThAA8p2/w1DY05vEzq6EIeI2mzDjvHsu7ULBVwog=
268268
github.com/gohugoio/locales v0.14.0 h1:Q0gpsZwfv7ATHMbcTNepFd59H7GoykzWJIxi113XGDc=

‎markup/goldmark/convert.go‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,26 @@ func newMarkdown(pcfg converter.ProviderConfig) goldmark.Markdown {
102102
if rendererOptions != nil {
103103
copy(tocRendererOptions, rendererOptions)
104104
}
105+
105106
tocRendererOptions = append(tocRendererOptions,
106107
renderer.WithNodeRenderers(util.Prioritized(extension.NewStrikethroughHTMLRenderer(), 500)),
107-
renderer.WithNodeRenderers(util.Prioritized(emoji.NewHTMLRenderer(), 200)))
108+
renderer.WithNodeRenderers(util.Prioritized(emoji.NewHTMLRenderer(), 200)),
109+
)
110+
111+
inlineTags := []extras.InlineTag{
112+
extras.DeleteTag,
113+
extras.InsertTag,
114+
extras.MarkTag,
115+
extras.SubscriptTag,
116+
extras.SuperscriptTag,
117+
}
118+
119+
for _, tag := range inlineTags {
120+
tocRendererOptions = append(tocRendererOptions,
121+
renderer.WithNodeRenderers(util.Prioritized(extras.NewInlineTagHTMLRenderer(tag), tag.RenderPriority)),
122+
)
123+
}
124+
108125
var (
109126
extensions = []goldmark.Extender{
110127
hugocontext.New(pcfg.Logger),

‎markup/goldmark/toc.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
emojiAst "github.com/yuin/goldmark-emoji/ast"
2525

26+
"github.com/gohugoio/hugo-goldmark-extensions/extras"
2627
"github.com/gohugoio/hugo/markup/tableofcontents"
2728

2829
"github.com/yuin/goldmark"
@@ -99,7 +100,12 @@ func (t *tocTransformer) Transform(n *ast.Document, reader text.Reader, pc parse
99100
ast.KindImage,
100101
ast.KindEmphasis,
101102
strikethroughAst.KindStrikethrough,
102-
emojiAst.KindEmoji:
103+
emojiAst.KindEmoji,
104+
extras.KindDelete,
105+
extras.KindInsert,
106+
extras.KindMark,
107+
extras.KindSubscript,
108+
extras.KindSuperscript:
103109
err := t.r.Render(&headingText, reader.Source(), n)
104110
if err != nil {
105111
return s, err

‎markup/goldmark/toc_integration_test.go‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package goldmark_test
1515

1616
import (
17+
"strings"
1718
"testing"
1819

1920
"github.com/gohugoio/hugo/hugolib"
@@ -303,3 +304,63 @@ title: home
303304

304305
b.AssertFileExists("public/index.html", true)
305306
}
307+
308+
// Issue 12605
309+
func TestTableOfContentsWithGoldmarkExtras(t *testing.T) {
310+
t.Parallel()
311+
312+
files := `
313+
-- hugo.toml --
314+
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
315+
[markup.goldmark.extensions]
316+
strikethrough = false
317+
[markup.goldmark.extensions.extras.delete]
318+
enable = true
319+
[markup.goldmark.extensions.extras.insert]
320+
enable = true
321+
[markup.goldmark.extensions.extras.mark]
322+
enable = true
323+
[markup.goldmark.extensions.extras.subscript]
324+
enable = true
325+
[markup.goldmark.extensions.extras.superscript]
326+
enable = true
327+
328+
-- content/_index.md --
329+
---
330+
title: home
331+
---
332+
## ~~deleted~~
333+
334+
## ++inserted++
335+
336+
## ==marked==
337+
338+
## H~2~O
339+
340+
## 1^st^
341+
-- layouts/home.html --
342+
{{ .TableOfContents }}
343+
`
344+
345+
b := hugolib.Test(t, files)
346+
347+
b.AssertFileContent("public/index.html",
348+
`<li><a href="#deleted"><del>deleted</del></a></li>`,
349+
`<li><a href="#inserted"><ins>inserted</ins></a></li>`,
350+
`<li><a href="#marked"><mark>marked</mark></a></li>`,
351+
`<li><a href="#h2o">H<sub>2</sub>O</a></li>`,
352+
`<li><a href="#1st">1<sup>st</sup></a></li>`,
353+
)
354+
355+
files = strings.ReplaceAll(files, "enable = true", "enable = false")
356+
357+
b = hugolib.Test(t, files)
358+
359+
b.AssertFileContent("public/index.html",
360+
`<li><a href="#deleted">~~deleted~~</a></li>`,
361+
`<li><a href="#inserted">++inserted++</a></li>`,
362+
`<li><a href="#marked">==marked==</a></li>`,
363+
`<li><a href="#h2o">H~2~O</a></li>`,
364+
`<li><a href="#1st">1^st^</a></li>`,
365+
)
366+
}

0 commit comments

Comments
 (0)