zap/app/Render/MarkdownRenderer.php

34 lines
984 B
PHP

<?php
namespace App\Render;
use App\Models\Page;
use Illuminate\Support\Facades\Blade;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
use League\CommonMark\MarkdownConverter;
// TODO: Use commonmark instead of parsedown
class MarkdownRenderer implements Renderer
{
public static function render(Page $p): string
{
// Build the markdown parser
$mdEnvironment = new Environment();
$mdEnvironment->addExtension(new CommonMarkCoreExtension);
$mdEnvironment->addExtension(new GithubFlavoredMarkdownExtension);
$parser = new MarkdownConverter($mdEnvironment);
$parsed = $parser->convert($p->content());
$tmpBlade = <<<HTML
<x-layouts.main>
$parsed
</x-layouts.main>
HTML;
$rendered = Blade::render($tmpBlade, []);
return $rendered;
}
}