diff --git a/src/Rules/ListRule.php b/src/Rules/ListRule.php index ea4588c..d4a3cf4 100644 --- a/src/Rules/ListRule.php +++ b/src/Rules/ListRule.php @@ -11,18 +11,18 @@ final class ListRule implements Rule, ProvidesFirstChar { - public string $firstChar = '-'; + public string $firstChar = '-*+'; public function shouldParse(Parser $parser): bool { - return $parser->comesNext('- ', 2); + return $parser->comesNext(($parser->current ?? '') . ' ', 2); } public function parse(Parser $parser): ?Token { - $parser->consumeIncluding('- '); + $parser->consume(2); $content = trim($parser->consumeUntil(Parser::NEW_LINE)); - $parser->consumeWhile(Parser::NEW_LINE); + $newlines = $parser->consumeWhile(Parser::NEW_LINE); $childContent = ''; $indent = strspn($parser->content, ' ', $parser->position); @@ -34,12 +34,12 @@ public function parse(Parser $parser): ?Token $parser->consume($indent); $childContent .= $parser->consumeUntil(Parser::NEW_LINE) . PHP_EOL; - $parser->consumeWhile(Parser::NEW_LINE); + $newlines = $parser->consumeWhile(Parser::NEW_LINE); } $children = null; - if (substr_count($childContent, '-') > 0) { + if (preg_match('/(?:^|\n)[-*+] /', $childContent)) { $children = $parser->withRules(new ListRule())->lex($childContent)[0]; } @@ -47,6 +47,16 @@ public function parse(Parser $parser): ?Token $content .= ' ' . trim(preg_replace('/\s+/u', ' ', $childContent) ?? ''); } + while ($children === null && ($newlines === "\n" || $newlines === "\r\n") && $parser->current !== null) { + $nextLine = substr($parser->content, $parser->position, strcspn($parser->content, Parser::NEW_LINE, $parser->position)); + if (trim($nextLine) === '' || preg_match('/^(?:[-*+] |[0-9]+[.)] |#{1,6}[ \t]|> |`{3}|~{3})/', $nextLine)) { + break; + } + + $content .= ' ' . trim($parser->consumeUntil(Parser::NEW_LINE)); + $newlines = $parser->consumeWhile(Parser::NEW_LINE); + } + $item = new ListItem($content, $children); if ($parser->lastToken instanceof ListToken) { diff --git a/tests/Rules/ListRuleTest.php b/tests/Rules/ListRuleTest.php index a67aa4c..b91e5a5 100644 --- a/tests/Rules/ListRuleTest.php +++ b/tests/Rules/ListRuleTest.php @@ -26,6 +26,24 @@ public function test_lex_multiple_items(): void $this->assertSame('', $html); } + #[Test] + public function test_asterisk_and_plus_markers(): void + { + $parser = new Parser(highlighter: null, rules: [new ListRule()]); + + $this->assertSame('', (string) $parser->parse("* one\n* two")); + $this->assertSame('', (string) $parser->parse("+ one\n+ two")); + } + + #[Test] + public function test_lazy_continuation(): void + { + $parser = new Parser(highlighter: null, rules: [new ListRule()]); + + $this->assertSame('', (string) $parser->parse("- one\ncontinued\n- two")); + $this->assertSame('', (string) $parser->parse("* one\ncontinued")); + } + #[Test] public function test_lex_multiline_items(): void {