]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
markup/goldmark: Enhance footnote extension with backlinkHTML option
authorJoe Mooring <joe.mooring@veriphor.com>
Sun, 28 Sep 2025 16:45:52 +0000 (09:45 -0700)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 29 Sep 2025 10:21:17 +0000 (12:21 +0200)
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

docs/data/docs.yaml
markup/goldmark/convert.go
markup/goldmark/goldmark_config/config.go
markup/goldmark/goldmark_integration_test.go

index e359ff6c98a7bd01b4d5033e92dcc626dd69bec3..9cd80c4f7afe6d9f1ee570fc799e42dfadd86343 100644 (file)
@@ -1239,6 +1239,7 @@ config:
           superscript:
             enable: false
         footnote:
+          backlinkHTML: '&#x21a9;&#xfe0e;'
           enable: true
           enableAutoIDPrefix: false
         linkify: true
index 7b23c89865b5a4045bf46d803f28d552e84794ff..f4519dff45f6f3f65b7b8d1d853763d73ffdb3c1 100644 (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 {
index fe808f5d4cab2c79a88656a08a49352ed87d3b34..595222cdc53899961cbfeddd12eff1b198bdb3ac 100644 (file)
@@ -43,6 +43,7 @@ var Default = Config{
                Footnote: Footnote{
                        Enable:             true,
                        EnableAutoIDPrefix: false,
+                       BacklinkHTML:       "&#x21a9;&#xfe0e;",
                },
                DefinitionList:  true,
                Table:           true,
@@ -173,6 +174,7 @@ type Extensions struct {
 type Footnote struct {
        Enable             bool
        EnableAutoIDPrefix bool
+       BacklinkHTML       string
 }
 
 // Typographer holds typographer configuration.
index 9a861682997f89858e7886111e2a2446ea7071dd..a6eac4e483239a0aef41897827087171063ef508 100644 (file)
@@ -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>",
+       )
 }