mirror of
https://github.com/russross/blackfriday.git
synced 2024-03-22 13:40:34 +08:00
Move reference extraction to paragraph parser
Move reference and footnote extraction code from firstPass to a paragraph block parser. This makes firstPass a little bit slimmer, ergo closer to elimination.
This commit is contained in:
parent
efa77da18b
commit
ea8dfc4880
13
block.go
13
block.go
|
@ -1380,7 +1380,10 @@ func (p *parser) paragraph(data []byte) int {
|
|||
// line: index of 1st char of current line
|
||||
// i: index of cursor/end of current line
|
||||
var prev, line, i int
|
||||
|
||||
tabSize := TabSizeDefault
|
||||
if p.flags&TabSizeEight != 0 {
|
||||
tabSize = TabSizeDouble
|
||||
}
|
||||
// keep going until we find something to mark the end of the paragraph
|
||||
for i < len(data) {
|
||||
// mark the beginning of the current line
|
||||
|
@ -1388,6 +1391,14 @@ func (p *parser) paragraph(data []byte) int {
|
|||
current := data[i:]
|
||||
line = i
|
||||
|
||||
// did we find a reference or a footnote? If so, end a paragraph
|
||||
// preceding it and report that we have consumed up to the end of that
|
||||
// reference:
|
||||
if refEnd := isReference(p, current, tabSize); refEnd > 0 {
|
||||
p.renderParagraph(data[:i])
|
||||
return i + refEnd
|
||||
}
|
||||
|
||||
// did we find a blank line marking the end of the paragraph?
|
||||
if n := p.isEmpty(current); n > 0 {
|
||||
// did this blank line followed by a definition list item?
|
||||
|
|
10
markdown.go
10
markdown.go
|
@ -453,7 +453,6 @@ func (p *parser) parseRefsToAST() {
|
|||
|
||||
// first pass:
|
||||
// - normalize newlines
|
||||
// - extract references (outside of fenced code blocks)
|
||||
// - expand tabs (outside of fenced code blocks)
|
||||
// - copy everything else
|
||||
func firstPass(p *parser, input []byte) []byte {
|
||||
|
@ -485,9 +484,6 @@ func firstPass(p *parser, input []byte) []byte {
|
|||
if end > beg {
|
||||
if end < lastFencedCodeBlockEnd { // Do not expand tabs while inside fenced code blocks.
|
||||
out.Write(input[beg:end])
|
||||
} else if refEnd := isReference(p, input[beg:], tabSize); refEnd > 0 {
|
||||
beg += refEnd
|
||||
continue
|
||||
} else {
|
||||
expandTabs(&out, input[beg:end], tabSize)
|
||||
}
|
||||
|
@ -593,7 +589,11 @@ func isReference(p *parser, data []byte, tabSize int) int {
|
|||
return 0
|
||||
}
|
||||
idEnd := i
|
||||
|
||||
// footnotes can have empty ID, like this: [^], but a reference can not be
|
||||
// empty like this: []. Break early if it's not a footnote and there's no ID
|
||||
if noteID == 0 && idOffset == idEnd {
|
||||
return 0
|
||||
}
|
||||
// spacer: colon (space | tab)* newline? (space | tab)*
|
||||
i++
|
||||
if i >= len(data) || data[i] != ':' {
|
||||
|
|
Loading…
Reference in New Issue
Block a user