mirror of
https://github.com/russross/blackfriday.git
synced 2024-03-22 13:40:34 +08:00
Remove a couple calls to Truncate() from parser
Link parser used to truncate in two cases: when parsing image links and inline footnotes. In order to avoid this truncation, introduce a separate callback for each of these cases and avoid writing extra characters instead of truncating them after the fact.
This commit is contained in:
parent
29f02f7d01
commit
ef087889f4
34
inline.go
34
inline.go
|
@ -198,6 +198,20 @@ func isReferenceStyleLink(data []byte, pos int, t linkType) bool {
|
||||||
return pos < len(data)-1 && data[pos] == '[' && data[pos+1] != '^'
|
return pos < len(data)-1 && data[pos] == '[' && data[pos+1] != '^'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func maybeImage(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
|
if offset < len(data)-1 && data[offset+1] == '[' {
|
||||||
|
return link(p, out, data, offset)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func maybeInlineFootnote(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
|
if offset < len(data)-1 && data[offset+1] == '[' {
|
||||||
|
return link(p, out, data, offset)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
// '[': parse a link or an image or a footnote
|
// '[': parse a link or an image or a footnote
|
||||||
func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
// no links allowed inside regular links, footnote, and deferred footnotes
|
// no links allowed inside regular links, footnote, and deferred footnotes
|
||||||
|
@ -212,13 +226,15 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
case p.flags&Footnotes != 0 && len(data)-1 > offset && data[offset+1] == '^':
|
case p.flags&Footnotes != 0 && len(data)-1 > offset && data[offset+1] == '^':
|
||||||
t = linkDeferredFootnote
|
t = linkDeferredFootnote
|
||||||
// ![alt] == image
|
// ![alt] == image
|
||||||
case offset > 0 && data[offset-1] == '!':
|
case offset >= 0 && data[offset] == '!':
|
||||||
t = linkImg
|
t = linkImg
|
||||||
|
offset += 1
|
||||||
// ^[text] == inline footnote
|
// ^[text] == inline footnote
|
||||||
// [^refId] == deferred footnote
|
// [^refId] == deferred footnote
|
||||||
case p.flags&Footnotes != 0:
|
case p.flags&Footnotes != 0:
|
||||||
if offset > 0 && data[offset-1] == '^' {
|
if offset >= 0 && data[offset] == '^' {
|
||||||
t = linkInlineFootnote
|
t = linkInlineFootnote
|
||||||
|
offset += 1
|
||||||
} else if len(data)-1 > offset && data[offset+1] == '^' {
|
} else if len(data)-1 > offset && data[offset+1] == '^' {
|
||||||
t = linkDeferredFootnote
|
t = linkDeferredFootnote
|
||||||
}
|
}
|
||||||
|
@ -533,22 +549,12 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
case linkImg:
|
case linkImg:
|
||||||
outSize := out.Len()
|
|
||||||
outBytes := out.Bytes()
|
|
||||||
if outSize > 0 && outBytes[outSize-1] == '!' {
|
|
||||||
out.Truncate(outSize - 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
p.r.Image(out, uLink, title, content.Bytes())
|
p.r.Image(out, uLink, title, content.Bytes())
|
||||||
|
i += 1
|
||||||
|
|
||||||
case linkInlineFootnote:
|
case linkInlineFootnote:
|
||||||
outSize := out.Len()
|
|
||||||
outBytes := out.Bytes()
|
|
||||||
if outSize > 0 && outBytes[outSize-1] == '^' {
|
|
||||||
out.Truncate(outSize - 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
p.r.FootnoteRef(out, link, noteId)
|
p.r.FootnoteRef(out, link, noteId)
|
||||||
|
i += 1
|
||||||
|
|
||||||
case linkDeferredFootnote:
|
case linkDeferredFootnote:
|
||||||
p.r.FootnoteRef(out, link, noteId)
|
p.r.FootnoteRef(out, link, noteId)
|
||||||
|
|
|
@ -374,6 +374,8 @@ func MarkdownOptions(input []byte, renderer Renderer, opts Options) []byte {
|
||||||
p.inlineCallback['<'] = leftAngle
|
p.inlineCallback['<'] = leftAngle
|
||||||
p.inlineCallback['\\'] = escape
|
p.inlineCallback['\\'] = escape
|
||||||
p.inlineCallback['&'] = entity
|
p.inlineCallback['&'] = entity
|
||||||
|
p.inlineCallback['!'] = maybeImage
|
||||||
|
p.inlineCallback['^'] = maybeInlineFootnote
|
||||||
|
|
||||||
if extensions&Autolink != 0 {
|
if extensions&Autolink != 0 {
|
||||||
p.inlineCallback[':'] = autoLink
|
p.inlineCallback[':'] = autoLink
|
||||||
|
|
Loading…
Reference in New Issue
Block a user