92 lines
3.4 KiB
PHP
92 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Utils;
|
|
|
|
use League\CommonMark\Environment\Environment;
|
|
use League\CommonMark\Extension\DisallowedRawHtml\DisallowedRawHtmlExtension;
|
|
use League\CommonMark\Extension\TableOfContents\TableOfContentsExtension;
|
|
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkExtension;
|
|
use League\CommonMark\Extension\ExternalLink\ExternalLinkExtension;
|
|
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
|
|
use League\CommonMark\Extension\Mention\MentionExtension;
|
|
use League\CommonMark\Extension\Table\TableExtension;
|
|
use League\CommonMark\MarkdownConverter;
|
|
|
|
class RenderHtml {
|
|
/**
|
|
* Converts markdown content to HTML
|
|
*
|
|
* @param string $content The markdown content of page
|
|
* @return string The converted HTML content
|
|
*/
|
|
static function markdownToHtml(string $content): string {
|
|
$config = [
|
|
'disallowed_raw_html' => [
|
|
'disallowed_tags' => [
|
|
'title',
|
|
'textarea',
|
|
'style',
|
|
'xmp',
|
|
'iframe',
|
|
'noembed',
|
|
'noframes',
|
|
'script',
|
|
'plaintext'
|
|
],
|
|
],
|
|
'table_of_contents' => [
|
|
'html_class' => 'table-of-contents',
|
|
'min_heading_level' => 2,
|
|
'max_heading_level' => 6,
|
|
'normalize' => 'relative',
|
|
'placeholder' => null,
|
|
],
|
|
'heading_permalink' => [
|
|
'html_class' => '',
|
|
'apply_id_to_heading' => false,
|
|
'min_heading_level' => 2,
|
|
'max_heading_level' => 6,
|
|
'symbol' => '',
|
|
],
|
|
'table' => [
|
|
'wrap' => [
|
|
'enabled' => false,
|
|
'tag' => 'div',
|
|
'attributes' => [],
|
|
],
|
|
'alignment_attributes' => [
|
|
'left' => ['align' => 'left'],
|
|
'center' => ['align' => 'center'],
|
|
'right' => ['align' => 'right'],
|
|
],
|
|
],
|
|
'mentions' => [
|
|
'tag_handle' => [
|
|
'prefix' => '#',
|
|
'pattern' => '[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}(?!\w)',
|
|
'generator' => '/tag/%s',
|
|
]
|
|
],
|
|
'external_link' => [
|
|
'internal_hosts' => 'site.knah-tsaeb.local', // TODO: Don't forget to set this!
|
|
'open_in_new_window' => true,
|
|
'html_class' => 'external-link',
|
|
'nofollow' => '',
|
|
'noopener' => 'external',
|
|
'noreferrer' => 'external',
|
|
],
|
|
];
|
|
|
|
$environment = new Environment($config);
|
|
$environment->addExtension(new CommonMarkCoreExtension());
|
|
$environment->addExtension(new MentionExtension());
|
|
$environment->addExtension(new TableExtension());
|
|
$environment->addExtension(new TableOfContentsExtension());
|
|
$environment->addExtension(new ExternalLinkExtension());
|
|
$environment->addExtension(new HeadingPermalinkExtension());
|
|
$environment->addExtension(new DisallowedRawHtmlExtension());
|
|
|
|
$converter = new MarkdownConverter($environment);
|
|
return $converter->convert($content);
|
|
}
|
|
}
|