Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix duplicate and recursive footnotes. (#241) #366

Merged
merged 3 commits into from
Jun 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
}

p.notes = append(p.notes, ref)
p.notesRecord[string(ref.link)] = struct{}{}

link = ref.link
title = ref.title
Expand All @@ -498,9 +499,10 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
return 0
}

if t == linkDeferredFootnote {
if t == linkDeferredFootnote && !p.isFootnote(lr) {
lr.noteId = len(p.notes) + 1
p.notes = append(p.notes, lr)
p.notesRecord[string(lr.link)] = struct{}{}
}

// keep link and title from reference
Expand Down
50 changes: 50 additions & 0 deletions inline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,28 @@ what happens here
</li>
</ol>
</div>
`,
`testing footnotes.[^a]

test footnotes the second.[^b]

[^a]: This is the first note[^a].
[^b]: this is the second note.[^a]
`,
`<p>testing footnotes.<sup class="footnote-ref" id="fnref:a"><a rel="footnote" href="#fn:a">1</a></sup></p>

<p>test footnotes the second.<sup class="footnote-ref" id="fnref:b"><a rel="footnote" href="#fn:b">2</a></sup></p>
<div class="footnotes">

<hr />

<ol>
<li id="fn:a">This is the first note<sup class="footnote-ref" id="fnref:a"><a rel="footnote" href="#fn:a">1</a></sup>.
</li>
<li id="fn:b">this is the second note.<sup class="footnote-ref" id="fnref:a"><a rel="footnote" href="#fn:a">1</a></sup>
</li>
</ol>
</div>
`,
}

Expand Down Expand Up @@ -1076,6 +1098,34 @@ func TestNestedFootnotes(t *testing.T) {
</li>
</ol>
</div>
`,
`This uses footnote A.[^A]

This uses footnote C.[^C]

[^A]:
A note. use itself.[^A]
[^B]:
B note, uses A to test duplicate.[^A]
[^C]:
C note, uses B.[^B]
`,
`<p>This uses footnote A.<sup class="footnote-ref" id="fnref:A"><a rel="footnote" href="#fn:A">1</a></sup></p>

<p>This uses footnote C.<sup class="footnote-ref" id="fnref:C"><a rel="footnote" href="#fn:C">2</a></sup></p>
<div class="footnotes">

<hr />

<ol>
<li id="fn:A">A note. use itself.<sup class="footnote-ref" id="fnref:A"><a rel="footnote" href="#fn:A">1</a></sup>
</li>
<li id="fn:C">C note, uses B.<sup class="footnote-ref" id="fnref:B"><a rel="footnote" href="#fn:B">3</a></sup>
</li>
<li id="fn:B">B note, uses A to test duplicate.<sup class="footnote-ref" id="fnref:A"><a rel="footnote" href="#fn:A">1</a></sup>
</li>
</ol>
</div>
`,
}
doTestsInlineParam(t, tests, Options{Extensions: EXTENSION_FOOTNOTES}, 0,
Expand Down
9 changes: 8 additions & 1 deletion markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ type parser struct {
// Footnotes need to be ordered as well as available to quickly check for
// presence. If a ref is also a footnote, it's stored both in refs and here
// in notes. Slice is nil if footnotes not enabled.
notes []*reference
notes []*reference
notesRecord map[string]struct{}
}

func (p *parser) getRef(refid string) (ref *reference, found bool) {
Expand All @@ -241,6 +242,11 @@ func (p *parser) getRef(refid string) (ref *reference, found bool) {
return ref, found
}

func (p *parser) isFootnote(ref *reference) bool {
_, ok := p.notesRecord[string(ref.link)]
return ok
}

//
//
// Public interface
Expand Down Expand Up @@ -376,6 +382,7 @@ func MarkdownOptions(input []byte, renderer Renderer, opts Options) []byte {

if extensions&EXTENSION_FOOTNOTES != 0 {
p.notes = make([]*reference, 0)
p.notesRecord = make(map[string]struct{})
}

first := firstPass(p, input)
Expand Down