Skip to content
Open
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
9 changes: 7 additions & 2 deletions src/Rules/ListRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ public function parse(Parser $parser): ?Token
$newlines = $parser->consumeWhile(Parser::NEW_LINE);
}

if (preg_match('/(?:^|\n)[-*+] /', $childContent)) {
if (preg_match(
'/(?:^|\n)(?:[-*+]|[0-9]+\.) /',
$childContent,
)) {
break;
}

Expand Down Expand Up @@ -77,7 +80,9 @@ public function parse(Parser $parser): ?Token

$children = $childContent === ''
? null
: $parser->withRules(new ListRule())->lex($childContent)[0];
: $parser->withRules(new ListRule(), new OrderedListRule())->lex(
$childContent,
)[0];

$item = new ListItem($content, $children);

Expand Down
4 changes: 3 additions & 1 deletion src/Rules/OrderedListRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public function parse(Parser $parser): ?Token
}

$children = $childContent !== ''
? $parser->withRules(new OrderedListRule())->lex($childContent)[0]
? $parser->withRules(new ListRule(), new OrderedListRule())->lex(
$childContent,
)[0]
: null;

$item = new ListItem($content, $children);
Expand Down
15 changes: 15 additions & 0 deletions tests/Rules/ListRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\Attributes\Test;
use Tempest\Markdown\Parser;
use Tempest\Markdown\Rules\ListRule;
use Tempest\Markdown\Rules\OrderedListRule;
use Tempest\Markdown\Rules\TextRule;
use Tempest\Markdown\Tests\ParserTestCase;

Expand Down Expand Up @@ -158,4 +159,18 @@ public function test_lex_nested_sibling_after_sublist(): void
$html,
);
}

#[Test]
public function test_lex_nested_ordered_list(): void
{
$html = (string) new Parser(highlighter: null, rules: [
new ListRule(),
new OrderedListRule(),
])->parse("- one\n 1. child\n- two\n");

$this->assertSame(
'<ul><li>one<ol><li>child</li></ol></li><li>two</li></ul>',
$html,
);
}
}
15 changes: 15 additions & 0 deletions tests/Rules/OrderedListRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\Attributes\Test;
use Tempest\Markdown\Parser;
use Tempest\Markdown\Rules\ListRule;
use Tempest\Markdown\Rules\OrderedListRule;
use Tempest\Markdown\Rules\TextRule;
use Tempest\Markdown\Tests\ParserTestCase;
Expand Down Expand Up @@ -110,6 +111,20 @@ public function test_lex_nested_sibling_after_sublist(): void
);
}

#[Test]
public function test_lex_nested_bullet_list(): void
{
$html = (string) new Parser(highlighter: null, rules: [
new OrderedListRule(),
new ListRule(),
])->parse("1. one\n * child\n * other\n2. two\n");

$this->assertSame(
'<ol><li>one<ul><li>child</li><li>other</li></ul></li><li>two</li></ol>',
$html,
);
}

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