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: 4 additions & 0 deletions src/Tokens/BoldAndItalicToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Tempest\Markdown\Tokens;

use Tempest\Markdown\Parser;
use Tempest\Markdown\Rules\CodeRule;
use Tempest\Markdown\Rules\ImageRule;
use Tempest\Markdown\Rules\LinkRule;
use Tempest\Markdown\Rules\SocialHandleRule;
use Tempest\Markdown\Rules\StrikethroughRule;
Expand All @@ -20,8 +22,10 @@ public function parse(Parser $parser): string
$content = $parser
->forToken($this, [
new StrikethroughRule(),
new CodeRule(),
new LinkRule(),
new SocialHandleRule(),
new ImageRule(),
new TextRule(),
])
->parse($this->content);
Expand Down
4 changes: 4 additions & 0 deletions src/Tokens/BoldToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Tempest\Markdown\Tokens;

use Tempest\Markdown\Parser;
use Tempest\Markdown\Rules\CodeRule;
use Tempest\Markdown\Rules\ImageRule;
use Tempest\Markdown\Rules\ItalicRule;
use Tempest\Markdown\Rules\LinkRule;
use Tempest\Markdown\Rules\SocialHandleRule;
Expand All @@ -22,8 +24,10 @@ public function parse(Parser $parser): string
->forToken($this, [
new ItalicRule(),
new StrikethroughRule(),
new CodeRule(),
new LinkRule(),
new SocialHandleRule(),
new ImageRule(),
new TextRule(),
])
->parse($this->content);
Expand Down
2 changes: 2 additions & 0 deletions src/Tokens/DivToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Tempest\Markdown\Rules\PreRule;
use Tempest\Markdown\Rules\QuoteRule;
use Tempest\Markdown\Rules\SocialHandleRule;
use Tempest\Markdown\Rules\StrikethroughRule;
use Tempest\Markdown\Rules\TextRule;
use Tempest\Markdown\Token;

Expand All @@ -32,6 +33,7 @@ public function parse(Parser $parser): string
new BoldAndItalicRule(),
new BoldRule(),
new ItalicRule(),
new StrikethroughRule(),
new LinkRule(),
new SocialHandleRule(),
new ImageRule(),
Expand Down
2 changes: 2 additions & 0 deletions src/Tokens/HeadingToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Tempest\Markdown\Rules\BoldAndItalicRule;
use Tempest\Markdown\Rules\BoldRule;
use Tempest\Markdown\Rules\CodeRule;
use Tempest\Markdown\Rules\ImageRule;
use Tempest\Markdown\Rules\ItalicRule;
use Tempest\Markdown\Rules\LinkRule;
use Tempest\Markdown\Rules\SocialHandleRule;
Expand Down Expand Up @@ -39,6 +40,7 @@ public function parse(Parser $parser): string
new StrikethroughRule(),
new LinkRule(),
new SocialHandleRule(),
new ImageRule(),
new CodeRule(),
new TextRule(),
])
Expand Down
4 changes: 4 additions & 0 deletions src/Tokens/ItalicToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Tempest\Markdown\Parser;
use Tempest\Markdown\Rules\BoldRule;
use Tempest\Markdown\Rules\CodeRule;
use Tempest\Markdown\Rules\ImageRule;
use Tempest\Markdown\Rules\LinkRule;
use Tempest\Markdown\Rules\SocialHandleRule;
use Tempest\Markdown\Rules\StrikethroughRule;
Expand All @@ -22,8 +24,10 @@ public function parse(Parser $parser): string
->forToken($this, [
new BoldRule(),
new StrikethroughRule(),
new CodeRule(),
new LinkRule(),
new SocialHandleRule(),
new ImageRule(),
new TextRule(),
])
->parse($this->content);
Expand Down
2 changes: 2 additions & 0 deletions src/Tokens/ListToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Tempest\Markdown\Rules\ItalicRule;
use Tempest\Markdown\Rules\LinkRule;
use Tempest\Markdown\Rules\SocialHandleRule;
use Tempest\Markdown\Rules\StrikethroughRule;
use Tempest\Markdown\Rules\TextRule;
use Tempest\Markdown\Token;

Expand All @@ -26,6 +27,7 @@ public function parse(Parser $parser): string
new BoldAndItalicRule(),
new BoldRule(),
new ItalicRule(),
new StrikethroughRule(),
new LinkRule(),
new SocialHandleRule(),
new ImageRule(),
Expand Down
2 changes: 2 additions & 0 deletions src/Tokens/OrderedListToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Tempest\Markdown\Rules\ItalicRule;
use Tempest\Markdown\Rules\LinkRule;
use Tempest\Markdown\Rules\SocialHandleRule;
use Tempest\Markdown\Rules\StrikethroughRule;
use Tempest\Markdown\Rules\TextRule;
use Tempest\Markdown\Token;

Expand All @@ -26,6 +27,7 @@ public function parse(Parser $parser): string
new BoldAndItalicRule(),
new BoldRule(),
new ItalicRule(),
new StrikethroughRule(),
new LinkRule(),
new SocialHandleRule(),
new ImageRule(),
Expand Down
4 changes: 4 additions & 0 deletions src/Tokens/QuoteToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use Tempest\Markdown\Parser;
use Tempest\Markdown\Rules\BoldAndItalicRule;
use Tempest\Markdown\Rules\BoldRule;
use Tempest\Markdown\Rules\CodeRule;
use Tempest\Markdown\Rules\ImageRule;
use Tempest\Markdown\Rules\ItalicRule;
use Tempest\Markdown\Rules\LinkRule;
use Tempest\Markdown\Rules\QuoteRule;
use Tempest\Markdown\Rules\SocialHandleRule;
use Tempest\Markdown\Rules\StrikethroughRule;
use Tempest\Markdown\Rules\TextRule;
use Tempest\Markdown\Token;

Expand All @@ -26,6 +28,8 @@ public function parse(Parser $parser): string
new BoldAndItalicRule(),
new BoldRule(),
new ItalicRule(),
new StrikethroughRule(),
new CodeRule(),
new QuoteRule(),
new LinkRule(),
new SocialHandleRule(),
Expand Down
4 changes: 4 additions & 0 deletions src/Tokens/StrikethroughToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Tempest\Markdown\Parser;
use Tempest\Markdown\Rules\BoldAndItalicRule;
use Tempest\Markdown\Rules\BoldRule;
use Tempest\Markdown\Rules\CodeRule;
use Tempest\Markdown\Rules\ImageRule;
use Tempest\Markdown\Rules\ItalicRule;
use Tempest\Markdown\Rules\LinkRule;
use Tempest\Markdown\Rules\SocialHandleRule;
Expand All @@ -24,8 +26,10 @@ public function parse(Parser $parser): string
new BoldAndItalicRule(),
new BoldRule(),
new ItalicRule(),
new CodeRule(),
new LinkRule(),
new SocialHandleRule(),
new ImageRule(),
new TextRule(),
])
->parse($this->content);
Expand Down
2 changes: 2 additions & 0 deletions src/Tokens/TableToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Tempest\Markdown\Rules\ItalicRule;
use Tempest\Markdown\Rules\LinkRule;
use Tempest\Markdown\Rules\SocialHandleRule;
use Tempest\Markdown\Rules\StrikethroughRule;
use Tempest\Markdown\Rules\TextRule;
use Tempest\Markdown\Token;

Expand All @@ -26,6 +27,7 @@ public function parse(Parser $parser): string
new BoldAndItalicRule(),
new BoldRule(),
new ItalicRule(),
new StrikethroughRule(),
new LinkRule(),
new SocialHandleRule(),
new CodeRule(),
Expand Down
150 changes: 150 additions & 0 deletions tests/NestedInlineRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

namespace Tempest\Markdown\Tests;

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

/**
* Every token that holds inline content runs the same set of inline rules, so
* the same Markdown renders the same way whichever container it sits in.
*/
final class NestedInlineRuleTest extends ParserTestCase
{
#[Test]
#[DataProvider('provideNestedInline')]
public function inline_rules_apply_inside_every_container(
string $markdown,
string $expected,
): void {
$this->assertSame(
$expected,
trim(new Markdown(highlighter: null)->parse($markdown)->html),
);
}

public static function provideNestedInline(): iterable
{
yield 'code in a paragraph' => [
'`code`',
'<p><code>code</code></p>',
];

yield 'code in a heading' => [
'# `code`',
'<h1 id="code"><code>code</code></h1>',
];

yield 'code in a blockquote' => [
'> `code`',
'<blockquote><code>code</code></blockquote>',
];

yield 'code in bold' => [
'**`code`**',
'<p><strong><code>code</code></strong></p>',
];

yield 'code in italic' => [
'*`code`*',
'<p><em><code>code</code></em></p>',
];

yield 'code in bold italic' => [
'***`code`***',
'<p><strong><em><code>code</code></em></strong></p>',
];

yield 'code in strikethrough' => [
'~~`code`~~',
'<p><s><code>code</code></s></p>',
];

yield 'image in a paragraph' => [
'![alt](img.png)',
'<p><img src="img.png" alt="alt"></p>',
];

yield 'image in a heading' => [
'# ![alt](img.png)',
'<h1 id="alt-img-png"><img src="img.png" alt="alt"></h1>',
];

yield 'image in bold' => [
'**![alt](img.png)**',
'<p><strong><img src="img.png" alt="alt"></strong></p>',
];

yield 'image in italic' => [
'*![alt](img.png)*',
'<p><em><img src="img.png" alt="alt"></em></p>',
];

yield 'image in bold italic' => [
'***![alt](img.png)***',
'<p><strong><em><img src="img.png" alt="alt"></em></strong></p>',
];

yield 'image in strikethrough' => [
'~~![alt](img.png)~~',
'<p><s><img src="img.png" alt="alt"></s></p>',
];

yield 'strikethrough in a paragraph' => [
'~~struck~~',
'<p><s>struck</s></p>',
];

yield 'strikethrough in a list item' => [
'- ~~struck~~',
'<ul><li><s>struck</s></li></ul>',
];

yield 'strikethrough in an ordered list item' => [
'1. ~~struck~~',
'<ol><li><s>struck</s></li></ol>',
];

yield 'strikethrough in a table cell' => [
"| h |\n| - |\n| ~~struck~~ |",
'<table><thead><tr><th>h</th></tr></thead>'
. '<tbody><tr><td><s>struck</s></td></tr></tbody></table>',
];

yield 'strikethrough in a blockquote' => [
'> ~~struck~~',
'<blockquote><s>struck</s></blockquote>',
];

yield 'strikethrough in a div' => [
":::\n~~struck~~\n:::\n",
"<div><s>struck</s>\n</div>",
];
}

#[Test]
#[DataProvider('provideSelfNesting')]
public function a_rule_does_not_nest_in_the_token_it_produces(
string $markdown,
string $expected,
): void {
$this->assertSame(
$expected,
trim(new Markdown(highlighter: null)->parse($markdown)->html),
);
}

public static function provideSelfNesting(): iterable
{
yield 'a link does not nest in a link' => [
'[a [b](https://b.test) c](https://a.test)',
'<p><a href="https://a.test">a [b](https://b.test) c</a></p>',
];

yield 'a social handle does not nest in a link' => [
'[{x:tempestphp}](https://a.test)',
'<p><a href="https://a.test">{x:tempestphp}</a></p>',
];
}
}
Loading