This commit is contained in:
yuin 2019-11-29 17:03:00 +09:00
parent b611cd333a
commit 54fc7c3f18
3 changed files with 54 additions and 1 deletions

View file

@ -12,7 +12,7 @@ That's some text with a footnote.[^1]
<ol> <ol>
<li id="fn:1" role="doc-endnote"> <li id="fn:1" role="doc-endnote">
<p>And that's the footnote.</p> <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> </li>
</ol> </ol>
</section> </section>

View file

@ -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 // A Footnote struct represents a footnote of Markdown
// (PHP Markdown Extra) text. // (PHP Markdown Extra) text.
type Footnote struct { type Footnote struct {

View file

@ -180,6 +180,16 @@ func (a *footnoteASTTransformer) Transform(node *gast.Document, reader text.Read
return return
} }
pc.Set(footnoteListKey, nil) 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) node.AppendChild(node, list)
} }
@ -203,6 +213,7 @@ func NewFootnoteHTMLRenderer(opts ...html.Option) renderer.NodeRenderer {
// RegisterFuncs implements renderer.NodeRenderer.RegisterFuncs. // RegisterFuncs implements renderer.NodeRenderer.RegisterFuncs.
func (r *FootnoteHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { func (r *FootnoteHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(ast.KindFootnoteLink, r.renderFootnoteLink) reg.Register(ast.KindFootnoteLink, r.renderFootnoteLink)
reg.Register(ast.KindFootnoteBackLink, r.renderFootnoteBackLink)
reg.Register(ast.KindFootnote, r.renderFootnote) reg.Register(ast.KindFootnote, r.renderFootnote)
reg.Register(ast.KindFootnoteList, r.renderFootnoteList) reg.Register(ast.KindFootnoteList, r.renderFootnoteList)
} }
@ -222,6 +233,19 @@ func (r *FootnoteHTMLRenderer) renderFootnoteLink(w util.BufWriter, source []byt
return gast.WalkContinue, nil 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) { func (r *FootnoteHTMLRenderer) renderFootnote(w util.BufWriter, source []byte, node gast.Node, entering bool) (gast.WalkStatus, error) {
n := node.(*ast.Footnote) n := node.(*ast.Footnote)
is := strconv.Itoa(n.Index) is := strconv.Itoa(n.Index)