Skip to content
Open
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.

// 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