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
4 changes: 2 additions & 2 deletions src/Rules/OrderedListRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function shouldParse(Parser $parser): bool

public function parse(Parser $parser): ?Token
{
$parser->consumeWhile('0123456789');
$number = $parser->consumeWhile('0123456789');
$parser->consumeIncluding('.');
$content = trim($parser->consumeUntil(Parser::NEW_LINE));
$parser->consumeWhile(Parser::NEW_LINE);
Expand Down Expand Up @@ -68,6 +68,6 @@ public function parse(Parser $parser): ?Token
return null;
}

return new OrderedListToken([$item]);
return new OrderedListToken([$item], (int) $number);
}
}
7 changes: 6 additions & 1 deletion src/Tokens/OrderedListToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ final class OrderedListToken implements Token
public function __construct(
/** @var \Tempest\Markdown\Tokens\ListItem[] */
public array $items = [],

/** The number the list starts at, as written in the first item's marker. */
public int $start = 1,
) {}

public function parse(Parser $parser): string
Expand All @@ -33,7 +36,9 @@ public function parse(Parser $parser): string
new TextRule(),
]);

$list = '<ol>';
$list = $this->start === 1
? '<ol>'
: '<ol start="' . $this->start . '">';

foreach ($this->items as $item) {
$content = $parser->parse($item->content);
Expand Down
30 changes: 29 additions & 1 deletion tests/Rules/OrderedListRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public function test_lex_multi_digit_numbers(): void
"10. ten\n11. eleven\n",
);

$this->assertSame('<ol><li>ten</li><li>eleven</li></ol>', $html);
$this->assertSame(
'<ol start="10"><li>ten</li><li>eleven</li></ol>',
$html,
);
}

#[Test]
Expand Down Expand Up @@ -117,4 +120,29 @@ public function test_only_numbers_are_allowed(): void

$this->assertSame('', $html);
}

#[Test]
public function lex_keeps_the_start_number(): void
{
$html =
(string) new Parser(highlighter: null, rules: [new OrderedListRule()])->parse(
"2. two\n3. three\n",
);

$this->assertSame(
'<ol start="2"><li>two</li><li>three</li></ol>',
$html,
);
}

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

$this->assertSame('<ol><li>one</li><li>five</li></ol>', $html);
}
}
Loading