the latest code
This commit is contained in:
1 parent
d28bc1ed43
commit
9bbb24a52b
12226 files changed
+1455579
-21
No files matched your search
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
* (c) Colin O'Dell <colinodell@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace League\CommonMark\Extension\TaskList;
|
||||
|
||||
use League\CommonMark\Environment\EnvironmentBuilderInterface;
|
||||
use League\CommonMark\Extension\ExtensionInterface;
|
||||
|
||||
final class TaskListExtension implements ExtensionInterface
|
||||
{
|
||||
public function register(EnvironmentBuilderInterface $environment): void
|
||||
{
|
||||
$environment->addInlineParser(new TaskListItemMarkerParser(), 35);
|
||||
$environment->addRenderer(TaskListItemMarker::class, new TaskListItemMarkerRenderer());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
* (c) Colin O'Dell <colinodell@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace League\CommonMark\Extension\TaskList;
|
||||
|
||||
use League\CommonMark\Node\Inline\AbstractInline;
|
||||
|
||||
final class TaskListItemMarker extends AbstractInline
|
||||
{
|
||||
/** @psalm-readonly-allow-private-mutation */
|
||||
private bool $checked;
|
||||
|
||||
public function __construct(bool $isCompleted)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->checked = $isCompleted;
|
||||
}
|
||||
|
||||
public function isChecked(): bool
|
||||
{
|
||||
return $this->checked;
|
||||
}
|
||||
|
||||
public function setChecked(bool $checked): void
|
||||
{
|
||||
$this->checked = $checked;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
* (c) Colin O'Dell <colinodell@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace League\CommonMark\Extension\TaskList;
|
||||
|
||||
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
|
||||
use League\CommonMark\Node\Block\Paragraph;
|
||||
use League\CommonMark\Parser\Inline\InlineParserInterface;
|
||||
use League\CommonMark\Parser\Inline\InlineParserMatch;
|
||||
use League\CommonMark\Parser\InlineParserContext;
|
||||
|
||||
final class TaskListItemMarkerParser implements InlineParserInterface
|
||||
{
|
||||
public function getMatchDefinition(): InlineParserMatch
|
||||
{
|
||||
return InlineParserMatch::oneOf('[ ]', '[x]');
|
||||
}
|
||||
|
||||
public function parse(InlineParserContext $inlineContext): bool
|
||||
{
|
||||
$container = $inlineContext->getContainer();
|
||||
|
||||
// Checkbox must come at the beginning of the first paragraph of the list item
|
||||
if ($container->hasChildren() || ! ($container instanceof Paragraph && $container->parent() && $container->parent() instanceof ListItem)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$cursor = $inlineContext->getCursor();
|
||||
$oldState = $cursor->saveState();
|
||||
|
||||
$cursor->advanceBy(3);
|
||||
|
||||
if ($cursor->getNextNonSpaceCharacter() === null) {
|
||||
$cursor->restoreState($oldState);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$isChecked = $inlineContext->getFullMatch() !== '[ ]';
|
||||
|
||||
$container->appendChild(new TaskListItemMarker($isChecked));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
* (c) Colin O'Dell <colinodell@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace League\CommonMark\Extension\TaskList;
|
||||
|
||||
use League\CommonMark\Node\Node;
|
||||
use League\CommonMark\Renderer\ChildNodeRendererInterface;
|
||||
use League\CommonMark\Renderer\NodeRendererInterface;
|
||||
use League\CommonMark\Util\HtmlElement;
|
||||
use League\CommonMark\Xml\XmlNodeRendererInterface;
|
||||
|
||||
final class TaskListItemMarkerRenderer implements NodeRendererInterface, XmlNodeRendererInterface
|
||||
{
|
||||
/**
|
||||
* @param TaskListItemMarker $node
|
||||
*
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @psalm-suppress MoreSpecificImplementedParamType
|
||||
*/
|
||||
public function render(Node $node, ChildNodeRendererInterface $childRenderer): \Stringable
|
||||
{
|
||||
TaskListItemMarker::assertInstanceOf($node);
|
||||
|
||||
$attrs = $node->data->get('attributes');
|
||||
$checkbox = new HtmlElement('input', $attrs, '', true);
|
||||
|
||||
if ($node->isChecked()) {
|
||||
$checkbox->setAttribute('checked', '');
|
||||
}
|
||||
|
||||
$checkbox->setAttribute('disabled', '');
|
||||
$checkbox->setAttribute('type', 'checkbox');
|
||||
|
||||
return $checkbox;
|
||||
}
|
||||
|
||||
public function getXmlTagName(Node $node): string
|
||||
{
|
||||
return 'task_list_item_marker';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TaskListItemMarker $node
|
||||
*
|
||||
* @return array<string, scalar>
|
||||
*
|
||||
* @psalm-suppress MoreSpecificImplementedParamType
|
||||
*/
|
||||
public function getXmlAttributes(Node $node): array
|
||||
{
|
||||
TaskListItemMarker::assertInstanceOf($node);
|
||||
|
||||
if ($node->isChecked()) {
|
||||
return ['checked' => 'checked'];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user