*/ declare(strict_types=1); /** Render a basic-Markdown string to safe HTML. */ function markdown_basic(string $text): string { $lines = explode("\n", str_replace("\r\n", "\n", $text)); $html = ''; $inList = false; $para = []; $flushPara = static function () use (&$para, &$html): void { if ($para) { $html .= '

' . implode('
', $para) . "

\n"; $para = []; } }; $closeList = static function () use (&$inList, &$html): void { if ($inList) { $html .= "\n"; $inList = false; } }; foreach ($lines as $line) { $trimmed = trim($line); if ($trimmed === '') { $flushPara(); $closeList(); continue; } if (preg_match('/^(#{1,3})\s+(.*)$/', $trimmed, $m)) { $flushPara(); $closeList(); $level = strlen($m[1]); $html .= "" . markdown_inline($m[2]) . "\n"; continue; } if (preg_match('/^[-*]\s+(.*)$/', $trimmed, $m)) { $flushPara(); if (!$inList) { $html .= "