Skip to content

Fix table swallowing a following list item or other block that contains a pipe - #483

Open
MikeFTV wants to merge 2 commits into
commonmark:mainfrom
MikeFTV:fix/table-ends-at-list-item
Open

MikeFTV wants to merge 2 commits into
commonmark:mainfrom
MikeFTV:fix/table-ends-at-list-item

Conversation

@MikeFTV

@MikeFTV MikeFTV commented Nov 28, 2026

Copy link
Copy Markdown

Why

With the tables extension, a line that contains a pipe and starts another block is parsed as a table row. The case from #482:

| Header |
| --- |
* | [Keep me](https://example.com/) |

renders one table whose body is <tr><td>*</td></tr>, and the link is lost. cmark-gfm renders the table and then a list. The GFM spec says the table ends at the first empty line or the beginning of another block-level structure.

The cause is not specific to bullets. DocumentParser only tries block starts after a paragraph or a container, and TableBlockParser.tryContinue accepts any line with a pipe, so a block quote (> | x |), a heading (# h | x), a fenced code block (```a|b), an HTML block and an ordered list item were swallowed the same way (checked before the change).

Fixes #482.

What changes

  • DocumentParser: also try block starts when the matched block parser canHaveLazyContinuationLines(). Only ParagraphParser (already covered) and TableBlockParser return true, so in practice this adds tables.
  • IndentedCodeBlockParser: an indented code block can not interrupt a block that takes lazy continuation lines, as it can not interrupt a paragraph. Without this, an existing test (paddingWithCodeBlockIndentation, a row indented by four spaces) fails.
  • Four tests in TablesTest: bullet with a pipe after the header and after a body row, block quote / heading / fenced code, and an indented row that stays a table row.
  • CHANGELOG.md: entry under [Unreleased].

Tests

  • ./mvnw -B test on the whole repository: BUILD SUCCESS, no failure (2322 tests counted across modules, including the CommonMark and GFM spec tests).
  • With commonmark/src restored to main: ./mvnw -B -pl commonmark-ext-gfm-tables -am test -Dtest=TablesTest: 3 of the new tests fail (bulletListWithPipeEndsTable, bodyRowThenBulletListWithPipeEndsTable, otherBlockStartsEndTable); indentedLineWithPipeStaysTableRow is a guard for the second change and passes on both.
  • ./mvnw spotless:check on commonmark-ext-gfm-tables: clean. On commonmark it reports one violation in BackslashInlineParser.java, a file this PR does not touch; it is the same on main with JDK 21.
  • Behaviour change to be aware of: a table row that starts with a list marker, >, #, a fence or an HTML tag now ends the table, as in cmark-gfm. A row that starts with a list marker (- | a |) used to be a one-cell row.

…ns a pipe

A table row was continued for any line containing a pipe, so a line that starts
another block (list item, block quote, heading, fenced code, HTML) was parsed as
a row. Try block starts after a block that can have lazy continuation lines, as
for a paragraph, and keep indented code from interrupting it.
// adding children to the last matched container:
boolean tryBlockStarts =
blockParser.getBlock() instanceof Paragraph || blockParser.isContainer();
blockParser.getBlock() instanceof Paragraph

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.

WDYT about removing the instanceof Paragraph (given ParagraphParser returns true for canHaveLazyContinuationLines)?

Theoretically someone could have implemented a block parser with a Paragraph block but with canHaveLazyContinuationLines returning false, which would mean we'd change behavior, but it seems unlikely.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good call, removed. ParagraphParser already returns true for canHaveLazyContinuationLines, so the instanceof Paragraph branch added nothing for the built-in parsers, and I agree a custom parser with a Paragraph block and no lazy continuation is unlikely. Full ./mvnw test passes after the change (the table tests, including the indented-row case, still included).

This branch has not been deployed

No deployments
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.

CommonMark Java GFM tables swallow a following list item that contains pipes

2 participants