...

Source file src/github.com/yuin/goldmark/extension/footnote_test.go

Documentation: github.com/yuin/goldmark/extension

     1  package extension
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/yuin/goldmark"
     7  	gast "github.com/yuin/goldmark/ast"
     8  	"github.com/yuin/goldmark/parser"
     9  	"github.com/yuin/goldmark/renderer/html"
    10  	"github.com/yuin/goldmark/testutil"
    11  	"github.com/yuin/goldmark/text"
    12  	"github.com/yuin/goldmark/util"
    13  )
    14  
    15  func TestFootnote(t *testing.T) {
    16  	markdown := goldmark.New(
    17  		goldmark.WithRendererOptions(
    18  			html.WithUnsafe(),
    19  		),
    20  		goldmark.WithExtensions(
    21  			Footnote,
    22  		),
    23  	)
    24  	testutil.DoTestCaseFile(markdown, "_test/footnote.txt", t, testutil.ParseCliCaseArg()...)
    25  }
    26  
    27  type footnoteID struct {
    28  }
    29  
    30  func (a *footnoteID) Transform(node *gast.Document, reader text.Reader, pc parser.Context) {
    31  	node.Meta()["footnote-prefix"] = "article12-"
    32  }
    33  
    34  func TestFootnoteOptions(t *testing.T) {
    35  	markdown := goldmark.New(
    36  		goldmark.WithRendererOptions(
    37  			html.WithUnsafe(),
    38  		),
    39  		goldmark.WithExtensions(
    40  			NewFootnote(
    41  				WithFootnoteIDPrefix([]byte("article12-")),
    42  				WithFootnoteLinkClass([]byte("link-class")),
    43  				WithFootnoteBacklinkClass([]byte("backlink-class")),
    44  				WithFootnoteLinkTitle([]byte("link-title-%%-^^")),
    45  				WithFootnoteBacklinkTitle([]byte("backlink-title")),
    46  				WithFootnoteBacklinkHTML([]byte("^")),
    47  			),
    48  		),
    49  	)
    50  
    51  	testutil.DoTestCase(
    52  		markdown,
    53  		testutil.MarkdownTestCase{
    54  			No:          1,
    55  			Description: "Footnote with options",
    56  			Markdown: `That's some text with a footnote.[^1]
    57  
    58  Same footnote.[^1]
    59  
    60  Another one.[^2]
    61  
    62  [^1]: And that's the footnote.
    63  [^2]: Another footnote.
    64  `,
    65  			Expected: `<p>That's some text with a footnote.<sup id="article12-fnref:1"><a href="#article12-fn:1" class="link-class" title="link-title-2-1" role="doc-noteref">1</a></sup></p>
    66  <p>Same footnote.<sup id="article12-fnref1:1"><a href="#article12-fn:1" class="link-class" title="link-title-2-1" role="doc-noteref">1</a></sup></p>
    67  <p>Another one.<sup id="article12-fnref:2"><a href="#article12-fn:2" class="link-class" title="link-title-1-2" role="doc-noteref">2</a></sup></p>
    68  <div class="footnotes" role="doc-endnotes">
    69  <hr>
    70  <ol>
    71  <li id="article12-fn:1">
    72  <p>And that's the footnote.&#160;<a href="#article12-fnref:1" class="backlink-class" title="backlink-title" role="doc-backlink">^</a>&#160;<a href="#article12-fnref1:1" class="backlink-class" title="backlink-title" role="doc-backlink">^</a></p>
    73  </li>
    74  <li id="article12-fn:2">
    75  <p>Another footnote.&#160;<a href="#article12-fnref:2" class="backlink-class" title="backlink-title" role="doc-backlink">^</a></p>
    76  </li>
    77  </ol>
    78  </div>`,
    79  		},
    80  		t,
    81  	)
    82  
    83  	markdown = goldmark.New(
    84  		goldmark.WithParserOptions(
    85  			parser.WithASTTransformers(
    86  				util.Prioritized(&footnoteID{}, 100),
    87  			),
    88  		),
    89  		goldmark.WithRendererOptions(
    90  			html.WithUnsafe(),
    91  		),
    92  		goldmark.WithExtensions(
    93  			NewFootnote(
    94  				WithFootnoteIDPrefixFunction(func(n gast.Node) []byte {
    95  					v, ok := n.OwnerDocument().Meta()["footnote-prefix"]
    96  					if ok {
    97  						return util.StringToReadOnlyBytes(v.(string))
    98  					}
    99  					return nil
   100  				}),
   101  				WithFootnoteLinkClass([]byte("link-class")),
   102  				WithFootnoteBacklinkClass([]byte("backlink-class")),
   103  				WithFootnoteLinkTitle([]byte("link-title-%%-^^")),
   104  				WithFootnoteBacklinkTitle([]byte("backlink-title")),
   105  				WithFootnoteBacklinkHTML([]byte("^")),
   106  			),
   107  		),
   108  	)
   109  
   110  	testutil.DoTestCase(
   111  		markdown,
   112  		testutil.MarkdownTestCase{
   113  			No:          2,
   114  			Description: "Footnote with an id prefix function",
   115  			Markdown: `That's some text with a footnote.[^1]
   116  
   117  Same footnote.[^1]
   118  
   119  Another one.[^2]
   120  
   121  [^1]: And that's the footnote.
   122  [^2]: Another footnote.
   123  `,
   124  			Expected: `<p>That's some text with a footnote.<sup id="article12-fnref:1"><a href="#article12-fn:1" class="link-class" title="link-title-2-1" role="doc-noteref">1</a></sup></p>
   125  <p>Same footnote.<sup id="article12-fnref1:1"><a href="#article12-fn:1" class="link-class" title="link-title-2-1" role="doc-noteref">1</a></sup></p>
   126  <p>Another one.<sup id="article12-fnref:2"><a href="#article12-fn:2" class="link-class" title="link-title-1-2" role="doc-noteref">2</a></sup></p>
   127  <div class="footnotes" role="doc-endnotes">
   128  <hr>
   129  <ol>
   130  <li id="article12-fn:1">
   131  <p>And that's the footnote.&#160;<a href="#article12-fnref:1" class="backlink-class" title="backlink-title" role="doc-backlink">^</a>&#160;<a href="#article12-fnref1:1" class="backlink-class" title="backlink-title" role="doc-backlink">^</a></p>
   132  </li>
   133  <li id="article12-fn:2">
   134  <p>Another footnote.&#160;<a href="#article12-fnref:2" class="backlink-class" title="backlink-title" role="doc-backlink">^</a></p>
   135  </li>
   136  </ol>
   137  </div>`,
   138  		},
   139  		t,
   140  	)
   141  }
   142  

View as plain text