Merge pull request #306 from russross/v2-add-links-to-footnotes V2 add links to footnotes
Vytautas Ĺ altenis vytas@rtfb.lt
Mon, 03 Oct 2016 08:14:53 +0300
3 files changed,
19 insertions(+),
9 deletions(-)
M
inline.go
→
inline.go
@@ -297,6 +297,7 @@ }
txtE := i i++ + var footnoteNode *Node // skip any amount of whitespace or newline // (this is much more lax than original markdown syntax)@@ -476,6 +477,7 @@ id = data[1:txtE]
} } + footnoteNode = NewNode(Item) if t == linkInlineFootnote { // create a new reference noteID = len(p.notes) + 1@@ -497,6 +499,7 @@ noteID: noteID,
hasBlock: false, link: fragment, title: id, + footnote: footnoteNode, } p.notes = append(p.notes, ref)@@ -512,6 +515,7 @@ }
if t == linkDeferredFootnote { lr.noteID = len(p.notes) + 1 + lr.footnote = footnoteNode p.notes = append(p.notes, lr) }@@ -570,6 +574,7 @@ linkNode = NewNode(Link)
linkNode.Destination = link linkNode.Title = title linkNode.NoteID = noteID + linkNode.Footnote = footnoteNode if t == linkInlineFootnote { i++ }
M
markdown.go
→
markdown.go
@@ -214,14 +214,16 @@ p.tip = above
} func (p *parser) addChild(node NodeType, offset uint32) *Node { - for !p.tip.canContain(node) { + return p.addExistingChild(NewNode(node), offset) +} + +func (p *parser) addExistingChild(node *Node, offset uint32) *Node { + for !p.tip.canContain(node.Type) { p.finalize(p.tip) } - newNode := NewNode(node) - newNode.content = []byte{} - p.tip.AppendChild(newNode) - p.tip = newNode - return newNode + p.tip.AppendChild(node) + p.tip = node + return node } func (p *parser) closeUnmatchedBlocks() {@@ -419,7 +421,8 @@ // we need to process those late additions. Range form would only walk over
// the fixed initial set. for i := 0; i < len(p.notes); i++ { ref := p.notes[i] - block := p.addBlock(Item, nil) + p.addExistingChild(ref.footnote, 0) + block := ref.footnote block.ListFlags = flags | ListTypeOrdered block.RefLink = ref.link if ref.hasBlock {@@ -570,6 +573,7 @@ link []byte
title []byte noteID int // 0 if not a footnote ref hasBlock bool + footnote *Node // a link to the Item node within a list of footnotes text []byte // only gets populated by refOverride feature with Reference.Text }
M
node.go
→
node.go
@@ -84,6 +84,7 @@ type LinkData struct {
Destination []byte // Destination is what goes into a href Title []byte // Title is the tooltip thing that goes in a title attribute NoteID int // NoteID contains a serial number of a footnote, zero if it's not a footnote + Footnote *Node // If it's a footnote, this is a direct link to the footnote Node. Otherwise nil. } // CodeBlockData contains fields relevant to a CodeBlock node type.@@ -277,8 +278,8 @@ type NodeVisitor func(node *Node, entering bool) WalkStatus
// Walk is a convenience method that instantiates a walker and starts a // traversal of subtree rooted at n. -func (root *Node) Walk(visitor NodeVisitor) { - w := newNodeWalker(root) +func (n *Node) Walk(visitor NodeVisitor) { + w := newNodeWalker(n) for w.current != nil { status := visitor(w.current, w.entering) switch status {