mirror of
https://github.com/yuin/goldmark
synced 2025-03-04 23:04:52 +00:00
Closes #40
This commit is contained in:
parent
b611cd333a
commit
54fc7c3f18
3 changed files with 54 additions and 1 deletions
|
|
@ -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">↩</a></p>
|
||||||
</li>
|
</li>
|
||||||
</ol>
|
</ol>
|
||||||
</section>
|
</section>
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
|
|
||||||
|
|
@ -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("↩")
|
||||||
|
_, _ = 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)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue