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
39 changes: 21 additions & 18 deletions src/Rules/ListRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,41 @@ public function parse(Parser $parser): ?Token
$newlines = $parser->consumeWhile(Parser::NEW_LINE);

$childContent = '';
$indent = strspn($parser->content, ' ', $parser->position);

while ($indent >= 2 && $parser->current !== null) {
if (strspn($parser->content, ' ', $parser->position) < $indent) {
break;
}
while ($parser->current !== null) {
$indent = strspn($parser->content, ' ', $parser->position);

$parser->consume($indent);
$childContent .= $parser->consumeUntil(Parser::NEW_LINE) . PHP_EOL;
$newlines = $parser->consumeWhile(Parser::NEW_LINE);
}
if ($indent >= 2) {
while ($parser->current !== null && strspn($parser->content, ' ', $parser->position) >= $indent) {
$parser->consume($indent);
$childContent .= $parser->consumeUntil(Parser::NEW_LINE) . PHP_EOL;
$newlines = $parser->consumeWhile(Parser::NEW_LINE);
}

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

if (preg_match('/(?:^|\n)[-*+] /', $childContent)) {
$children = $parser->withRules(new ListRule())->lex($childContent)[0];
}
$content .= ' ' . trim(preg_replace('/\s+/u', ' ', $childContent) ?? '');
$childContent = '';
continue;
}

if ($children === null && $childContent !== '') {
$content .= ' ' . trim(preg_replace('/\s+/u', ' ', $childContent) ?? '');
}
if ($newlines !== "\n" && $newlines !== "\r\n") {
break;
}

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)) {
if (trim($nextLine) === '' || preg_match('/^(?:[-*+] |[0-9]+[.)] |#{1,6}[ \t]|>|`{3}|~{3}|-{3}|={3}|<|:{3})/', $nextLine)) {
break;
}

$content .= ' ' . trim($parser->consumeUntil(Parser::NEW_LINE));
$newlines = $parser->consumeWhile(Parser::NEW_LINE);
}

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

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

if ($parser->lastToken instanceof ListToken) {
Expand Down
36 changes: 20 additions & 16 deletions src/Rules/ParagraphRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,31 @@ public function parse(Parser $parser): Token
$content = '';

while ($parser->current !== null) {
$content .= $parser->consumeUntil(Parser::NEW_LINE);
$line = $parser->consumeUntil(Parser::NEW_LINE);

if ($parser->current === null) {
break;
}
// A blank line ends the paragraph and stays available to the newline rule.
$endsParagraph =
$parser->position >= $parser->length
|| $parser->comesNext("\n\n", 2)
|| $parser->comesNext("\r\n\r\n", 4)
|| $parser->comesNext("\n\r\n", 3)
|| $parser->comesNext("\r\n\n", 3);

// A blank line (two consecutive newlines) ends the paragraph
if ($parser->comesNext("\n\n", 2) || $parser->comesNext("\r\n\r\n", 4) || $parser->comesNext("\n\r\n", 3) || $parser->comesNext("\r\n\n", 3)) {
break;
}
$newline = $endsParagraph ? '' : $parser->consumeWhile(Parser::NEW_LINE);

// Single newline — consume it and continue to the next line
$content .= $parser->consumeWhile(Parser::NEW_LINE);
}
$matches = [];
if ($content !== '' && preg_match('/\A {0,3}(=+|-+)[ \t]*\z/', $line, $matches)) {
$heading = trim($content);
$id = mb_strtolower($heading) |> (fn (string $value) => trim(preg_replace('/[^\p{L}\p{N}]+/u', '-', $value) ?? '', '-'));

$trimmed = rtrim($content, "\r\n");
if (preg_match('/\A(.+)\r?\n {0,3}(=+|-+)[ \t]*\z/s', $trimmed, $matches)) {
$heading = trim($matches[1]);
$id = mb_strtolower($heading) |> (fn (string $value) => trim(preg_replace('/[^\p{L}\p{N}]+/u', '-', $value) ?? '', '-'));
return new HeadingToken($heading, $matches[1][0] === '=' ? 1 : 2, $id);
}

$content .= $line . $newline;

return new HeadingToken($heading, $matches[2][0] === '=' ? 1 : 2, $id);
if ($endsParagraph) {
break;
}
}

return new ParagraphToken($content);
Expand Down
16 changes: 8 additions & 8 deletions src/Rules/StrikethroughRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Tempest\Markdown\Rule;
use Tempest\Markdown\Token;
use Tempest\Markdown\Tokens\StrikethroughToken;
use Tempest\Markdown\Tokens\TextToken;

final class StrikethroughRule implements Rule, ProvidesFirstChar, ProvidesStopChar
{
Expand All @@ -16,18 +17,17 @@ final class StrikethroughRule implements Rule, ProvidesFirstChar, ProvidesStopCh

public function shouldParse(Parser $parser): bool
{
if (! $parser->comesNext('~', 1)) {
return false;
}

$openingLength = strspn($parser->content, '~', $parser->position);

return strpos($parser->content, '~', $parser->position + $openingLength) !== false;
return $parser->comesNext('~', 1);
}

public function parse(Parser $parser): Token
{
$parser->consumeWhile('~');
$opening = $parser->consumeWhile('~');

if (strpos($parser->content, '~', $parser->position) === false) {
return new TextToken($opening);
}

$buffer = $parser->consumeUntil('~');
$parser->consumeWhile('~');

Expand Down
1 change: 1 addition & 0 deletions src/Rules/ThinRulerRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function shouldParse(Parser $parser): bool
public function parse(Parser $parser): Token
{
$content = $parser->consumeWhile('-');
$parser->consumeWhile(" \t");

return new RulerToken(
$content,
Expand Down
2 changes: 2 additions & 0 deletions tests/Rules/FrontMatterRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function standalone_delimiter_is_a_thematic_break(): void
$this->assertSame("<hr/>\n", $parser->parse("---\n")->html);
$this->assertSame('<hr/>', $parser->parse('-----')->html);
$this->assertSame('<hr/>', new Markdown(highlighter: null)->parse('---')->html);
$this->assertSame('<hr/>', new Markdown(highlighter: null)->parse("--- \t")->html);
$this->assertSame("<hr/>\n", new Markdown(highlighter: null)->parse("--- \t\n")->html);
}

#[Test]
Expand Down
22 changes: 22 additions & 0 deletions tests/Rules/ListRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ public function test_lazy_continuation(): void
$this->assertSame('<ul><li>one continued</li></ul>', (string) $parser->parse("* one\ncontinued"));
}

#[Test]
public function test_lazy_continuation_preserves_following_blocks(): void
{
$parser = new Parser(highlighter: null);

$this->assertSame("<ul><li>one</li></ul><hr/>\n<p>after</p>", (string) $parser->parse("- one\n---\nafter"));
$this->assertSame('<ul><li>one</li></ul><div>block</div>', (string) $parser->parse("- one\n<div>block</div>"));
}

#[Test]
public function test_nested_list_after_lazy_continuation(): void
{
$parser = new Parser(highlighter: null);

foreach (['-', '*', '+'] as $marker) {
$this->assertSame(
'<ul><li>one continued<ul><li>child</li></ul></li><li>two</li></ul>',
(string) $parser->parse("{$marker} one\ncontinued\n {$marker} child\n{$marker} two"),
);
}
}

#[Test]
public function test_lex_multiline_items(): void
{
Expand Down
16 changes: 16 additions & 0 deletions tests/Rules/ParagraphRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,20 @@ public function test_setext_heading_can_span_multiple_lines(): void

$this->assertSame("<h2 id=\"first-second\">First\nSecond</h2>", (string) $parser->parse("First\nSecond\n---"));
}

#[Test]
public function test_setext_heading_followed_by_paragraph(): void
{
$parser = new Parser(highlighter: null);

$this->assertSame('<h2 id="title">Title</h2><p>Body</p>', (string) $parser->parse("Title\n---\nBody"));
}

#[Test]
public function test_consecutive_setext_headings(): void
{
$parser = new Parser(highlighter: null);

$this->assertSame('<h1 id="one">One</h1><h2 id="two">Two</h2>', (string) $parser->parse("One\n===\nTwo\n---"));
}
}
23 changes: 23 additions & 0 deletions tests/Rules/StrikethroughRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Tempest\Markdown\Rules\StrikethroughRule;
use Tempest\Markdown\Rules\TextRule;
use Tempest\Markdown\Tests\ParserTestCase;
use Tempest\Markdown\Tokens\TextToken;

class StrikethroughRuleTest extends ParserTestCase
{
Expand Down Expand Up @@ -37,4 +38,26 @@ public function unmatched_tildes_remain_literal(): void
$this->assertSame('~~~', (string) $parser->parse('~~~'));
$this->assertSame('<p>Duration: ~2h30.</p>', new Markdown(highlighter: null)->parse('Duration: ~2h30.')->html);
}

#[Test]
public function test_unmatched_run_is_consumed_as_one_literal_token(): void
{
$opening = str_repeat('~', 80_000);
$parser = new Parser(highlighter: null)->setContent($opening . '**bold**');
$rule = new StrikethroughRule();

$this->assertTrue($rule->shouldParse($parser));
$this->assertEquals(new TextToken($opening), $rule->parse($parser));
$this->assertSame(strlen($opening), $parser->position);
$this->assertTrue($parser->comesNext('**bold**'));
}

#[Test]
public function test_inline_formatting_after_an_unmatched_run(): void
{
$this->assertSame(
'<p>About ~<strong>5 km</strong></p>',
new Markdown(highlighter: null)->parse('About ~**5 km**')->html,
);
}
}
Loading