Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ public BlockContinue tryContinue(ParserState state) {
hadBlankLine = true;
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.

// The blank line was part of a fenced code block that is still open, so it
// doesn't separate list items
hadBlankLine = false;
} else {
linesAfterBlank++;
}
}
// List blocks themselves don't have any markers, only list items. So try to stay in the
// list. If there is a block start other than list item, canContain makes sure that this
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.commonmark.internal;

import org.commonmark.node.Block;
import org.commonmark.node.FencedCodeBlock;
import org.commonmark.node.ListBlock;
import org.commonmark.node.ListItem;
import org.commonmark.parser.block.AbstractBlockParser;
Expand Down Expand Up @@ -57,8 +58,9 @@ public Block getBlock() {
@Override
public BlockContinue tryContinue(ParserState state) {
// Only remember whether the previous line was blank, so that blank lines inside a block
// that continues afterwards (e.g. a code block) don't affect if the list is tight.
previousLineBlank = lineBlank;
// that continues afterwards (e.g. a code block) don't affect if the list is tight. If a
// fenced code block is still open, the blank line was part of it.
previousLineBlank = lineBlank && !isInFencedCodeBlock(state);
lineBlank = state.isBlank();
if (lineBlank) {
if (block.getFirstChild() == null) {
Expand All @@ -76,4 +78,8 @@ public BlockContinue tryContinue(ParserState state) {
return BlockContinue.none();
}
}

static boolean isInFencedCodeBlock(ParserState state) {
return state.getActiveBlockParser().getBlock() instanceof FencedCodeBlock;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,71 @@ public void tightOuterWithLooseNested() {
</ul>
""");
}

@Test
public void tightListWithBlankLineInUnclosedFencedCodeBlock() {
// The blank line is part of the fenced code block, so it doesn't separate the items
assertRendering(
"""
- a
- ```

- b
""",
"""
<ul>
<li>a</li>
<li>
<pre><code>
</code></pre>
</li>
<li>b</li>
</ul>
""");
}

@Test
public void tightOuterWithBlankLineInNestedFencedCodeBlock() {
assertRendering(
"""
- - ```

b
""",
"""
<ul>
<li>
<ul>
<li>
<pre><code>
</code></pre>
</li>
</ul>
b</li>
</ul>
""");
}

@Test
public void looseBlankLineAfterFencedCodeBlockInBlockQuote() {
// The blank line ends the block quote, so it isn't part of the fenced code block
assertRendering(
"""
- > ```

- b
""",
"""
<ul>
<li>
<blockquote>
<pre><code></code></pre>
</blockquote>
</li>
<li>
<p>b</p>
</li>
</ul>
""");
}
}
Loading