package extension import ( "testing" "github.com/yuin/goldmark" gast "github.com/yuin/goldmark/ast" "github.com/yuin/goldmark/parser" "github.com/yuin/goldmark/renderer/html" "github.com/yuin/goldmark/testutil" "github.com/yuin/goldmark/text" "github.com/yuin/goldmark/util" ) func TestFootnote(t *testing.T) { markdown := goldmark.New( goldmark.WithRendererOptions( html.WithUnsafe(), ), goldmark.WithExtensions( Footnote, ), ) testutil.DoTestCaseFile(markdown, "_test/footnote.txt", t, testutil.ParseCliCaseArg()...) } type footnoteID struct { } func (a *footnoteID) Transform(node *gast.Document, reader text.Reader, pc parser.Context) { node.Meta()["footnote-prefix"] = "article12-" } func TestFootnoteOptions(t *testing.T) { markdown := goldmark.New( goldmark.WithRendererOptions( html.WithUnsafe(), ), goldmark.WithExtensions( NewFootnote( WithFootnoteIDPrefix([]byte("article12-")), WithFootnoteLinkClass([]byte("link-class")), WithFootnoteBacklinkClass([]byte("backlink-class")), WithFootnoteLinkTitle([]byte("link-title-%%-^^")), WithFootnoteBacklinkTitle([]byte("backlink-title")), WithFootnoteBacklinkHTML([]byte("^")), ), ), ) testutil.DoTestCase( markdown, testutil.MarkdownTestCase{ No: 1, Description: "Footnote with options", Markdown: `That's some text with a footnote.[^1] Same footnote.[^1] Another one.[^2] [^1]: And that's the footnote. [^2]: Another footnote. `, Expected: `

That's some text with a footnote.1

Same footnote.1

Another one.2


  1. And that's the footnote. ^ ^

  2. Another footnote. ^

`, }, t, ) markdown = goldmark.New( goldmark.WithParserOptions( parser.WithASTTransformers( util.Prioritized(&footnoteID{}, 100), ), ), goldmark.WithRendererOptions( html.WithUnsafe(), ), goldmark.WithExtensions( NewFootnote( WithFootnoteIDPrefixFunction(func(n gast.Node) []byte { v, ok := n.OwnerDocument().Meta()["footnote-prefix"] if ok { return util.StringToReadOnlyBytes(v.(string)) } return nil }), WithFootnoteLinkClass([]byte("link-class")), WithFootnoteBacklinkClass([]byte("backlink-class")), WithFootnoteLinkTitle([]byte("link-title-%%-^^")), WithFootnoteBacklinkTitle([]byte("backlink-title")), WithFootnoteBacklinkHTML([]byte("^")), ), ), ) testutil.DoTestCase( markdown, testutil.MarkdownTestCase{ No: 2, Description: "Footnote with an id prefix function", Markdown: `That's some text with a footnote.[^1] Same footnote.[^1] Another one.[^2] [^1]: And that's the footnote. [^2]: Another footnote. `, Expected: `

That's some text with a footnote.1

Same footnote.1

Another one.2


  1. And that's the footnote. ^ ^

  2. Another footnote. ^

`, }, t, ) }