2022-04-18 18:39:28 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Tools\Markdown;
|
|
|
|
|
|
|
|
use BookStack\Facades\Theme;
|
|
|
|
use BookStack\Theming\ThemeEvents;
|
|
|
|
use League\CommonMark\Block\Element\ListItem;
|
|
|
|
use League\CommonMark\Environment;
|
|
|
|
use League\CommonMark\Extension\Table\TableExtension;
|
|
|
|
use League\CommonMark\Extension\TaskList\TaskListExtension;
|
2023-01-21 21:50:04 +01:00
|
|
|
use League\CommonMark\MarkdownConverter;
|
2022-04-18 18:39:28 +02:00
|
|
|
|
|
|
|
class MarkdownToHtml
|
|
|
|
{
|
|
|
|
protected string $markdown;
|
|
|
|
|
|
|
|
public function __construct(string $markdown)
|
|
|
|
{
|
|
|
|
$this->markdown = $markdown;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function convert(): string
|
|
|
|
{
|
|
|
|
$environment = Environment::createCommonMarkEnvironment();
|
|
|
|
$environment->addExtension(new TableExtension());
|
|
|
|
$environment->addExtension(new TaskListExtension());
|
|
|
|
$environment->addExtension(new CustomStrikeThroughExtension());
|
|
|
|
$environment = Theme::dispatch(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $environment) ?? $environment;
|
2023-01-21 21:50:04 +01:00
|
|
|
$converter = new MarkdownConverter($environment);
|
2022-04-18 18:39:28 +02:00
|
|
|
|
|
|
|
$environment->addBlockRenderer(ListItem::class, new CustomListItemRenderer(), 10);
|
|
|
|
|
|
|
|
return $converter->convertToHtml($this->markdown);
|
|
|
|
}
|
2022-04-24 19:22:40 +02:00
|
|
|
}
|