Skip to content

Commit b462980

Browse files
jmooringbep
authored andcommitted
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
1 parent 47678d8 commit b462980

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

‎docs/data/docs.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,7 @@ config:
12391239
superscript:
12401240
enable: false
12411241
footnote:
1242+
backlinkHTML: '↩︎'
12421243
enable: true
12431244
enableAutoIDPrefix: false
12441245
linkify: true

‎markup/goldmark/convert.go‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,17 @@ func newMarkdown(pcfg converter.ProviderConfig) goldmark.Markdown {
177177
}
178178

179179
if cfg.Extensions.Footnote.Enable {
180+
opts := []extension.FootnoteOption{}
181+
opts = append(opts, extension.WithFootnoteBacklinkHTML(cfg.Extensions.Footnote.BacklinkHTML))
180182
if cfg.Extensions.Footnote.EnableAutoIDPrefix {
181-
extensions = append(extensions, extension.NewFootnote(extension.WithFootnoteIDPrefixFunction(func(n ast.Node) []byte {
182-
documentID := n.OwnerDocument().Meta()["documentID"].(string)
183-
return []byte("h" + documentID)
184-
})))
185-
} else {
186-
extensions = append(extensions, extension.Footnote)
183+
opts = append(opts,
184+
extension.WithFootnoteIDPrefixFunction(func(n ast.Node) []byte {
185+
documentID := n.OwnerDocument().Meta()["documentID"].(string)
186+
return []byte("h" + documentID)
187+
}))
187188
}
189+
f := extension.NewFootnote(opts...)
190+
extensions = append(extensions, f)
188191
}
189192

190193
if cfg.Extensions.CJK.Enable {

‎markup/goldmark/goldmark_config/config.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var Default = Config{
4343
Footnote: Footnote{
4444
Enable: true,
4545
EnableAutoIDPrefix: false,
46+
BacklinkHTML: "↩︎",
4647
},
4748
DefinitionList: true,
4849
Table: true,
@@ -173,6 +174,7 @@ type Extensions struct {
173174
type Footnote struct {
174175
Enable bool
175176
EnableAutoIDPrefix bool
177+
BacklinkHTML string
176178
}
177179

178180
// Typographer holds typographer configuration.

‎markup/goldmark/goldmark_integration_test.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,7 @@ disableKinds = ['home','rss','section','sitemap','taxonomy','term']
971971
[markup.goldmark.extensions.footnote]
972972
enable = false
973973
enableAutoIDPrefix = false
974+
TBD = 'back'
974975
-- layouts/all.html --
975976
{{ .Content }}
976977
-- content/p1.md --
@@ -1000,6 +1001,8 @@ foo[^1] and bar[^2]
10001001
{{ end }}
10011002
`
10021003

1004+
// test enableAutoIDPrefix
1005+
10031006
want := "<p>foo[^1] and bar[^2]</p>\n<p>[^1]: footnote one\n[^2]: footnote two</p>"
10041007
b := hugolib.Test(t, files)
10051008
b.AssertFileContent("public/p1/index.html", want)
@@ -1029,4 +1032,12 @@ foo[^1] and bar[^2]
10291032
b.AssertFileContent("public/p4/index.html",
10301033
"<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>",
10311034
)
1035+
1036+
// test backlinkHTML
1037+
1038+
files = strings.ReplaceAll(files, "TBD", "backlinkHTML")
1039+
b = hugolib.Test(t, files)
1040+
b.AssertFileContent("public/p1/index.html",
1041+
"<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>",
1042+
)
10321043
}

0 commit comments

Comments
 (0)