Skip to content

Commit

Permalink
Implement the initial dynamic markdown link feature
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Jul 24, 2024
1 parent 47bfef5 commit 9a08865
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
'/<a href="hyde::route\(\'([^\']+)\'\)"/' => function ($matches) {
return '<a href="'.Hyde::route($matches[1]).'"';
},
'/<a href="hyde::relativeLink\(\'([^\']+)\'\)"/' => function ($matches) {
return '<a href="'.Hyde::relativeLink($matches[1]).'"';
},
'/<img src="hyde::asset\(\'([^\']+)\'\)"/' => function ($matches) {
return '<img src="'.Hyde::asset($matches[1]).'"';
},
];

foreach ($patterns as $pattern => $replacement) {
$html = preg_replace_callback($pattern, $replacement, $html);
}

return $html;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<p><a href="hyde::route(\'home\')">Home</a></p>';
$expected = '<p><a href="home.html">Home</a></p>';
$this->assertEquals($expected, DynamicMarkdownLinkProcessor::postprocess($input));
}

public function testRelativeLinkReplacement()
{
$input = '<p><a href="hyde::relativeLink(\'about\')">About</a></p>';
$expected = '<p><a href="about">About</a></p>';
$this->assertEquals($expected, DynamicMarkdownLinkProcessor::postprocess($input));
}

public function testAssetReplacement()
{
$input = '<p><img src="hyde::asset(\'image.jpg\')" alt="Image" /></p>';
$expected = '<p><img src="media/image.jpg" alt="Image" /></p>';
$this->assertEquals($expected, DynamicMarkdownLinkProcessor::postprocess($input));
}

public function testMultipleReplacements()
{
$input = '<p><a href="hyde::route(\'home\')">Home</a> <a href="hyde::relativeLink(\'about\')">About</a> <img src="hyde::asset(\'logo.png\')" alt="Logo" /></p>';
$expected = '<p><a href="home.html">Home</a> <a href="about">About</a> <img src="media/logo.png" alt="Logo" /></p>';
$this->assertEquals($expected, DynamicMarkdownLinkProcessor::postprocess($input));
}

public function testNoReplacements()
{
$input = '<p>This is a regular <a href="https://example.com">link</a> with no Hyde syntax.</p>';
$this->assertEquals($input, DynamicMarkdownLinkProcessor::postprocess($input));
}
}

0 comments on commit 9a08865

Please sign in to comment.