From ef2a2b02dc226565748b2546054149c1349a4dc2 Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Fri, 11 Apr 2014 19:54:55 -0700 Subject: [PATCH 1/2] Add failing test for an issue introduced by PR #56. The issue is that when there are more than 1 fenced code blocks with a blank line before and after, the parser introduces a single extra new line to all the fenced code blocks except the last one. --- block_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/block_test.go b/block_test.go index 6692ce1..c320edd 100644 --- a/block_test.go +++ b/block_test.go @@ -711,6 +711,12 @@ func TestFencedCodeBlock(t *testing.T) { "`", "

`

\n", + + "Bla bla\n\n``` oz\ncode blocks breakup paragraphs\n```\n\nBla Bla\n\n``` oz\nmultiple code blocks work okay\n```\n\nBla Bla\n", + "

Bla bla

\n\n
code blocks breakup paragraphs\n
\n\n

Bla Bla

\n\n
multiple code blocks work okay\n
\n\n

Bla Bla

\n", + + "Some text before a fenced code block\n``` oz\ncode blocks breakup paragraphs\n```\nSome text in between\n``` oz\nmultiple code blocks work okay\n```\nAnd some text after a fenced code block", + "

Some text before a fenced code block

\n\n
code blocks breakup paragraphs\n
\n\n

Some text in between

\n\n
multiple code blocks work okay\n
\n\n

And some text after a fenced code block

\n", } doTestsBlock(t, tests, EXTENSION_FENCED_CODE) } From 8df342acd510645e8bbc608a209e788edd2667d6 Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Fri, 11 Apr 2014 21:27:28 -0700 Subject: [PATCH 2/2] Fix bug where newlines were inserted inside fenced code blocks. Change firstPass() code that checks for fenced code blocks to check all of them and properly keep track of lastFencedCodeBlockEnd. This way, it won't misinterpret the end of a fenced code block as a beginning of a new one. --- markdown.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/markdown.go b/markdown.go index 3d211e1..33f36b2 100644 --- a/markdown.go +++ b/markdown.go @@ -330,9 +330,11 @@ 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 - if !lastLineWasBlank && beg >= lastFencedCodeBlockEnd { + if beg >= lastFencedCodeBlockEnd { if i := p.fencedCode(&out, append(input[beg:], '\n'), false); i > 0 { - out.WriteByte('\n') // need to inject additional linebreak + if !lastLineWasBlank { + out.WriteByte('\n') // need to inject additional linebreak + } lastFencedCodeBlockEnd = beg + i } }