Repository navigation
Keep list tight for a blank line inside an open fenced code block - #481
Conversation
In this input the blank line is part of the fenced code block, which is only closed by the next list item: - a - ``` - b The blank line did not separate the items, but the list was made loose. Same for a blank line in a fenced code block of a nested list followed by more content of the outer item. cmark and commonmark.js keep these lists tight. A blank line in a fenced code block that is still open on the next line was part of the code block, so don't count it.
| linesAfterBlank = 0; | ||
| } else if (hadBlankLine) { | ||
| linesAfterBlank++; | ||
| if (linesAfterBlank == 0 && ListItemParser.isInFencedCodeBlock(state)) { |
There was a problem hiding this comment.
Hmm, can you check other implementations? Do they also special-case fenced code blocks like this? Feels like there should be a more generic way to do this.
There was a problem hiding this comment.
I checked both reference implementations:
- cmark special-cases it the same way. When it records whether a line was blank, it says "not blank" if the open block is a fenced code block (and also for a block quote, heading or thematic break): blocks.c#L1202-L1207.
- commonmark.js has no special case. It uses line numbers: a list item ends where its last child ends (blocks.js#L353-L361), and the list is loose if there is a gap between that and the start of the next item (blocks.js#L78-L81). A fenced code block eats the blank line, so there is no gap. A paragraph or block quote ends before it, so there is one.
So the generic way is the commonmark.js one. Here it would mean tracking the last line of every block even when source spans are off, and comparing line numbers in the list finalizer instead of the hadBlankLine / previousLineBlank flags. That is a bigger change to ListBlockParser and ListItemParser. I'm happy to do it in this PR if you prefer it, or keep the cmark-style check. Which do you want?
There was a problem hiding this comment.
Thanks for checking! No I guess it's fine then.
In this input the blank line is inside the fenced code block. The fence is never closed, so the code block runs until the next list item starts:
commonmark-java made the list loose:
But the blank line is part of the code block (it shows up in the
<code>content), so it does not separate the items. cmark and commonmark.js keep the list tight (<li>a</li>,<li>b</li>). Same thing for a blank line in a fenced code block of a nested list, when more content of the outer item follows:Here the outer list should stay tight (
b</li>), but it got<p>b</p>.The fix: on the line after a blank line, if a fenced code block is still the active block, the blank line was part of that code block, so
ListBlockParserandListItemParserdon't count it. A blank line that ends a block quote around the fence still counts, so- > ~~~, blank line,- bstays loose, like cmark and commonmark.js.I left HTML blocks alone on purpose: for
- <!--, blank line,- b, cmark says loose and commonmark.js says tight.Tests: added 3 cases to
ListTightLooseTest. Before the fix, 2 of them fail (19 run, 2 failures). After the fix all 19 pass. Fullmvn testpasses (2305 incommonmark, all other modules green), andmvn spotless:checkpasses. Expected outputs are what cmark-gfm and commonmark.js 0.31.2 produce.