all repos — grayfriday @ ef087889f49d63f4ada853a7b255072f85ec970f

blackfriday fork with a few changes

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.
Vytautas Ĺ altenis vytas@rtfb.lt
Mon, 26 Oct 2015 20:47:20 +0200
commit

ef087889f49d63f4ada853a7b255072f85ec970f

parent

29f02f7d01e2575258da122cb38ad3c15c544b4e

2 files changed, 22 insertions(+), 14 deletions(-)

jump to
M inline.goinline.go

@@ -198,6 +198,20 @@ }

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 func link(p *parser, out *bytes.Buffer, data []byte, offset int) int { // no links allowed inside regular links, footnote, and deferred footnotes

@@ -212,13 +226,15 @@ // an exclamation point)

case p.flags&Footnotes != 0 && len(data)-1 > offset && data[offset+1] == '^': t = linkDeferredFootnote // ![alt] == image - case offset > 0 && data[offset-1] == '!': + case offset >= 0 && data[offset] == '!': t = linkImg + offset += 1 // ^[text] == inline footnote // [^refId] == deferred footnote case p.flags&Footnotes != 0: - if offset > 0 && data[offset-1] == '^' { + if offset >= 0 && data[offset] == '^' { t = linkInlineFootnote + offset += 1 } else if len(data)-1 > offset && data[offset+1] == '^' { t = linkDeferredFootnote }

@@ -533,22 +549,12 @@ p.r.Link(out, uLink, title, content.Bytes())

} 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()) + i += 1 case linkInlineFootnote: - outSize := out.Len() - outBytes := out.Bytes() - if outSize > 0 && outBytes[outSize-1] == '^' { - out.Truncate(outSize - 1) - } - p.r.FootnoteRef(out, link, noteId) + i += 1 case linkDeferredFootnote: p.r.FootnoteRef(out, link, noteId)
M markdown.gomarkdown.go

@@ -374,6 +374,8 @@ p.inlineCallback['['] = link

p.inlineCallback['<'] = leftAngle p.inlineCallback['\\'] = escape p.inlineCallback['&'] = entity + p.inlineCallback['!'] = maybeImage + p.inlineCallback['^'] = maybeInlineFootnote if extensions&Autolink != 0 { p.inlineCallback[':'] = autoLink