Skip to content

Keep list tight for a blank line inside an open fenced code block - #481

Merged
robinst merged 1 commit into
commonmark:mainfrom
bardrop-oss:fix-loose-list-fenced-code
Nov 30, 2026
Merged

robinst merged 1 commit into
commonmark:mainfrom
bardrop-oss:fix-loose-list-fenced-code

Conversation

@bardrop

@bardrop bardrop commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

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:

- a
- ```

- b

commonmark-java made the list loose:

<ul>
<li>
<p>a</p>
</li>
<li>
<pre><code>
</code></pre>
</li>
<li>
<p>b</p>
</li>
</ul>

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:

- - ```

  b

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 ListBlockParser and ListItemParser don't count it. A blank line that ends a block quote around the fence still counts, so - > ~~~, blank line, - b stays 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. Full mvn test passes (2305 in commonmark, all other modules green), and mvn spotless:check passes. Expected outputs are what cmark-gfm and commonmark.js 0.31.2 produce.

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)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking! No I guess it's fine then.

@robinst
robinst merged commit 088be69 into commonmark:main Nov 30, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants