Skip to content

Commit

Permalink
Closes #40
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed Nov 29, 2019
1 parent b611cd3 commit 54fc7c3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion extension/_test/footnote.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ That's some text with a footnote.[^1]
<ol>
<li id="fn:1" role="doc-endnote">
<p>And that's the footnote.</p>
<p>That's the second paragraph.</p>
<p>That's the second paragraph.<a href="#fnref:1" class="footnote-backref" role="doc-backlink">&#8617;</a></p>
</li>
</ol>
</section>
Expand Down
29 changes: 29 additions & 0 deletions extension/ast/footnote.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,35 @@ func NewFootnoteLink(index int) *FootnoteLink {
}
}

// A FootnoteBackLink struct represents a link to a footnote of Markdown
// (PHP Markdown Extra) text.
type FootnoteBackLink struct {
gast.BaseInline
Index int
}

// Dump implements Node.Dump.
func (n *FootnoteBackLink) Dump(source []byte, level int) {
m := map[string]string{}
m["Index"] = fmt.Sprintf("%v", n.Index)
gast.DumpHelper(n, source, level, m, nil)
}

// KindFootnoteBackLink is a NodeKind of the FootnoteBackLink node.
var KindFootnoteBackLink = gast.NewNodeKind("FootnoteBackLink")

// Kind implements Node.Kind.
func (n *FootnoteBackLink) Kind() gast.NodeKind {
return KindFootnoteBackLink
}

// NewFootnoteBackLink returns a new FootnoteBackLink node.
func NewFootnoteBackLink(index int) *FootnoteBackLink {
return &FootnoteBackLink{
Index: index,
}
}

// A Footnote struct represents a footnote of Markdown
// (PHP Markdown Extra) text.
type Footnote struct {
Expand Down
24 changes: 24 additions & 0 deletions extension/footnote.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ func (a *footnoteASTTransformer) Transform(node *gast.Document, reader text.Read
return
}
pc.Set(footnoteListKey, nil)

for footnote := list.FirstChild(); footnote != nil; footnote = footnote.NextSibling() {
var container gast.Node = footnote
if fc := container.LastChild(); fc != nil && gast.IsParagraph(fc) {
container = fc
}
index := footnote.(*ast.Footnote).Index
container.AppendChild(container, ast.NewFootnoteBackLink(index))
}

node.AppendChild(node, list)
}

Expand All @@ -203,6 +213,7 @@ func NewFootnoteHTMLRenderer(opts ...html.Option) renderer.NodeRenderer {
// RegisterFuncs implements renderer.NodeRenderer.RegisterFuncs.
func (r *FootnoteHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(ast.KindFootnoteLink, r.renderFootnoteLink)
reg.Register(ast.KindFootnoteBackLink, r.renderFootnoteBackLink)
reg.Register(ast.KindFootnote, r.renderFootnote)
reg.Register(ast.KindFootnoteList, r.renderFootnoteList)
}
Expand All @@ -222,6 +233,19 @@ func (r *FootnoteHTMLRenderer) renderFootnoteLink(w util.BufWriter, source []byt
return gast.WalkContinue, nil
}

func (r *FootnoteHTMLRenderer) renderFootnoteBackLink(w util.BufWriter, source []byte, node gast.Node, entering bool) (gast.WalkStatus, error) {
if entering {
n := node.(*ast.FootnoteBackLink)
is := strconv.Itoa(n.Index)
_, _ = w.WriteString(`<a href="#fnref:`)
_, _ = w.WriteString(is)
_, _ = w.WriteString(`" class="footnote-backref" role="doc-backlink">`)
_, _ = w.WriteString("&#8617;")
_, _ = w.WriteString(`</a>`)
}
return gast.WalkContinue, nil
}

func (r *FootnoteHTMLRenderer) renderFootnote(w util.BufWriter, source []byte, node gast.Node, entering bool) (gast.WalkStatus, error) {
n := node.(*ast.Footnote)
is := strconv.Itoa(n.Index)
Expand Down

0 comments on commit 54fc7c3

Please sign in to comment.