From 0a92c4aa55d519a0e085f9d2cafa94ac105a27f6 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius Date: Thu, 24 Jan 2019 10:23:35 +0200 Subject: [PATCH] Fix fenced code in lists (#521) This attempts to fix #495 and #485. Note the test cases which were added at the bottom of the list. The first added test case was passing even before the changes, but the second was not. --- block.go | 24 +++++++++++++++--------- block_test.go | 23 +++++++++++++++++++---- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/block.go b/block.go index 45c21a6c..563cb290 100644 --- a/block.go +++ b/block.go @@ -649,14 +649,17 @@ func isFenceLine(data []byte, info *string, oldmarker string, newlineOptional bo } i = skipChar(data, i, ' ') - if i >= len(data) || data[i] != '\n' { - if newlineOptional && i == len(data) { + if i >= len(data) { + if newlineOptional { return i, marker } return 0, "" } + if data[i] == '\n' { + i++ // Take newline into account + } - return i + 1, marker // Take newline into account. + return i, marker } // fencedCodeBlock returns the end index if data contains a fenced code block at the beginning, @@ -1133,6 +1136,15 @@ func (p *parser) listItem(out *bytes.Buffer, data []byte, flags *int) int { i++ } + // process the following lines + containsBlankLine := false + sublist := 0 + codeBlockMarker := "" + if p.flags&EXTENSION_FENCED_CODE != 0 && i > line { + // determine if codeblock starts on the first line + _, codeBlockMarker = isFenceLine(data[line:i], nil, "", false) + } + // get working buffer var raw bytes.Buffer @@ -1140,11 +1152,6 @@ func (p *parser) listItem(out *bytes.Buffer, data []byte, flags *int) int { raw.Write(data[line:i]) line = i - // process the following lines - containsBlankLine := false - sublist := 0 - codeBlockMarker := "" - gatherlines: for line < len(data) { i++ @@ -1153,7 +1160,6 @@ gatherlines: for data[i-1] != '\n' { i++ } - // if it is an empty line, guess that it is part of this item // and move on to the next line if p.isEmpty(data[line:i]) > 0 { diff --git a/block_test.go b/block_test.go index 820fe890..f2d998df 100644 --- a/block_test.go +++ b/block_test.go @@ -697,8 +697,8 @@ func TestUnorderedList(t *testing.T) { "* List\n extra indent, same paragraph\n", "\n", - "* List\n\n code block\n", - "\n", + "* List\n\n code block\n\n* List continues", + "\n", "* List\n\n code block with spaces\n", "\n", @@ -1096,7 +1096,7 @@ func TestFencedCodeBlock(t *testing.T) { "

``` lisp\nno ending

\n", "~~~ lisp\nend with language\n~~~ lisp\n", - "

~~~ lisp\nend with language\n~~~ lisp

\n", + "
end with language\n
\n\n

lisp

\n", "```\nmismatched begin and end\n~~~\n", "

```\nmismatched begin and end\n~~~

\n", @@ -1139,6 +1139,21 @@ func TestFencedCodeBlock(t *testing.T) { "```\n[]:()\n[]:)\n[]:(\n[]:x\n[]:testing\n[:testing\n\n[]:\nlinebreak\n[]()\n\n[]:\n[]()\n```", "
[]:()\n[]:)\n[]:(\n[]:x\n[]:testing\n[:testing\n\n[]:\nlinebreak\n[]()\n\n[]:\n[]()\n
\n", + + "- test\n\n```\n codeblock\n ```\ntest\n", + "\n\n

test

\n", + + "- ```\n codeblock\n ```\n\n- test\n", + "\n", + + "- test\n- ```\n codeblock\n ```\n", + "\n", + + "- test\n```\ncodeblock\n```\n\n- test\n", + "\n", + + "- test\n```go\nfunc foo() bool {\n\treturn true;\n}\n```\n\n- test\n", + "\n", } doTestsBlock(t, tests, EXTENSION_FENCED_CODE) } @@ -1557,7 +1572,7 @@ func TestFencedCodeBlock_EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK(t *testing.T) { "

``` lisp\nno ending

\n", "~~~ lisp\nend with language\n~~~ lisp\n", - "

~~~ lisp\nend with language\n~~~ lisp

\n", + "
end with language\n
\n\n

lisp

\n", "```\nmismatched begin and end\n~~~\n", "

```\nmismatched begin and end\n~~~

\n",