Skip to content
Merged
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
7 changes: 4 additions & 3 deletions src/Parser/BlockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2533,9 +2533,10 @@ protected function startsNewBlockSignificant(string $line): bool
// Fenced divs: :{3,}
return isset($line[1], $line[2]) && $line[1] === ':' && $line[2] === ':';
default:
// Ordered lists: digit or letter followed by . or )
if (ctype_digit($first) || ctype_alpha($first)) {
return preg_match('/^(\d+|[a-zA-Z])[.)]\s/', $line) === 1;
// Only 1. or 1) can interrupt paragraphs (CommonMark rule)
// Prevents "1985. That year..." from becoming a list
if ($first === '1') {
return preg_match('/^1[.)]\s/', $line) === 1;
}

return false;
Expand Down
48 changes: 48 additions & 0 deletions tests/TestCase/SignificantNewlinesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,52 @@ public function testHeadingInterruptsParagraphInSignificantNewlinesMode(): void
$this->assertInstanceOf(Paragraph::class, $children[0]);
$this->assertInstanceOf(Heading::class, $children[1]);
}

// ==================== CommonMark Ordered List Rule ====================

public function testOnlyOneCanInterruptParagraph(): void
{
// Only "1." can interrupt a paragraph (CommonMark rule)
$parser = new BlockParser(significantNewlines: true);
$doc = $parser->parse("Steps:\n1. First step");

$children = $doc->getChildren();
$this->assertCount(2, $children);
$this->assertInstanceOf(Paragraph::class, $children[0]);
$this->assertInstanceOf(ListBlock::class, $children[1]);
}

public function testYearDoesNotBecomeList(): void
{
// "1985." should NOT interrupt - prevents years becoming lists
$parser = new BlockParser(significantNewlines: true);
$doc = $parser->parse("My favorite year was\n1985. It was great.");

$children = $doc->getChildren();
$this->assertCount(1, $children);
$this->assertInstanceOf(Paragraph::class, $children[0]);
}

public function testHighNumberedListDoesNotInterrupt(): void
{
// "5." should NOT interrupt paragraphs
$parser = new BlockParser(significantNewlines: true);
$doc = $parser->parse("Continue from step\n5. Do this thing");

$children = $doc->getChildren();
$this->assertCount(1, $children);
$this->assertInstanceOf(Paragraph::class, $children[0]);
}

public function testHighNumberedListAfterBlankLine(): void
{
// With blank line, any number can start a list
$parser = new BlockParser(significantNewlines: true);
$doc = $parser->parse("Continue from step\n\n5. Do this thing");

$children = $doc->getChildren();
$this->assertCount(2, $children);
$this->assertInstanceOf(Paragraph::class, $children[0]);
$this->assertInstanceOf(ListBlock::class, $children[1]);
}
}