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