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

Implement Strikethrough

This commit is contained in:
Aidan Woods 2019-01-20 02:36:55 +00:00
parent f256352f53
commit db1d0a4999
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
2 changed files with 67 additions and 21 deletions

View File

@ -0,0 +1,67 @@
<?php
namespace Erusev\Parsedown\Components\Inlines;
use Erusev\Parsedown\AST\Handler;
use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\Inline;
use Erusev\Parsedown\Html\Renderables\Element;
use Erusev\Parsedown\Html\Renderables\Text;
use Erusev\Parsedown\Parsedown;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
final class Strikethrough implements Inline
{
use WidthTrait, DefaultBeginPosition;
/** @var string */
private $text;
/**
* @param string $text
* @param int $width
*/
public function __construct($text, $width)
{
$this->text = $text;
$this->width = $width;
}
/**
* @param Excerpt $Excerpt
* @param State $State
* @return static|null
*/
public static function build(Excerpt $Excerpt, State $State)
{
$text = $Excerpt->text();
if (\strlen($text) < 2) {
return null;
}
if ($text[1] === '~' and \preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $text, $matches)) {
return new self($matches[1], \strlen($matches[0]));
}
return null;
}
/**
* @return Handler<Element>
*/
public function stateRenderable(Parsedown $Parsedown)
{
return new Handler(
/** @return Element */
function (State $State) use ($Parsedown) {
return new Element(
'del',
[],
$State->applyTo($Parsedown->lineElements($this->text))
);
}
);
}
}

View File

@ -411,27 +411,6 @@ class Parsedown
return $Elements;
}
protected function inlineStrikethrough($Excerpt)
{
if (! isset($Excerpt['text'][1])) {
return;
}
if ($Excerpt['text'][1] === '~' and \preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $Excerpt['text'], $matches)) {
return [
'extent' => \strlen($matches[0]),
'element' => [
'name' => 'del',
'handler' => [
'function' => 'lineElements',
'argument' => $matches[1],
'destination' => 'elements',
]
],
];
}
}
protected function inlineUrl($Excerpt)
{
if ($this->urlsLinked !== true or ! isset($Excerpt['text'][2]) or $Excerpt['text'][2] !== '/') {