From 9a08865586e21def88514c4ad64e2ae007e3e09b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 24 Jul 2024 17:33:23 +0200 Subject: [PATCH] Implement the initial dynamic markdown link feature --- .../DynamicMarkdownLinkProcessor.php | 19 ++++++- .../DynamicMarkdownLinkProcessorTest.php | 52 ++++++++++++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Markdown/Processing/DynamicMarkdownLinkProcessor.php b/packages/framework/src/Markdown/Processing/DynamicMarkdownLinkProcessor.php index a47642414a4..3b17b28b5fc 100644 --- a/packages/framework/src/Markdown/Processing/DynamicMarkdownLinkProcessor.php +++ b/packages/framework/src/Markdown/Processing/DynamicMarkdownLinkProcessor.php @@ -4,12 +4,29 @@ namespace Hyde\Markdown\Processing; +use Hyde\Hyde; use Hyde\Markdown\Contracts\MarkdownPostProcessorContract; class DynamicMarkdownLinkProcessor implements MarkdownPostProcessorContract { public static function postprocess(string $html): string { - // TODO: Implement postprocess() method. + $patterns = [ + '/ function ($matches) { + return ' function ($matches) { + return ' function ($matches) { + return ' $replacement) { + $html = preg_replace_callback($pattern, $replacement, $html); + } + + return $html; } } diff --git a/packages/framework/tests/Feature/Services/Markdown/DynamicMarkdownLinkProcessorTest.php b/packages/framework/tests/Feature/Services/Markdown/DynamicMarkdownLinkProcessorTest.php index 4fc7c0e2f1a..c6e42bcff2a 100644 --- a/packages/framework/tests/Feature/Services/Markdown/DynamicMarkdownLinkProcessorTest.php +++ b/packages/framework/tests/Feature/Services/Markdown/DynamicMarkdownLinkProcessorTest.php @@ -4,12 +4,62 @@ namespace Hyde\Framework\Testing\Feature\Services\Markdown; +use Hyde\Pages\InMemoryPage; use Hyde\Testing\UnitTestCase; +use Hyde\Support\Models\Route; +use Hyde\Support\Facades\Render; +use Hyde\Foundation\Facades\Routes; +use Hyde\Support\Models\RenderData; +use Hyde\Markdown\Processing\DynamicMarkdownLinkProcessor; /** * @covers \Hyde\Markdown\Processing\DynamicMarkdownLinkProcessor */ class DynamicMarkdownLinkProcessorTest extends UnitTestCase { - // + protected static bool $needsConfig = true; + protected static bool $needsKernel = true; + + protected function setUp(): void + { + parent::setUp(); + + Render::swap(new RenderData()); + + Routes::addRoute(new Route(new InMemoryPage('home'))); + } + + public function testRouteReplacement() + { + $input = '

Home

'; + $expected = '

Home

'; + $this->assertEquals($expected, DynamicMarkdownLinkProcessor::postprocess($input)); + } + + public function testRelativeLinkReplacement() + { + $input = '

About

'; + $expected = '

About

'; + $this->assertEquals($expected, DynamicMarkdownLinkProcessor::postprocess($input)); + } + + public function testAssetReplacement() + { + $input = '

Image

'; + $expected = '

Image

'; + $this->assertEquals($expected, DynamicMarkdownLinkProcessor::postprocess($input)); + } + + public function testMultipleReplacements() + { + $input = '

Home About Logo

'; + $expected = '

Home About Logo

'; + $this->assertEquals($expected, DynamicMarkdownLinkProcessor::postprocess($input)); + } + + public function testNoReplacements() + { + $input = '

This is a regular link with no Hyde syntax.

'; + $this->assertEquals($input, DynamicMarkdownLinkProcessor::postprocess($input)); + } }