1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

Add easy way to remove Components from InlineTyes and BlockTypes

This commit is contained in:
Aidan Woods 2019-01-25 20:54:25 +00:00
parent 9f9ef78662
commit 8fe93f30ac
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
5 changed files with 50 additions and 2 deletions

View File

@ -30,6 +30,7 @@
<referencedMethod name="Erusev\Parsedown\Html\Renderables\Container::__construct" /> <referencedMethod name="Erusev\Parsedown\Html\Renderables\Container::__construct" />
<referencedMethod name="Erusev\Parsedown\Configurables\BlockTypes::settingMarked" /> <referencedMethod name="Erusev\Parsedown\Configurables\BlockTypes::settingMarked" />
<referencedMethod name="Erusev\Parsedown\Configurables\BlockTypes::settingUnmarked" /> <referencedMethod name="Erusev\Parsedown\Configurables\BlockTypes::settingUnmarked" />
<referencedMethod name="Erusev\Parsedown\Configurables\BlockTypes::removing" />
</errorLevel> </errorLevel>
</PossiblyUnusedMethod> </PossiblyUnusedMethod>
</issueHandlers> </issueHandlers>

View File

@ -6,6 +6,7 @@ use Erusev\Parsedown\AST\Handler;
use Erusev\Parsedown\AST\StateRenderable; use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\Inline; use Erusev\Parsedown\Components\Inline;
use Erusev\Parsedown\Configurables\DefinitionBook; use Erusev\Parsedown\Configurables\DefinitionBook;
use Erusev\Parsedown\Configurables\InlineTypes;
use Erusev\Parsedown\Configurables\SafeMode; use Erusev\Parsedown\Configurables\SafeMode;
use Erusev\Parsedown\Html\Renderables\Element; use Erusev\Parsedown\Html\Renderables\Element;
use Erusev\Parsedown\Html\Renderables\Text; use Erusev\Parsedown\Html\Renderables\Text;
@ -126,10 +127,14 @@ final class Link implements Inline
$attributes['href'] = Element::filterUnsafeUrl($attributes['href']); $attributes['href'] = Element::filterUnsafeUrl($attributes['href']);
} }
$NewState = $State->setting(
$State->get(InlineTypes::class)->removing([Url::class])
);
return new Element( return new Element(
'a', 'a',
$attributes, $attributes,
$State->applyTo($Parsedown->line($this->label)) $State->applyTo((new Parsedown($NewState))->line($this->label))
); );
} }
); );

View File

@ -100,6 +100,27 @@ final class BlockTypes implements Configurable
return new self($this->blockTypes, $newUnmarkedBlockTypes); return new self($this->blockTypes, $newUnmarkedBlockTypes);
} }
/**
* @param class-string<Block>[] $removeBlockTypes
* @return self
*/
public function removing(array $removeBlockTypes)
{
return new self(
\array_map(
/**
* @param class-string<Block>[] $blockTypes
* @return class-string<Block>[]
*/
function ($blockTypes) use ($removeBlockTypes) {
return \array_diff($blockTypes, $removeBlockTypes);
},
$this->blockTypes
),
\array_diff($this->unmarkedBlockTypes, $removeBlockTypes)
);
}
/** /**
* @param string $marker * @param string $marker
* @return class-string<Block>[] * @return class-string<Block>[]

View File

@ -59,6 +59,24 @@ final class InlineTypes implements Configurable
return new self(self::$defaultInlineTypes); return new self(self::$defaultInlineTypes);
} }
/**
* @param class-string<Inline>[] $removeInlineTypes
* @return self
*/
public function removing(array $removeInlineTypes)
{
return new self(\array_map(
/**
* @param class-string<Inline>[] $inlineTypes
* @return class-string<Inline>[]
*/
function ($inlineTypes) use ($removeInlineTypes) {
return \array_diff($inlineTypes, $removeInlineTypes);
},
$this->inlineTypes
));
}
/** /**
* @param string $marker * @param string $marker
* @return class-string<Inline>[] * @return class-string<Inline>[]

View File

@ -2,6 +2,8 @@
namespace Erusev\Parsedown\Tests; namespace Erusev\Parsedown\Tests;
use Erusev\Parsedown\Components\Inlines\Url;
use Erusev\Parsedown\Configurables\InlineTypes;
use Erusev\Parsedown\Configurables\StrictMode; use Erusev\Parsedown\Configurables\StrictMode;
use Erusev\Parsedown\Parsedown; use Erusev\Parsedown\Parsedown;
use Erusev\Parsedown\State; use Erusev\Parsedown\State;
@ -21,7 +23,8 @@ class CommonMarkTestStrict extends TestCase
protected function setUp() protected function setUp()
{ {
$this->parsedown = new Parsedown(new State([ $this->parsedown = new Parsedown(new State([
StrictMode::enabled() StrictMode::enabled(),
InlineTypes::initial()->removing([Url::class]),
])); ]));
// $this->parsedown->setUrlsLinked(false); // $this->parsedown->setUrlsLinked(false);
} }