38 lines
1.2 KiB
PHP
38 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Support;
|
|
|
|
use Illuminate\Support\Str;
|
|
use League\CommonMark\Extension\ExternalLink\ExternalLinkExtension;
|
|
|
|
/**
|
|
* Posts are written in Markdown. Raw HTML in a post is escaped, so the rendered output is safe
|
|
* to print. Links to other hosts open in a new window; `manager.markdown.internal_hosts` lists
|
|
* more hosts that count as this site.
|
|
*/
|
|
final class Markdown
|
|
{
|
|
public static function toHtml(string $markdown): string
|
|
{
|
|
$host = parse_url((string) config('app.url'), PHP_URL_HOST);
|
|
|
|
return Str::markdown($markdown, [
|
|
'html_input' => 'escape',
|
|
'allow_unsafe_links' => false,
|
|
'external_link' => [
|
|
'internal_hosts' => array_values(array_filter([$host, ...(array) config('manager.markdown.internal_hosts', [])])),
|
|
'open_in_new_window' => true,
|
|
'noopener' => 'external',
|
|
'noreferrer' => 'external',
|
|
],
|
|
], [new ExternalLinkExtension]);
|
|
}
|
|
|
|
public static function toPlainText(string $markdown): string
|
|
{
|
|
$text = strip_tags(self::toHtml($markdown));
|
|
|
|
return trim((string) preg_replace('/\s+/', ' ', html_entity_decode($text, ENT_QUOTES | ENT_HTML5)));
|
|
}
|
|
}
|