mirror of
https://github.com/yuin/goldmark
synced 2025-03-04 23:04:52 +00:00
Adding ability to use labeled footnotes.
This commit is contained in:
parent
faaafa55b6
commit
04acf85d56
2 changed files with 20 additions and 9 deletions
|
|
@ -2,6 +2,7 @@ package ast
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
gast "github.com/yuin/goldmark/ast"
|
||||
)
|
||||
|
||||
|
|
@ -9,6 +10,7 @@ import (
|
|||
// (PHP Markdown Extra) text.
|
||||
type FootnoteLink struct {
|
||||
gast.BaseInline
|
||||
Ref []byte
|
||||
Index int
|
||||
}
|
||||
|
||||
|
|
@ -28,9 +30,10 @@ func (n *FootnoteLink) Kind() gast.NodeKind {
|
|||
}
|
||||
|
||||
// NewFootnoteLink returns a new FootnoteLink node.
|
||||
func NewFootnoteLink(index int) *FootnoteLink {
|
||||
func NewFootnoteLink(index int, ref []byte) *FootnoteLink {
|
||||
return &FootnoteLink{
|
||||
Index: index,
|
||||
Ref: ref,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -38,6 +41,7 @@ func NewFootnoteLink(index int) *FootnoteLink {
|
|||
// (PHP Markdown Extra) text.
|
||||
type FootnoteBackLink struct {
|
||||
gast.BaseInline
|
||||
Ref []byte
|
||||
Index int
|
||||
}
|
||||
|
||||
|
|
@ -57,9 +61,10 @@ func (n *FootnoteBackLink) Kind() gast.NodeKind {
|
|||
}
|
||||
|
||||
// NewFootnoteBackLink returns a new FootnoteBackLink node.
|
||||
func NewFootnoteBackLink(index int) *FootnoteBackLink {
|
||||
func NewFootnoteBackLink(index int, ref []byte) *FootnoteBackLink {
|
||||
return &FootnoteBackLink{
|
||||
Index: index,
|
||||
Ref: ref,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ package extension
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"strconv"
|
||||
|
||||
"github.com/yuin/goldmark"
|
||||
gast "github.com/yuin/goldmark/ast"
|
||||
"github.com/yuin/goldmark/extension/ast"
|
||||
|
|
@ -10,7 +12,6 @@ import (
|
|||
"github.com/yuin/goldmark/renderer/html"
|
||||
"github.com/yuin/goldmark/text"
|
||||
"github.com/yuin/goldmark/util"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var footnoteListKey = parser.NewContextKey()
|
||||
|
|
@ -149,8 +150,10 @@ func (s *footnoteParser) Parse(parent gast.Node, block text.Reader, pc parser.Co
|
|||
return nil
|
||||
}
|
||||
index := 0
|
||||
var ref []byte
|
||||
for def := list.FirstChild(); def != nil; def = def.NextSibling() {
|
||||
d := def.(*ast.Footnote)
|
||||
ref = d.Ref
|
||||
if bytes.Equal(d.Ref, value) {
|
||||
if d.Index < 0 {
|
||||
list.Count += 1
|
||||
|
|
@ -164,7 +167,7 @@ func (s *footnoteParser) Parse(parent gast.Node, block text.Reader, pc parser.Co
|
|||
return nil
|
||||
}
|
||||
|
||||
return ast.NewFootnoteLink(index)
|
||||
return ast.NewFootnoteLink(index, ref)
|
||||
}
|
||||
|
||||
type footnoteASTTransformer struct {
|
||||
|
|
@ -193,10 +196,11 @@ func (a *footnoteASTTransformer) Transform(node *gast.Document, reader text.Read
|
|||
container = fc
|
||||
}
|
||||
index := footnote.(*ast.Footnote).Index
|
||||
ref := footnote.(*ast.Footnote).Ref
|
||||
if index < 0 {
|
||||
list.RemoveChild(list, footnote)
|
||||
} else {
|
||||
container.AppendChild(container, ast.NewFootnoteBackLink(index))
|
||||
container.AppendChild(container, ast.NewFootnoteBackLink(index, ref))
|
||||
}
|
||||
footnote = next
|
||||
}
|
||||
|
|
@ -243,10 +247,11 @@ func (r *FootnoteHTMLRenderer) renderFootnoteLink(w util.BufWriter, source []byt
|
|||
if entering {
|
||||
n := node.(*ast.FootnoteLink)
|
||||
is := strconv.Itoa(n.Index)
|
||||
ref := string(n.Ref)
|
||||
_, _ = w.WriteString(`<sup id="fnref:`)
|
||||
_, _ = w.WriteString(is)
|
||||
_, _ = w.WriteString(ref)
|
||||
_, _ = w.WriteString(`"><a href="#fn:`)
|
||||
_, _ = w.WriteString(is)
|
||||
_, _ = w.WriteString(ref)
|
||||
_, _ = w.WriteString(`" class="footnote-ref" role="doc-noteref">`)
|
||||
_, _ = w.WriteString(is)
|
||||
_, _ = w.WriteString(`</a></sup>`)
|
||||
|
|
@ -257,9 +262,10 @@ func (r *FootnoteHTMLRenderer) renderFootnoteLink(w util.BufWriter, source []byt
|
|||
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)
|
||||
// is := strconv.Itoa(n.Index)
|
||||
ref := string(n.Ref)
|
||||
_, _ = w.WriteString(` <a href="#fnref:`)
|
||||
_, _ = w.WriteString(is)
|
||||
_, _ = w.WriteString(ref)
|
||||
_, _ = w.WriteString(`" class="footnote-backref" role="doc-backlink">`)
|
||||
_, _ = w.WriteString("↩︎")
|
||||
_, _ = w.WriteString(`</a>`)
|
||||
|
|
|
|||
Loading…
Reference in a new issue