fix duplicate and recursive footnotes. (#241)

Fix the infinite loop when there is a self-refer footnote by checking if
the reference is already added into parser.notes.

It also fixes of creating duplicate footnotes through this modification.

Add coresponding testcase.
pull/366/head
choueric 2017-06-08 14:43:56 +08:00
parent 0ba0f2b6ed
commit a20399916c
3 changed files with 61 additions and 1 deletions

View File

@ -498,7 +498,7 @@ 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)
}

View File

@ -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>
`,
}
@ -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,

View File

@ -241,6 +241,16 @@ func (p *parser) getRef(refid string) (ref *reference, found bool) {
return ref, found
}
func (p *parser) isFootnote(ref *reference) bool {
for _, v := range p.notes {
if string(ref.link) == string(v.link) {
return true
}
}
return false
}
//
//
// Public interface