mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Implement Strikethrough
This commit is contained in:
parent
f256352f53
commit
db1d0a4999
67
src/Components/Inlines/Strikethrough.php
Normal file
67
src/Components/Inlines/Strikethrough.php
Normal 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))
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
@ -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] !== '/') {
|
||||
|
Loading…
Reference in New Issue
Block a user