Refix fenced code blocks w/o preceding blank lines

Change approach at fixing #45: don't patch input markdown at preprocess
pass, instead improve special case detection when parsing paragraphs.

Leave the fenced code block detection in the preprocess pass though,
it's been put to another use since then, to suppress tab expansion
inside code blocks.
pull/214/head
Vytautas Šaltenis 2015-10-28 21:37:14 +02:00
parent d4ee3ea08b
commit 133788657b
2 changed files with 10 additions and 6 deletions

View File

@ -1342,6 +1342,14 @@ func (p *parser) paragraph(out *bytes.Buffer, data []byte) int {
return i
}
// if there's a fenced code block, paragraph is over
if p.flags&EXTENSION_FENCED_CODE != 0 {
if p.fencedCode(out, current, false) > 0 {
p.renderParagraph(out, data[:i])
return i
}
}
// if there's a definition list item, prev line is a definition term
if p.flags&EXTENSION_DEFINITION_LISTS != 0 {
if p.dliPrefix(current) != 0 {

View File

@ -393,7 +393,6 @@ func firstPass(p *parser, input []byte) []byte {
tabSize = TAB_SIZE_EIGHT
}
beg, end := 0, 0
lastLineWasBlank := false
lastFencedCodeBlockEnd := 0
for beg < len(input) { // iterate over lines
if end = isReference(p, input[beg:], tabSize); end > 0 {
@ -405,16 +404,13 @@ func firstPass(p *parser, input []byte) []byte {
}
if p.flags&EXTENSION_FENCED_CODE != 0 {
// when last line was none blank and a fenced code block comes after
// track fenced code block boundaries to suppress tab expansion
// inside them:
if beg >= lastFencedCodeBlockEnd {
if i := p.fencedCode(&out, input[beg:], false); i > 0 {
if !lastLineWasBlank {
out.WriteByte('\n') // need to inject additional linebreak
}
lastFencedCodeBlockEnd = beg + i
}
}
lastLineWasBlank = end == beg
}
// add the line body if present