From 4af22ec41a13fb7bbaddf6fce2ef2efca17c6afa Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Fri, 15 Oct 2021 19:25:47 +0100 Subject: [PATCH] Allow substitution of blocks and inlines --- src/Configurables/BlockTypes.php | 39 +++++++++++++++++++++ src/Configurables/InlineTypes.php | 29 +++++++++++++++ tests/src/Configurables/BlockTypesTest.php | 5 +++ tests/src/Configurables/InlineTypesTest.php | 4 +++ 4 files changed, 77 insertions(+) diff --git a/src/Configurables/BlockTypes.php b/src/Configurables/BlockTypes.php index 398aa84..0631866 100644 --- a/src/Configurables/BlockTypes.php +++ b/src/Configurables/BlockTypes.php @@ -101,6 +101,45 @@ final class BlockTypes implements Configurable ); } + /** + * @param class-string $searchBlockType + * @param class-string $replacementBlockType + */ + public function replacing($searchBlockType, $replacementBlockType): self + { + $replacer = self::makeReplacer($searchBlockType, $replacementBlockType); + + return new self( + \array_map($replacer, $this->blockTypes), + $replacer($this->unmarkedBlockTypes) + ); + } + + /** + * @param class-string $searchBlockType + * @param class-string $replacementBlockType + * @return \Closure(list>):list> + */ + private static function makeReplacer($searchBlockType, $replacementBlockType) + { + /** + * @param list> $blockTypes + * @return list> + */ + return function ($blockTypes) use ($searchBlockType, $replacementBlockType) { + return \array_map( + /** + * @param class-string $blockType + * @return class-string + */ + function ($blockType) use ($searchBlockType, $replacementBlockType) { + return $blockType === $searchBlockType ? $replacementBlockType : $blockType; + }, + $blockTypes + ); + }; + } + /** * @param string $marker * @param list> $newBlockTypes diff --git a/src/Configurables/InlineTypes.php b/src/Configurables/InlineTypes.php index 483fd0c..fad7819 100644 --- a/src/Configurables/InlineTypes.php +++ b/src/Configurables/InlineTypes.php @@ -68,6 +68,35 @@ final class InlineTypes implements Configurable return new self($inlineTypes); } + /** + * @param class-string $searchInlineType + * @param class-string $replacementInlineType + */ + public function replacing($searchInlineType, $replacementInlineType): self + { + return new self( + \array_map( + /** + * @param list> $inlineTypes + * @return list> + */ + function ($inlineTypes) use ($searchInlineType, $replacementInlineType) { + return \array_map( + /** + * @param class-string $inlineType + * @return class-string + */ + function ($inlineType) use ($searchInlineType, $replacementInlineType) { + return $inlineType === $searchInlineType ? $replacementInlineType : $inlineType; + }, + $inlineTypes + ); + }, + $this->inlineTypes + ) + ); + } + /** * @param string $marker * @param list> $newInlineTypes diff --git a/tests/src/Configurables/BlockTypesTest.php b/tests/src/Configurables/BlockTypesTest.php index 261b82c..04f9e54 100644 --- a/tests/src/Configurables/BlockTypesTest.php +++ b/tests/src/Configurables/BlockTypesTest.php @@ -2,6 +2,7 @@ namespace Erusev\Parsedown\Tests\Configurables; +use Erusev\Parsedown\Components\Blocks\FencedCode; use Erusev\Parsedown\Components\Blocks\IndentedCode; use Erusev\Parsedown\Components\Blocks\Markup; use Erusev\Parsedown\Components\Blocks\Rule; @@ -41,5 +42,9 @@ final class BlockTypesTest extends TestCase $BlockTypes = $BlockTypes->addingUnmarkedLowPrecedence([IndentedCode::class]); $this->assertSame([Markup::class, IndentedCode::class, Rule::class], $BlockTypes->markedBy('@')); $this->assertSame([Rule::class, Markup::class, IndentedCode::class], $BlockTypes->unmarked()); + + $BlockTypes = $BlockTypes->replacing(IndentedCode::class, FencedCode::class); + $this->assertSame([Markup::class, FencedCode::class, Rule::class], $BlockTypes->markedBy('@')); + $this->assertSame([Rule::class, Markup::class, FencedCode::class], $BlockTypes->unmarked()); } } diff --git a/tests/src/Configurables/InlineTypesTest.php b/tests/src/Configurables/InlineTypesTest.php index 604fd4d..6b92402 100644 --- a/tests/src/Configurables/InlineTypesTest.php +++ b/tests/src/Configurables/InlineTypesTest.php @@ -5,6 +5,7 @@ namespace Erusev\Parsedown\Tests\Configurables; use Erusev\Parsedown\Components\Inlines\Code; use Erusev\Parsedown\Components\Inlines\Emphasis; use Erusev\Parsedown\Components\Inlines\Link; +use Erusev\Parsedown\Components\Inlines\Url; use Erusev\Parsedown\Configurables\InlineTypes; use PHPUnit\Framework\TestCase; @@ -28,5 +29,8 @@ final class InlineTypesTest extends TestCase $InlineTypes = $InlineTypes->addingLowPrecedence('@', [Link::class]); $this->assertSame([Code::class, Emphasis::class, Link::class], $InlineTypes->markedBy('@')); + + $InlineTypes = $InlineTypes->replacing(Link::class, Url::class); + $this->assertSame([Code::class, Emphasis::class, Url::class], $InlineTypes->markedBy('@')); } }