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
22 changes: 16 additions & 6 deletions src/Rules/ListRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -34,19 +34,29 @@ 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];
}

if ($children === null && $childContent !== '') {
$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) {
Expand Down
18 changes: 18 additions & 0 deletions tests/Rules/ListRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ public function test_lex_multiple_items(): void
$this->assertSame('<ul><li>one</li><li>two</li></ul>', $html);
}

#[Test]
public function test_asterisk_and_plus_markers(): void
{
$parser = new Parser(highlighter: null, rules: [new ListRule()]);

$this->assertSame('<ul><li>one</li><li>two</li></ul>', (string) $parser->parse("* one\n* two"));
$this->assertSame('<ul><li>one</li><li>two</li></ul>', (string) $parser->parse("+ one\n+ two"));
}

#[Test]
public function test_lazy_continuation(): void
{
$parser = new Parser(highlighter: null, rules: [new ListRule()]);

$this->assertSame('<ul><li>one continued</li><li>two</li></ul>', (string) $parser->parse("- one\ncontinued\n- two"));
$this->assertSame('<ul><li>one continued</li></ul>', (string) $parser->parse("* one\ncontinued"));
}

#[Test]
public function test_lex_multiline_items(): void
{
Expand Down
Loading