mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Allow substitution of blocks and inlines
This commit is contained in:
parent
4553515e8e
commit
4af22ec41a
@ -101,6 +101,45 @@ final class BlockTypes implements Configurable
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param class-string<Block> $searchBlockType
|
||||||
|
* @param class-string<Block> $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<Block> $searchBlockType
|
||||||
|
* @param class-string<Block> $replacementBlockType
|
||||||
|
* @return \Closure(list<class-string<Block>>):list<class-string<Block>>
|
||||||
|
*/
|
||||||
|
private static function makeReplacer($searchBlockType, $replacementBlockType)
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param list<class-string<Block>> $blockTypes
|
||||||
|
* @return list<class-string<Block>>
|
||||||
|
*/
|
||||||
|
return function ($blockTypes) use ($searchBlockType, $replacementBlockType) {
|
||||||
|
return \array_map(
|
||||||
|
/**
|
||||||
|
* @param class-string<Block> $blockType
|
||||||
|
* @return class-string<Block>
|
||||||
|
*/
|
||||||
|
function ($blockType) use ($searchBlockType, $replacementBlockType) {
|
||||||
|
return $blockType === $searchBlockType ? $replacementBlockType : $blockType;
|
||||||
|
},
|
||||||
|
$blockTypes
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $marker
|
* @param string $marker
|
||||||
* @param list<class-string<Block>> $newBlockTypes
|
* @param list<class-string<Block>> $newBlockTypes
|
||||||
|
@ -68,6 +68,35 @@ final class InlineTypes implements Configurable
|
|||||||
return new self($inlineTypes);
|
return new self($inlineTypes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param class-string<Inline> $searchInlineType
|
||||||
|
* @param class-string<Inline> $replacementInlineType
|
||||||
|
*/
|
||||||
|
public function replacing($searchInlineType, $replacementInlineType): self
|
||||||
|
{
|
||||||
|
return new self(
|
||||||
|
\array_map(
|
||||||
|
/**
|
||||||
|
* @param list<class-string<Inline>> $inlineTypes
|
||||||
|
* @return list<class-string<Inline>>
|
||||||
|
*/
|
||||||
|
function ($inlineTypes) use ($searchInlineType, $replacementInlineType) {
|
||||||
|
return \array_map(
|
||||||
|
/**
|
||||||
|
* @param class-string<Inline> $inlineType
|
||||||
|
* @return class-string<Inline>
|
||||||
|
*/
|
||||||
|
function ($inlineType) use ($searchInlineType, $replacementInlineType) {
|
||||||
|
return $inlineType === $searchInlineType ? $replacementInlineType : $inlineType;
|
||||||
|
},
|
||||||
|
$inlineTypes
|
||||||
|
);
|
||||||
|
},
|
||||||
|
$this->inlineTypes
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $marker
|
* @param string $marker
|
||||||
* @param list<class-string<Inline>> $newInlineTypes
|
* @param list<class-string<Inline>> $newInlineTypes
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Erusev\Parsedown\Tests\Configurables;
|
namespace Erusev\Parsedown\Tests\Configurables;
|
||||||
|
|
||||||
|
use Erusev\Parsedown\Components\Blocks\FencedCode;
|
||||||
use Erusev\Parsedown\Components\Blocks\IndentedCode;
|
use Erusev\Parsedown\Components\Blocks\IndentedCode;
|
||||||
use Erusev\Parsedown\Components\Blocks\Markup;
|
use Erusev\Parsedown\Components\Blocks\Markup;
|
||||||
use Erusev\Parsedown\Components\Blocks\Rule;
|
use Erusev\Parsedown\Components\Blocks\Rule;
|
||||||
@ -41,5 +42,9 @@ final class BlockTypesTest extends TestCase
|
|||||||
$BlockTypes = $BlockTypes->addingUnmarkedLowPrecedence([IndentedCode::class]);
|
$BlockTypes = $BlockTypes->addingUnmarkedLowPrecedence([IndentedCode::class]);
|
||||||
$this->assertSame([Markup::class, IndentedCode::class, Rule::class], $BlockTypes->markedBy('@'));
|
$this->assertSame([Markup::class, IndentedCode::class, Rule::class], $BlockTypes->markedBy('@'));
|
||||||
$this->assertSame([Rule::class, Markup::class, IndentedCode::class], $BlockTypes->unmarked());
|
$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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ namespace Erusev\Parsedown\Tests\Configurables;
|
|||||||
use Erusev\Parsedown\Components\Inlines\Code;
|
use Erusev\Parsedown\Components\Inlines\Code;
|
||||||
use Erusev\Parsedown\Components\Inlines\Emphasis;
|
use Erusev\Parsedown\Components\Inlines\Emphasis;
|
||||||
use Erusev\Parsedown\Components\Inlines\Link;
|
use Erusev\Parsedown\Components\Inlines\Link;
|
||||||
|
use Erusev\Parsedown\Components\Inlines\Url;
|
||||||
use Erusev\Parsedown\Configurables\InlineTypes;
|
use Erusev\Parsedown\Configurables\InlineTypes;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
@ -28,5 +29,8 @@ final class InlineTypesTest extends TestCase
|
|||||||
|
|
||||||
$InlineTypes = $InlineTypes->addingLowPrecedence('@', [Link::class]);
|
$InlineTypes = $InlineTypes->addingLowPrecedence('@', [Link::class]);
|
||||||
$this->assertSame([Code::class, Emphasis::class, Link::class], $InlineTypes->markedBy('@'));
|
$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('@'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user