From 8f4da5a6b1ef09d55c0f3ba127fc66330d4ca6b3 Mon Sep 17 00:00:00 2001 From: Mike Date: Sat, 28 Nov 2026 07:39:02 +0100 Subject: [PATCH 1/2] Fix table swallowing a following list item or other block that contains 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. --- CHANGELOG.md | 8 ++ .../commonmark/ext/gfm/tables/TablesTest.java | 134 ++++++++++++++++++ .../commonmark/internal/DocumentParser.java | 4 +- .../internal/IndentedCodeBlockParser.java | 6 +- 4 files changed, 149 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f94d6c018..e2bb6a864 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html), with the exception that 0.x versions can break between minor versions. +## [Unreleased] +### Fixed +- GFM tables: a line that starts another block (a list item, block quote, heading, + fenced code block, HTML block) now ends the table, also when it contains a pipe. + Before, such a line was parsed as a table row. An indented code block still can not + interrupt a table, like it can not interrupt a paragraph. + ## [0.33.0] - 2026-11-20 ### Added - New artifact `org.commonmark:commonmark-ext-github` with a `GitHubExtensions` @@ -636,6 +643,7 @@ API breaking changes (caused by changes in spec): Initial release of commonmark-java, a port of commonmark.js with extensions for autolinking URLs, GitHub flavored strikethrough and tables. +[Unreleased]: https://github.com/commonmark/commonmark-java/compare/commonmark-parent-0.33.0...HEAD [0.33.0]: https://github.com/commonmark/commonmark-java/compare/commonmark-parent-0.32.0...commonmark-parent-0.33.0 [0.32.0]: https://github.com/commonmark/commonmark-java/compare/commonmark-parent-0.31.0...commonmark-parent-0.32.0 [0.31.0]: https://github.com/commonmark/commonmark-java/compare/commonmark-parent-0.30.1...commonmark-parent-0.31.0 diff --git a/commonmark-ext-gfm-tables/src/test/java/org/commonmark/ext/gfm/tables/TablesTest.java b/commonmark-ext-gfm-tables/src/test/java/org/commonmark/ext/gfm/tables/TablesTest.java index 31df43b76..150bc7d56 100644 --- a/commonmark-ext-gfm-tables/src/test/java/org/commonmark/ext/gfm/tables/TablesTest.java +++ b/commonmark-ext-gfm-tables/src/test/java/org/commonmark/ext/gfm/tables/TablesTest.java @@ -1150,6 +1150,140 @@ public void interruptsParagraph() { """); } + @Test + public void bulletListWithPipeEndsTable() { + assertRendering( + """ + | Header | + | --- | + * | [Keep me](https://example.com/) | + """, + """ + + + + + + +
Header
+ + """); + } + + @Test + public void bodyRowThenBulletListWithPipeEndsTable() { + assertRendering( + """ + | Header | + | --- | + | a | + - | b | + """, + """ + + + + + + + + + + + +
Header
a
+ + """); + } + + @Test + public void otherBlockStartsEndTable() { + // Each of these lines contains a pipe but starts another block, which ends the table. + assertRendering( + """ + | Header | + | --- | + > | quote | + """, + """ + + + + + + +
Header
+
+

| quote |

+
+ """); + assertRendering( + """ + | Header | + | --- | + # heading | x + """, + """ + + + + + + +
Header
+

heading | x

+ """); + assertRendering( + """ + | Header | + | --- | + ```a|b + code + ``` + """, + """ + + + + + + +
Header
+
code
+                
+ """); + } + + @Test + public void indentedLineWithPipeStaysTableRow() { + // An indented code block can not interrupt a table, just like it can not interrupt a + // paragraph. + assertRendering( + """ + | Header | + | --- | + | a | + """, + """ + + + + + + + + + + + +
Header
a
+ """); + } + @Test public void attributeProviderIsApplied() { AttributeProviderFactory factory = diff --git a/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java b/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java index 3724a8cf6..6938e60de 100644 --- a/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java +++ b/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java @@ -249,7 +249,9 @@ private void parseLine(String ln, int inputIndex) { // Unless last matched container is a code block, try new container starts, // adding children to the last matched container: boolean tryBlockStarts = - blockParser.getBlock() instanceof Paragraph || blockParser.isContainer(); + blockParser.getBlock() instanceof Paragraph + || blockParser.isContainer() + || blockParser.canHaveLazyContinuationLines(); while (tryBlockStarts) { lastIndex = index; findNextNonSpace(); diff --git a/commonmark/src/main/java/org/commonmark/internal/IndentedCodeBlockParser.java b/commonmark/src/main/java/org/commonmark/internal/IndentedCodeBlockParser.java index b05b0a456..6c444a96d 100644 --- a/commonmark/src/main/java/org/commonmark/internal/IndentedCodeBlockParser.java +++ b/commonmark/src/main/java/org/commonmark/internal/IndentedCodeBlockParser.java @@ -60,10 +60,12 @@ public static class Factory extends AbstractBlockParserFactory { @Override public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) { - // An indented code block cannot interrupt a paragraph. + // An indented code block cannot interrupt a paragraph (or any other block that takes + // lazy continuation lines, like a table). if (state.getIndent() >= Parsing.CODE_BLOCK_INDENT && !state.isBlank() - && !(state.getActiveBlockParser().getBlock() instanceof Paragraph)) { + && !(state.getActiveBlockParser().getBlock() instanceof Paragraph) + && !state.getActiveBlockParser().canHaveLazyContinuationLines()) { return BlockStart.of(new IndentedCodeBlockParser()) .atColumn(state.getColumn() + Parsing.CODE_BLOCK_INDENT); } else { From 2a6f2e4452800c3aad2fa2236bc9b2d9d3bd1ba2 Mon Sep 17 00:00:00 2001 From: Mike Date: Sat, 28 Nov 2026 17:29:10 +0100 Subject: [PATCH 2/2] Drop redundant instanceof Paragraph check in tryBlockStarts --- .../src/main/java/org/commonmark/internal/DocumentParser.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java b/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java index 6938e60de..36cb8e145 100644 --- a/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java +++ b/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java @@ -249,9 +249,7 @@ private void parseLine(String ln, int inputIndex) { // Unless last matched container is a code block, try new container starts, // adding children to the last matched container: boolean tryBlockStarts = - blockParser.getBlock() instanceof Paragraph - || blockParser.isContainer() - || blockParser.canHaveLazyContinuationLines(); + blockParser.isContainer() || blockParser.canHaveLazyContinuationLines(); while (tryBlockStarts) { lastIndex = index; findNextNonSpace();