Skip to content

Commit 2f9fbcc

Browse files
committed
Keep list tight for a blank line inside an open fenced code block
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.
1 parent 36b4ff0 commit 2f9fbcc

3 files changed

Lines changed: 82 additions & 3 deletions

File tree

‎commonmark/src/main/java/org/commonmark/internal/ListBlockParser.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ public BlockContinue tryContinue(ParserState state) {
5050
hadBlankLine = true;
5151
linesAfterBlank = 0;
5252
} else if (hadBlankLine) {
53-
linesAfterBlank++;
53+
if (linesAfterBlank == 0 && ListItemParser.isInFencedCodeBlock(state)) {
54+
// The blank line was part of a fenced code block that is still open, so it
55+
// doesn't separate list items
56+
hadBlankLine = false;
57+
} else {
58+
linesAfterBlank++;
59+
}
5460
}
5561
// List blocks themselves don't have any markers, only list items. So try to stay in the
5662
// list. If there is a block start other than list item, canContain makes sure that this

‎commonmark/src/main/java/org/commonmark/internal/ListItemParser.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.commonmark.internal;
22

33
import org.commonmark.node.Block;
4+
import org.commonmark.node.FencedCodeBlock;
45
import org.commonmark.node.ListBlock;
56
import org.commonmark.node.ListItem;
67
import org.commonmark.parser.block.AbstractBlockParser;
@@ -57,8 +58,9 @@ public Block getBlock() {
5758
@Override
5859
public BlockContinue tryContinue(ParserState state) {
5960
// Only remember whether the previous line was blank, so that blank lines inside a block
60-
// that continues afterwards (e.g. a code block) don't affect if the list is tight.
61-
previousLineBlank = lineBlank;
61+
// that continues afterwards (e.g. a code block) don't affect if the list is tight. If a
62+
// fenced code block is still open, the blank line was part of it.
63+
previousLineBlank = lineBlank && !isInFencedCodeBlock(state);
6264
lineBlank = state.isBlank();
6365
if (lineBlank) {
6466
if (block.getFirstChild() == null) {
@@ -76,4 +78,8 @@ public BlockContinue tryContinue(ParserState state) {
7678
return BlockContinue.none();
7779
}
7880
}
81+
82+
static boolean isInFencedCodeBlock(ParserState state) {
83+
return state.getActiveBlockParser().getBlock() instanceof FencedCodeBlock;
84+
}
7985
}

‎commonmark/src/test/java/org/commonmark/test/ListTightLooseTest.java‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,71 @@ public void tightOuterWithLooseNested() {
348348
</ul>
349349
""");
350350
}
351+
352+
@Test
353+
public void tightListWithBlankLineInUnclosedFencedCodeBlock() {
354+
// The blank line is part of the fenced code block, so it doesn't separate the items
355+
assertRendering(
356+
"""
357+
- a
358+
- ```
359+
360+
- b
361+
""",
362+
"""
363+
<ul>
364+
<li>a</li>
365+
<li>
366+
<pre><code>
367+
</code></pre>
368+
</li>
369+
<li>b</li>
370+
</ul>
371+
""");
372+
}
373+
374+
@Test
375+
public void tightOuterWithBlankLineInNestedFencedCodeBlock() {
376+
assertRendering(
377+
"""
378+
- - ```
379+
380+
b
381+
""",
382+
"""
383+
<ul>
384+
<li>
385+
<ul>
386+
<li>
387+
<pre><code>
388+
</code></pre>
389+
</li>
390+
</ul>
391+
b</li>
392+
</ul>
393+
""");
394+
}
395+
396+
@Test
397+
public void looseBlankLineAfterFencedCodeBlockInBlockQuote() {
398+
// The blank line ends the block quote, so it isn't part of the fenced code block
399+
assertRendering(
400+
"""
401+
- > ```
402+
403+
- b
404+
""",
405+
"""
406+
<ul>
407+
<li>
408+
<blockquote>
409+
<pre><code></code></pre>
410+
</blockquote>
411+
</li>
412+
<li>
413+
<p>b</p>
414+
</li>
415+
</ul>
416+
""");
417+
}
351418
}

0 commit comments

Comments
 (0)