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
21 changes: 17 additions & 4 deletions src/Rules/BoldAndItalicRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,28 @@ final class BoldAndItalicRule implements
ProvidesFirstChar,
ProvidesStopChar
{
use ChecksFlanking;

private(set) string $firstChar = '*_';
private(set) string $stopChar = '_*';

public function shouldParse(Parser $parser): bool
{
return (
$parser->comesNext('***', length: 3)
|| $parser->comesNext('___', length: 3)
);
if (
! $parser->comesNext('***', length: 3)
&& ! $parser->comesNext('___', length: 3)
) {
return false;
}

$start = $parser->position + 3;
$end = strpos($parser->content, $parser->current ?? '', $start);

return $this->isFlanked(substr(
$parser->content,
$start,
$end === false ? null : $end - $start,
));
}

public function parse(Parser $parser): Token
Expand Down
4 changes: 3 additions & 1 deletion src/Rules/BoldRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

final class BoldRule implements Rule, ProvidesFirstChar, ProvidesStopChar
{
use ChecksFlanking;

private(set) string $firstChar = '*_';
private(set) string $stopChar = '*_';

Expand Down Expand Up @@ -49,7 +51,7 @@ public function shouldParse(Parser $parser): bool
return false;
}

return true;
return $this->isFlanked(substr($content, 0, -1));
}

public function parse(Parser $parser): Token
Expand Down
23 changes: 23 additions & 0 deletions src/Rules/ChecksFlanking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Tempest\Markdown\Rules;

use Tempest\Markdown\Parser;

trait ChecksFlanking
{
/**
* An opening delimiter may not be followed by whitespace, nor a closing
* one preceded by it.
*
* @see https://spec.commonmark.org/0.31.2/#left-flanking-delimiter-run
*/
private function isFlanked(string $content): bool
{
return (
$content !== ''
&& ! str_contains(Parser::WHITESPACE, $content[0])
&& ! str_contains(Parser::WHITESPACE, $content[-1])
);
}
}
4 changes: 3 additions & 1 deletion src/Rules/ItalicRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

final class ItalicRule implements Rule, ProvidesFirstChar, ProvidesStopChar
{
use ChecksFlanking;

private(set) string $firstChar = '*_';
private(set) string $stopChar = '*_';

Expand Down Expand Up @@ -41,7 +43,7 @@ public function shouldParse(Parser $parser): bool
return false;
}

return true;
return $this->isFlanked(substr($end, 0, -1));
}

public function parse(Parser $parser): Token
Expand Down
66 changes: 66 additions & 0 deletions tests/EmphasisFlankingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Tempest\Markdown\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Tempest\Markdown\Markdown;

final class EmphasisFlankingTest extends ParserTestCase
{
#[Test]
#[DataProvider('provideFlanking')]
public function delimiters_follow_the_flanking_rules(
string $markdown,
string $expected,
): void {
$this->assertSame(
$expected,
new Markdown(highlighter: null)->parse($markdown)->html,
);
}

public static function provideFlanking(): iterable
{
yield 'italic' => ['*a*', '<p><em>a</em></p>'];
yield 'bold' => ['**a**', '<p><strong>a</strong></p>'];
yield 'bold and italic' => [
'***a***',
'<p><strong><em>a</em></strong></p>',
];
yield 'underscore italic' => ['_a_', '<p><em>a</em></p>'];
yield 'underscore bold' => ['__a__', '<p><strong>a</strong></p>'];
yield 'inside a word' => ['foo*bar*', '<p>foo<em>bar</em></p>'];

// An opening delimiter may not be followed by whitespace, and a
// closing one may not be preceded by it.
yield 'spaced asterisks' => ['2 * 3 * 4', '<p>2 * 3 * 4</p>'];
yield 'spaced double asterisks' => ['a ** b **', '<p>a ** b **</p>'];
yield 'spaced triple asterisks' => [
'a *** b ***',
'<p>a *** b ***</p>',
];
yield 'spaced underscores' => ['x _ y _', '<p>x _ y _</p>'];
yield 'space before the closing asterisk' => ['*a *', '<p>*a *</p>'];
yield 'space before the closing double asterisk' => [
'**a **',
'<p>**a **</p>',
];
yield 'space after the opening double asterisk' => [
'** a**',
'<p>** a**</p>',
];
yield 'space before the closing double underscore' => [
'__a __',
'<p>__a __</p>',
];
yield 'hotel ratings' => [
'Hôtels 3*** et 4****',
'<p>Hôtels 3*** et 4****</p>',
];
yield 'literal run after emphasis' => [
'*a* * b *',
'<p><em>a</em> * b *</p>',
];
}
}
Loading