From d93fbf23934d091ab75d1346f003b8db47206cfb Mon Sep 17 00:00:00 2001 From: ybai08 <271969568+ybai08@users.noreply.github.com> Date: Wed, 28 Oct 2026 07:07:11 -0500 Subject: [PATCH] Don't let lists interrupt a paragraph with only link reference definitions A paragraph whose lines so far are all link reference definitions is still an open paragraph, so an ordered list not starting with 1 or an empty list item can't interrupt it. ListBlockParser checked for remaining paragraph lines instead, so e.g. "[foo]: /url\n2. a" was rendered as a list starting at 2 instead of a paragraph "2. a". Check whether the matched block is a paragraph, like HtmlBlockParser does for type 7. This matches commonmark.js 0.31.2. --- .../org/commonmark/internal/ListBlockParser.java | 3 ++- .../java/org/commonmark/test/SpecialInputTest.java | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/commonmark/src/main/java/org/commonmark/internal/ListBlockParser.java b/commonmark/src/main/java/org/commonmark/internal/ListBlockParser.java index f160b10ff..e6088c0a6 100644 --- a/commonmark/src/main/java/org/commonmark/internal/ListBlockParser.java +++ b/commonmark/src/main/java/org/commonmark/internal/ListBlockParser.java @@ -211,7 +211,8 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar } int markerIndex = state.getNextNonSpaceIndex(); int markerColumn = state.getColumn() + state.getIndent(); - boolean inParagraph = !matchedBlockParser.getParagraphLines().isEmpty(); + // A paragraph that only has link reference definitions so far is still open + boolean inParagraph = matched.getBlock() instanceof Paragraph; ListData listData = parseList(state.getLine().getContent(), markerIndex, markerColumn, inParagraph); if (listData == null) { diff --git a/commonmark/src/test/java/org/commonmark/test/SpecialInputTest.java b/commonmark/src/test/java/org/commonmark/test/SpecialInputTest.java index f8d03df98..59382b28f 100644 --- a/commonmark/src/test/java/org/commonmark/test/SpecialInputTest.java +++ b/commonmark/src/test/java/org/commonmark/test/SpecialInputTest.java @@ -223,6 +223,20 @@ public void linkReferenceDefinitionWithUnbalancedParens() { "[foo]: a(b) \"t\"\n\n[foo]", "

foo

\n"); } + @Test + public void listCanNotInterruptParagraphWithOnlyLinkReferenceDefinitions() { + // The paragraph is still open, so the same rules apply as for a paragraph with text: an + // ordered list must start with 1 and an empty list item can not interrupt it + assertRendering( + "[foo]: /url\n2. a\n\n[foo]", "

2. a

\n

foo

\n"); + assertRendering("[foo]: /url\n-\n\n[foo]", "

-

\n

foo

\n"); + assertRendering("[foo]: /url \"t\"\n2. a", "

2. a

\n"); + assertRendering("> [foo]: /url\n> 2. a", "
\n

2. a

\n
\n"); + // These can interrupt it + assertRendering("[foo]: /url\n1. a", "
    \n
  1. a
  2. \n
\n"); + assertRendering("[foo]: /url\n- a", "\n"); + } + @Test public void linkLabelLength() { String label1 = "a".repeat(999);