Skip to content

Commit

Permalink
Merge pull request #54 from erikn69/patch-6
Browse files Browse the repository at this point in the history
Support embeds class names for config serialization
  • Loading branch information
freekmurze authored May 5, 2023
2 parents 50a32cd + 4ae3ecf commit 1cdde7a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/MarkdownRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,17 @@ protected function configureCommonMarkEnvironment(EnvironmentBuilderInterface $e

private function getMarkdownConverter(): MarkdownConverter
{
$environment = new Environment($this->commonmarkOptions);
$commonmarkOptions = $this->commonmarkOptions;

if (isset($commonmarkOptions['embed'])) {
$commonmarkOptions['embed']['adapter'] = $this->getClassInstance($commonmarkOptions['embed']['adapter']);
}

foreach ($commonmarkOptions['embeds'] ?? [] as $i => $embed) {
$commonmarkOptions['embeds'][$i] = $this->getClassInstance($embed);
}

$environment = new Environment($commonmarkOptions);
$this->configureCommonMarkEnvironment($environment);

return new MarkdownConverter(
Expand Down
26 changes: 26 additions & 0 deletions tests/MarkdownRendererSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,32 @@ function getProtectedPropertyValue(object $object, string $propertyName): mixed
expect($priorityArray[42][0])->toBeInstanceOf(InlineTextDividerRenderer::class);
});

it('can register embed renderers', function () {
config()->set('markdown.commonmark_options', [
'embed' => ['adapter' => new TextDividerRenderer()],
'embeds' => [ new TextDividerRenderer()],
]);

$config = getProtectedPropertyValue(markdownConverter()->getEnvironment(), 'config');
$configArray = getProtectedPropertyValue($config, 'userConfig');

expect($configArray['embed']['adapter'])->toBeInstanceOf(TextDividerRenderer::class);
expect($configArray['embeds'][0])->toBeInstanceOf(TextDividerRenderer::class);
});

it('can register embed renderers class', function () {
config()->set('markdown.commonmark_options', [
'embed' => ['adapter' => TextDividerRenderer::class],
'embeds' => [TextDividerRenderer::class],
]);

$config = getProtectedPropertyValue(markdownConverter()->getEnvironment(), 'config');
$configArray = getProtectedPropertyValue($config, 'userConfig');

expect($configArray['embed']['adapter'])->toBeInstanceOf(TextDividerRenderer::class);
expect($configArray['embeds'][0])->toBeInstanceOf(TextDividerRenderer::class);
});

it('can dynamically register renderers', function () {
$markdownRenderer = markdownRenderer();
$markdownRenderer = $markdownRenderer->addBlockRenderer(ThematicBreak::class, new TextDividerRenderer(), 42);
Expand Down

0 comments on commit 1cdde7a

Please sign in to comment.