zap/app/Render/MarkdownRenderer.php

37 lines
1.1 KiB
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\FrontMatter\FrontMatterExtension;
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);
$mdEnvironment->addExtension(new FrontMatterExtension);
$parser = new MarkdownConverter($mdEnvironment);
$parsed = $parser->convert($p->content());
$layout = $p->layout;
$tmpBlade = <<<HTML
<x-layouts.{$layout}>
$parsed
</x-layouts.{$layout}>
HTML;
$rendered = Blade::render($tmpBlade, []);
return $rendered;
}
}