2021-08-23 17:31:07 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Tools\Markdown;
|
|
|
|
|
2023-02-06 15:00:44 -05:00
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
|
|
|
|
use League\CommonMark\Extension\CommonMark\Renderer\Block\ListItemRenderer;
|
2021-08-23 17:31:07 -04:00
|
|
|
use League\CommonMark\Extension\TaskList\TaskListItemMarker;
|
2023-02-06 15:00:44 -05:00
|
|
|
use League\CommonMark\Node\Block\Paragraph;
|
|
|
|
use League\CommonMark\Node\Node;
|
|
|
|
use League\CommonMark\Renderer\ChildNodeRendererInterface;
|
|
|
|
use League\CommonMark\Renderer\NodeRendererInterface;
|
|
|
|
use League\CommonMark\Util\HtmlElement;
|
2021-08-23 17:31:07 -04:00
|
|
|
|
2023-02-06 15:00:44 -05:00
|
|
|
class CustomListItemRenderer implements NodeRendererInterface
|
2021-08-23 17:31:07 -04:00
|
|
|
{
|
2023-02-06 15:00:44 -05:00
|
|
|
protected ListItemRenderer $baseRenderer;
|
2021-08-23 17:31:07 -04:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->baseRenderer = new ListItemRenderer();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return HtmlElement|string|null
|
|
|
|
*/
|
2023-02-06 15:00:44 -05:00
|
|
|
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
|
2021-08-23 17:31:07 -04:00
|
|
|
{
|
2023-02-06 15:00:44 -05:00
|
|
|
$listItem = $this->baseRenderer->render($node, $childRenderer);
|
2021-08-23 17:31:07 -04:00
|
|
|
|
2023-02-06 15:00:44 -05:00
|
|
|
if ($node instanceof ListItem && $this->startsTaskListItem($node) && $listItem instanceof HtmlElement) {
|
2021-08-23 17:31:07 -04:00
|
|
|
$listItem->setAttribute('class', 'task-list-item');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $listItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function startsTaskListItem(ListItem $block): bool
|
|
|
|
{
|
|
|
|
$firstChild = $block->firstChild();
|
|
|
|
|
|
|
|
return $firstChild instanceof Paragraph && $firstChild->firstChild() instanceof TaskListItemMarker;
|
|
|
|
}
|
2021-08-24 16:23:55 -04:00
|
|
|
}
|