From 714e7e1537da05c0e2e2f67c980a24aaaf7b75dc Mon Sep 17 00:00:00 2001 From: brendt Date: Thu, 25 Jun 2026 08:47:51 +0200 Subject: [PATCH 1/2] wip --- src/Parser.php | 2 ++ src/Rules/RawRule.php | 25 ++++++++++++++++ src/Tokens/RawToken.php | 18 ++++++++++++ tests/MarkdownTest.php | 25 ++++++++++++++++ tests/Rules/RawRuleTest.php | 54 +++++++++++++++++++++++++++++++++++ tests/Tokens/RawTokenTest.php | 44 ++++++++++++++++++++++++++++ 6 files changed, 168 insertions(+) create mode 100644 src/Rules/RawRule.php create mode 100644 src/Tokens/RawToken.php create mode 100644 tests/Rules/RawRuleTest.php create mode 100644 tests/Tokens/RawTokenTest.php diff --git a/src/Parser.php b/src/Parser.php index c657224..3a524fc 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -14,6 +14,7 @@ use Tempest\Markdown\Rules\ParagraphRule; use Tempest\Markdown\Rules\PreRule; use Tempest\Markdown\Rules\QuoteRule; +use Tempest\Markdown\Rules\RawRule; use Tempest\Markdown\Rules\TableRule; use Tempest\Markdown\Rules\ThickRulerRule; use Tempest\Markdown\Rules\ThinRulerRule; @@ -45,6 +46,7 @@ public function __construct( public ?ResponsiveImageFactory $imageFactory = null, array $rules = [ new NewLineRule(), + new RawRule(), new FrontMatterRule(), new HeadingRule(), new QuoteRule(), diff --git a/src/Rules/RawRule.php b/src/Rules/RawRule.php new file mode 100644 index 0000000..adfe4b6 --- /dev/null +++ b/src/Rules/RawRule.php @@ -0,0 +1,25 @@ +comesNext('@@', length: 2); + } + + public function parse(Parser $parser): ?Token + { + $parser->consume(2); + $content = $parser->consumeUntil('@@'); + $parser->consume(2); + + return new RawToken($content); + } +} \ No newline at end of file diff --git a/src/Tokens/RawToken.php b/src/Tokens/RawToken.php new file mode 100644 index 0000000..f56a549 --- /dev/null +++ b/src/Tokens/RawToken.php @@ -0,0 +1,18 @@ +content; + } +} \ No newline at end of file diff --git a/tests/MarkdownTest.php b/tests/MarkdownTest.php index a9fe63a..91180dd 100644 --- a/tests/MarkdownTest.php +++ b/tests/MarkdownTest.php @@ -271,4 +271,29 @@ public function test_remove_rules_removes_rule(): void $this->assertStringNotContainsString('html); } + + #[Test] + public function test_raw(): void + { + $parsed = (string) $this->markdown + ->parse(<<<'MD' + ## Hello + + @@ + ### World + @@ + + _hi_ + MD); + + $this->assertSame(<<Hello + + + ### World + + +

hi

+ HTML, $parsed); + } } diff --git a/tests/Rules/RawRuleTest.php b/tests/Rules/RawRuleTest.php new file mode 100644 index 0000000..4a3966e --- /dev/null +++ b/tests/Rules/RawRuleTest.php @@ -0,0 +1,54 @@ +parse('@@raw@@'); + + $this->assertSame('raw', $html); + } + + #[Test] + public function test_raw_html_is_not_escaped(): void + { + $html = (string) new Parser(highlighter: null, rules: [new RawRule()])->parse('@@@@'); + + $this->assertSame('', $html); + } + + #[Test] + public function test_raw_inline_with_surrounding_text(): void + { + $html = (string) new Parser(highlighter: null, rules: [new RawRule(), new TextRule()])->parse('Hello @@world@@ end'); + + $this->assertSame('Hello world end', $html); + } + + #[Test] + public function test_multiple_raw_blocks(): void + { + $html = (string) new Parser(highlighter: null, rules: [new RawRule(), new TextRule()])->parse('@@foo@@ and @@bar@@'); + + $this->assertSame('foo and bar', $html); + } + + #[Test] + public function test_raw_multiline_content(): void + { + $input = "@@line1\nline2@@"; + + $html = (string) new Parser(highlighter: null, rules: [new RawRule()])->parse($input); + + $this->assertSame("line1\nline2", $html); + } +} diff --git a/tests/Tokens/RawTokenTest.php b/tests/Tokens/RawTokenTest.php new file mode 100644 index 0000000..d6537d6 --- /dev/null +++ b/tests/Tokens/RawTokenTest.php @@ -0,0 +1,44 @@ +raw'); + + $this->assertSame('raw', $token->parse(new Parser())); + } + + #[Test] + public function test_parse_multiline(): void + { + $content = "line1\nline2"; + $token = new RawToken($content); + + $this->assertSame($content, $token->parse(new Parser())); + } + + #[Test] + public function test_parse_does_not_process_markdown(): void + { + $token = new RawToken('**bold** _italic_'); + + $this->assertSame('**bold** _italic_', $token->parse(new Parser())); + } + + #[Test] + public function test_parse_empty_string(): void + { + $token = new RawToken(''); + + $this->assertSame('', $token->parse(new Parser())); + } +} From 82f23bea5f6aa6965d2fd8a023c9ac43876ee6ce Mon Sep 17 00:00:00 2001 From: brendt Date: Thu, 25 Jun 2026 08:47:58 +0200 Subject: [PATCH 2/2] wip --- src/Rules/RawRule.php | 2 +- src/Tokens/RawToken.php | 2 +- tests/MarkdownTest.php | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Rules/RawRule.php b/src/Rules/RawRule.php index adfe4b6..bc1ed13 100644 --- a/src/Rules/RawRule.php +++ b/src/Rules/RawRule.php @@ -22,4 +22,4 @@ public function parse(Parser $parser): ?Token return new RawToken($content); } -} \ No newline at end of file +} diff --git a/src/Tokens/RawToken.php b/src/Tokens/RawToken.php index f56a549..ac0329e 100644 --- a/src/Tokens/RawToken.php +++ b/src/Tokens/RawToken.php @@ -15,4 +15,4 @@ public function parse(Parser $parser): string { return $this->content; } -} \ No newline at end of file +} diff --git a/tests/MarkdownTest.php b/tests/MarkdownTest.php index 91180dd..fcdc618 100644 --- a/tests/MarkdownTest.php +++ b/tests/MarkdownTest.php @@ -278,21 +278,21 @@ public function test_raw(): void $parsed = (string) $this->markdown ->parse(<<<'MD' ## Hello - + @@ ### World @@ - + _hi_ MD); $this->assertSame(<<Hello - - + + ### World - - + +

hi

HTML, $parsed); }