Skip to content

[5.x] Added the option to add renderers to markdown parsers #11827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions src/Markdown/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Parser

protected $converter;
protected $extensions = [];
protected $renderers = [];
protected $config = [];

public function __construct(array $config = [])
Expand All @@ -41,6 +42,10 @@ public function converter(): CommonMarkConverter
$env->addExtension($ext);
}

foreach ($this->renderers() as $ren) {
$env->addRenderer(...$ren);
}

return $this->converter = $converter;
}

Expand All @@ -58,6 +63,15 @@ public function addExtension(Closure $closure): self
return $this;
}

public function addRenderers(Closure $closure): self
{
$this->converter = null;

$this->renderers[] = $closure;

return $this;
}

public function addExtensions(Closure $closure): self
{
return $this->addExtension($closure);
Expand All @@ -76,6 +90,19 @@ public function extensions(): array
return $exts;
}

public function renderers(): array
{
$renderers = [];

foreach ($this->renderers as $closure) {
foreach ($closure() as $renderer) {
$renderers[] = $renderer;
}
}

return $renderers;
}

public function withStatamicDefaults()
{
return $this->newInstance()->addExtensions(function () {
Expand Down Expand Up @@ -151,6 +178,10 @@ public function newInstance(array $config = [])
$parser->addExtensions($ext);
}

foreach ($this->renderers as $ren) {
$parser->addRenderers($ren);
}

return $parser;
}
}
20 changes: 20 additions & 0 deletions tests/Markdown/Fixtures/HeadingRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Tests\Markdown\Fixtures;

use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;

class HeadingRenderer implements NodeRendererInterface
{
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
if (! ($node instanceof Heading)) {
throw new \InvalidArgumentException('Incompatible node type: '.get_class($node));
}

return "<h1 data-custom-renderer>{$childRenderer->renderNodes($node->children())}</h1>";
}
}
20 changes: 20 additions & 0 deletions tests/Markdown/Fixtures/LinkRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Tests\Markdown\Fixtures;

use League\CommonMark\Extension\CommonMark\Node\Inline\Link as InlineLink;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;

class LinkRenderer implements NodeRendererInterface
{
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
if (! ($node instanceof InlineLink)) {
throw new \InvalidArgumentException('Incompatible node type: '.get_class($node));
}

return "<a data-custom-renderer href=\"{$node->getUrl()}\" title=\"{$node->getTitle()}\">{$childRenderer->renderNodes($node->children())}</a>";
}
}
30 changes: 30 additions & 0 deletions tests/Markdown/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Tests\Markdown;

use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Markdown;
use Tests\TestCase;
Expand Down Expand Up @@ -54,6 +56,12 @@ public function it_creates_a_new_instance_based_on_the_current_instance()
return new Fixtures\SmileyExtension;
});

$this->parser->addRenderers(function () {
return [
[Link::class, new Fixtures\LinkRenderer],
];
});

$this->assertEquals("\n", $this->parser->config('renderer/block_separator'));
$this->assertEquals("\n", $this->parser->config('renderer/inner_separator'));
$this->assertEquals('allow', $this->parser->config('html_input'));
Expand All @@ -71,11 +79,33 @@ public function it_creates_a_new_instance_based_on_the_current_instance()
return new Fixtures\FrownyExtension;
});

$newParser->addRenderers(function () {
return [
[Heading::class, new Fixtures\HeadingRenderer],
];
});

$this->assertNotSame($this->parser, $newParser);
$this->assertEquals("\n", $newParser->config('renderer/block_separator'));
$this->assertEquals('foo', $newParser->config('renderer/inner_separator'));
$this->assertEquals('strip', $newParser->config('html_input'));
$this->assertCount(2, $newParser->extensions());
$this->assertCount(1, $this->parser->extensions());
$this->assertCount(2, $newParser->renderers());
$this->assertCount(1, $this->parser->renderers());
}

#[Test]
public function it_uses_custom_renderers()
{
$this->assertEquals("<p><a href=\"http://example.com\">test</a></p>\n", $this->parser->parse('[test](http://example.com)'));

$this->parser->addRenderers(function () {
return [
[Link::class, new Fixtures\LinkRenderer],
];
});

$this->assertEquals("<p><a data-custom-renderer href=\"http://example.com\" title=\"\">test</a></p>\n", $this->parser->parse('[test](http://example.com)'));
}
}
Loading