From: Joe Mooring Date: Sun, 28 Sep 2025 16:45:52 +0000 (-0700) Subject: markup/goldmark: Enhance footnote extension with backlinkHTML option X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=b462980ac2ffc147a7952d1eaef6cdc700738108;p=brevno-suite%2Fhugo 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 --- diff --git a/docs/data/docs.yaml b/docs/data/docs.yaml index e359ff6c9..9cd80c4f7 100644 --- a/docs/data/docs.yaml +++ b/docs/data/docs.yaml @@ -1239,6 +1239,7 @@ config: superscript: enable: false footnote: + backlinkHTML: '↩︎' enable: true enableAutoIDPrefix: false linkify: true diff --git a/markup/goldmark/convert.go b/markup/goldmark/convert.go index 7b23c8986..f4519dff4 100644 --- a/markup/goldmark/convert.go +++ b/markup/goldmark/convert.go @@ -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 { diff --git a/markup/goldmark/goldmark_config/config.go b/markup/goldmark/goldmark_config/config.go index fe808f5d4..595222cdc 100644 --- a/markup/goldmark/goldmark_config/config.go +++ b/markup/goldmark/goldmark_config/config.go @@ -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. diff --git a/markup/goldmark/goldmark_integration_test.go b/markup/goldmark/goldmark_integration_test.go index 9a8616829..a6eac4e48 100644 --- a/markup/goldmark/goldmark_integration_test.go +++ b/markup/goldmark/goldmark_integration_test.go @@ -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 := "

foo[^1] and bar[^2]

\n

[^1]: footnote one\n[^2]: footnote two

" 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", "

foo1 and bar2

\n
\n
\n
    \n
  1. \n

    footnote one ↩︎

    \n
  2. \n
  3. \n

    footnote two ↩︎

    \n
  4. \n
\n
", ) + + // test backlinkHTML + + files = strings.ReplaceAll(files, "TBD", "backlinkHTML") + b = hugolib.Test(t, files) + b.AssertFileContent("public/p1/index.html", + "

foo1 and bar2

\n
\n
\n
    \n
  1. \n

    footnote one back

    \n
  2. \n
  3. \n

    footnote two back

    \n
  4. \n
\n
", + ) }