markup/goldmark: Enhance footnote extension with backlinkHTML option

This commit introduces a new option, backlinkHTML, to the Goldmark
footnote extension. This allows users to the set custom text for
footnote backlink anchors.

Closes #11434
This commit is contained in:
Joe Mooring
2025-09-28 09:45:52 -07:00
committed by Bjørn Erik Pedersen
parent 47678d8cbd
commit b462980ac2
4 changed files with 23 additions and 6 deletions
+1
View File
@@ -1239,6 +1239,7 @@ config:
superscript:
enable: false
footnote:
backlinkHTML: '↩︎'
enable: true
enableAutoIDPrefix: false
linkify: true
+9 -6
View File
@@ -177,14 +177,17 @@ func newMarkdown(pcfg converter.ProviderConfig) goldmark.Markdown {
}
if cfg.Extensions.Footnote.Enable {
opts := []extension.FootnoteOption{}
opts = append(opts, extension.WithFootnoteBacklinkHTML(cfg.Extensions.Footnote.BacklinkHTML))
if cfg.Extensions.Footnote.EnableAutoIDPrefix {
extensions = append(extensions, extension.NewFootnote(extension.WithFootnoteIDPrefixFunction(func(n ast.Node) []byte {
documentID := n.OwnerDocument().Meta()["documentID"].(string)
return []byte("h" + documentID)
})))
} else {
extensions = append(extensions, extension.Footnote)
opts = append(opts,
extension.WithFootnoteIDPrefixFunction(func(n ast.Node) []byte {
documentID := n.OwnerDocument().Meta()["documentID"].(string)
return []byte("h" + documentID)
}))
}
f := extension.NewFootnote(opts...)
extensions = append(extensions, f)
}
if cfg.Extensions.CJK.Enable {
@@ -43,6 +43,7 @@ var Default = Config{
Footnote: Footnote{
Enable: true,
EnableAutoIDPrefix: false,
BacklinkHTML: "↩︎",
},
DefinitionList: true,
Table: true,
@@ -173,6 +174,7 @@ type Extensions struct {
type Footnote struct {
Enable bool
EnableAutoIDPrefix bool
BacklinkHTML string
}
// Typographer holds typographer configuration.
@@ -971,6 +971,7 @@ disableKinds = ['home','rss','section','sitemap','taxonomy','term']
[markup.goldmark.extensions.footnote]
enable = false
enableAutoIDPrefix = false
TBD = 'back'
-- layouts/all.html --
{{ .Content }}
-- content/p1.md --
@@ -1000,6 +1001,8 @@ foo[^1] and bar[^2]
{{ end }}
`
// test enableAutoIDPrefix
want := "<p>foo[^1] and bar[^2]</p>\n<p>[^1]: footnote one\n[^2]: footnote two</p>"
b := hugolib.Test(t, files)
b.AssertFileContent("public/p1/index.html", want)
@@ -1029,4 +1032,12 @@ foo[^1] and bar[^2]
b.AssertFileContent("public/p4/index.html",
"<p>foo<sup id=\"ha35b794ad6e8626cfnref:1\"><a href=\"#ha35b794ad6e8626cfn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1</a></sup> and bar<sup id=\"ha35b794ad6e8626cfnref:2\"><a href=\"#ha35b794ad6e8626cfn:2\" class=\"footnote-ref\" role=\"doc-noteref\">2</a></sup></p>\n<div class=\"footnotes\" role=\"doc-endnotes\">\n<hr>\n<ol>\n<li id=\"ha35b794ad6e8626cfn:1\">\n<p>footnote one&#160;<a href=\"#ha35b794ad6e8626cfnref:1\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;</a></p>\n</li>\n<li id=\"ha35b794ad6e8626cfn:2\">\n<p>footnote two&#160;<a href=\"#ha35b794ad6e8626cfnref:2\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;</a></p>\n</li>\n</ol>\n</div>",
)
// test backlinkHTML
files = strings.ReplaceAll(files, "TBD", "backlinkHTML")
b = hugolib.Test(t, files)
b.AssertFileContent("public/p1/index.html",
"<p>foo<sup id=\"hb5cdcabc9e678612fnref:1\"><a href=\"#hb5cdcabc9e678612fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1</a></sup> and bar<sup id=\"hb5cdcabc9e678612fnref:2\"><a href=\"#hb5cdcabc9e678612fn:2\" class=\"footnote-ref\" role=\"doc-noteref\">2</a></sup></p>\n<div class=\"footnotes\" role=\"doc-endnotes\">\n<hr>\n<ol>\n<li id=\"hb5cdcabc9e678612fn:1\">\n<p>footnote one&#160;<a href=\"#hb5cdcabc9e678612fnref:1\" class=\"footnote-backref\" role=\"doc-backlink\">back</a></p>\n</li>\n<li id=\"hb5cdcabc9e678612fn:2\">\n<p>footnote two&#160;<a href=\"#hb5cdcabc9e678612fnref:2\" class=\"footnote-backref\" role=\"doc-backlink\">back</a></p>\n</li>\n</ol>\n</div>",
)
}