mirror of
https://github.com/status-im/markdown.git
synced 2025-02-22 16:18:22 +00:00
add 'notesRecord' to check footnote existence fast
It is necessary to use vector for 'notes' instead of map to keep footnotes ordered. But checking for presence in a vector is not efficient. So add a map variable 'notesRecord' to tackle this problem.
This commit is contained in:
parent
a20399916c
commit
8c89af6200
@ -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)] = true
|
||||
|
||||
link = ref.link
|
||||
title = ref.title
|
||||
@ -501,6 +502,7 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||
if t == linkDeferredFootnote && !p.isFootnote(lr) {
|
||||
lr.noteId = len(p.notes) + 1
|
||||
p.notes = append(p.notes, lr)
|
||||
p.notesRecord[string(lr.link)] = true
|
||||
}
|
||||
|
||||
// keep link and title from reference
|
||||
|
13
markdown.go
13
markdown.go
@ -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]bool
|
||||
}
|
||||
|
||||
func (p *parser) getRef(refid string) (ref *reference, found bool) {
|
||||
@ -242,13 +243,8 @@ func (p *parser) getRef(refid string) (ref *reference, found bool) {
|
||||
}
|
||||
|
||||
func (p *parser) isFootnote(ref *reference) bool {
|
||||
for _, v := range p.notes {
|
||||
if string(ref.link) == string(v.link) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
_, ok := p.notesRecord[string(ref.link)]
|
||||
return ok
|
||||
}
|
||||
|
||||
//
|
||||
@ -386,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]bool)
|
||||
}
|
||||
|
||||
first := firstPass(p, input)
|
||||
|
Loading…
x
Reference in New Issue
Block a user