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 @@ -3,7 +3,6 @@
import org.commonmark.node.Block;
import org.commonmark.node.ListBlock;
import org.commonmark.node.ListItem;
import org.commonmark.node.Paragraph;
import org.commonmark.parser.block.AbstractBlockParser;
import org.commonmark.parser.block.BlockContinue;
import org.commonmark.parser.block.ParserState;
Expand All @@ -18,7 +17,9 @@ public class ListItemParser extends AbstractBlockParser {
*/
private int contentIndent;

private boolean hadBlankLine;
private boolean lineBlank;

private boolean previousLineBlank;

public ListItemParser(int markerIndent, int contentIndent) {
this.contentIndent = contentIndent;
Expand All @@ -33,8 +34,10 @@ public boolean isContainer() {

@Override
public boolean canContain(Block childBlock) {
if (hadBlankLine) {
// We saw a blank line in this list item, that means the list block is loose.
if (previousLineBlank) {
// A new block starts after a blank line, and blank lines after an empty list item end
// the item, so this item already has a block before the blank line. That means the
// list block is loose.
//
// spec: if any of its constituent list items directly contain two block-level elements
// with a blank line between them
Expand All @@ -53,15 +56,15 @@ public Block getBlock() {

@Override
public BlockContinue tryContinue(ParserState state) {
if (state.isBlank()) {
// 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;
lineBlank = state.isBlank();
if (lineBlank) {
if (block.getFirstChild() == null) {
// Blank line after empty list item
return BlockContinue.none();
} else {
Block activeBlock = state.getActiveBlockParser().getBlock();
// If the active block is a code block, blank lines in it should not affect if the
// list is tight.
hadBlankLine = activeBlock instanceof Paragraph || activeBlock instanceof ListItem;
return BlockContinue.atIndex(state.getNextNonSpaceIndex());
}
}
Expand Down
104 changes: 104 additions & 0 deletions commonmark/src/test/java/org/commonmark/test/ListTightLooseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,108 @@ public void looseBlankLineAfterCodeBlock() {
</ol>
""");
}

@Test
public void looseBlankLineAfterHtmlBlock() {
assertRendering(
"""
- <!-- comment -->

bar
""",
"""
<ul>
<li>
<!-- comment -->
<p>bar</p>
</li>
</ul>
""");
}

@Test
public void looseBlankLineAfterIndentedCodeBlock() {
assertRendering(
"""
- foo

bar
""",
"""
<ul>
<li>
<pre><code>foo
</code></pre>
<p>bar</p>
</li>
</ul>
""");
}

@Test
public void looseBlankLineAfterSetextHeading() {
assertRendering(
"""
- foo
-

bar
""",
"""
<ul>
<li>
<h2>foo</h2>
<p>bar</p>
</li>
</ul>
""");
}

@Test
public void tightListWithIndentedCodeBlock() {
assertRendering(
"""
- foo

bar
baz
""",
"""
<ul>
<li>
<pre><code>foo

bar
</code></pre>
baz</li>
</ul>
""");
}

@Test
public void tightOuterWithLooseNested() {
// The blank line is between two blocks of the nested item, not between two blocks of the
// outer item.
assertRendering(
"""
- a
- b

c
# d
""",
"""
<ul>
<li>a
<ul>
<li>
<p>b</p>
<p>c</p>
</li>
</ul>
<h1>d</h1>
</li>
</ul>
""");
}
}