<?php

namespace PpPhp\Plugins;

use PpPhp\Core\Html\Markdown as MarkdownRenderer;
use PpPhp\Core\ViewPlugin;

/**
 * Markdown view-control (platform feature A5) — the display half of the Markdown
 * field. Renders the stored raw markdown to SANITISED HTML on every display via
 * \PpPhp\Core\Html\Markdown::toSafeHtml (which pipes through the allowlist
 * Sanitizer). Pair with the "Markdown Editor" edit-control, which stores the raw
 * markdown. Never render stored markdown as HTML without this.
 */
class Markdown implements ViewPlugin
{
    public static function id(): string { return 'Markdown'; }

    public static function label(): string { return 'Markdown'; }
    public static function version(): string { return '1.0.0'; }
    public static function description(): string { return 'Renders stored markdown to sanitised HTML.'; }

    public static function render(mixed $value, array $settings): string
    {
        $md = (string) ($value ?? '');
        if (trim($md) === '') {
            return '';
        }
        return '<div class="pp-md-rendered">' . MarkdownRenderer::toSafeHtml($md) . '</div>';
    }

    public static function settingsSchema(): array { return []; }

    public static function assets(): array
    {
        return ['js' => [], 'css' => ['plugins/View/Markdown/markdown.css']];
    }
}
