russross/blackfriday

Block parsing for fenced code blocks with syntax highlighting in lists regression.

dmitshur opened this issue · 5 comments

I've just discovered PR #476 is causing the following regression in v1. /cc @tfogo @client9

Given this Markdown input:

### Codeblock within list

-	list entry 1

	```
	if (i == 5)
	    break;
	```

-	list entry 2

### Stuff that comes later

stuff here

Everything works as expected:

image

However, if one uses a fenced code block with syntax highlighting, everything that comes after the fenced code block ends up being a part of "list entry 1":

### Codeblock within list

-	list entry 1

	```C
	if (i == 5)
	    break;
	```

-	list entry 2

### Stuff that comes later

stuff here

image

I.e., it parses as if the input was:

### Codeblock within list

-	list entry 1

	```C
	if (i == 5)
	    break;
	```

	-	list entry 2

	### Stuff that comes later

	stuff here

I have a lead. Modifying the code as follows seems to fix the issue, at least for that one instance of the problematic input:

-_, marker := isFenceLine(chunk, nil, codeBlockMarker, false)
+_, marker := isFenceLine(chunk, new(string), codeBlockMarker, false)

I.e., passing a non-nil info *string parameter to isFenceLine.

I came here to report a similar issue which sounds like it might have the same cause, right now if a fenced code block immediately follows a list even if it's not indented it becomes part of the list. The following patch adds a test that can be used to demonstrate the behavior (the expected result might have wrong newlines, I just guessed):

diff --git a/block_test.go b/block_test.go
index 326c3110ce70..4db7d24156e2 100644
--- a/block_test.go
+++ b/block_test.go
@@ -1135,6 +1135,9 @@ func TestFencedCodeBlock(t *testing.T) {

                "```\n[]:()\n[]:)\n[]:(\n[]:x\n[]:testing\n[:testing\n\n[]:\nlinebreak\n[]()\n\n[]:\n[]()\n```",
                "<pre><code>[]:()\n[]:)\n[]:(\n[]:x\n[]:testing\n[:testing\n\n[]:\nlinebreak\n[]()\n\n[]:\n[]()\n</code></pre>\n",                                                                                                                      
+
+               "- test\n\n\n``` go\nfunc foo() bool {\n\treturn true;\n}\n```\n",
+               "<ul>\n<li>test</li>\n<ul>\n\n<pre><code class=\"language-go\">func foo() bool {\n\treturn true;\n}\n</code></pre>\n",                                                                                                                  
        }
        doTestsBlock(t, tests, FencedCode)
 }

EDIT: The above is against the v2 branch, but just noticed that this has the v1 label. Not sure if it's the same or not.

+1 the same issue here

Thank you very much for resolving this issue, @aignas and @rtfb!

You've also indirectly resolved shurcooL/markdownfmt#44.

#715 Same issue on V2