feat(ext-blog): Add rich Markdown editor for blog posts

Replace the plain textarea with a CodeMirror-based editor supporting syntax highlighting, formatting toolbar (bold, italic, lists, code blocks), and a live preview toggle. Update PostContentRenderer to handle new Markdown syntax (blockquotes, fenced code blocks). Add a preview endpoint for rendering content without saving.
This commit is contained in:
2026-12-02 10:20:36 +00:00
parent e97d96c1f5
commit 2d4b196b04
17 changed files with 1127 additions and 38 deletions
@@ -11,7 +11,8 @@ class PostContentRenderer
$paragraph = [];
$listType = null;
$listItems = [];
$flush = function () use (&$blocks, &$paragraph, &$listType, &$listItems): void {
$quote = [];
$flush = function () use (&$blocks, &$paragraph, &$listType, &$listItems, &$quote): void {
if ($paragraph !== []) {
$blocks[] = '<p>' . nl2br($this->renderInline(implode("\n", $paragraph))) . '</p>';
$paragraph = [];
@@ -23,9 +24,35 @@ class PostContentRenderer
$listType = null;
$listItems = [];
}
if ($quote !== []) {
$blocks[] = '<blockquote class="border-l-4 border-base-300 pl-4 text-base-content/80"><p>' . nl2br($this->renderInline(implode("\n", $quote))) . '</p></blockquote>';
$quote = [];
}
};
$fence = null;
$codeLines = [];
foreach (preg_split('/\R/', $markdown) ?: [] as $line) {
if ($fence !== null) {
if (preg_match('/^ {0,3}(`{3,}|~{3,})[ \t]*$/', $line, $matches)
&& $matches[1][0] === $fence[0]
&& strlen($matches[1]) >= strlen($fence)) {
$blocks[] = '<pre class="overflow-x-auto rounded-box bg-base-200 p-4"><code>' . $this->escape(implode("\n", $codeLines)) . '</code></pre>';
$fence = null;
$codeLines = [];
continue;
}
$codeLines[] = $line;
continue;
}
if (preg_match('/^ {0,3}(`{3,}|~{3,})(.*)$/', $line, $matches)) {
$flush();
$fence = $matches[1];
$codeLines = [];
continue;
}
if (trim($line) === '') {
$flush();
continue;
@@ -45,6 +72,23 @@ class PostContentRenderer
continue;
}
if (preg_match('/^ {0,3}(?:(?:\*[ \t]*){3,}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,})$/', $line)) {
$flush();
$blocks[] = '<hr class="my-6 border-base-300">';
continue;
}
if (preg_match('/^ {0,3}> ?(.*)$/', $line, $matches)) {
if ($paragraph !== [] || $listType !== null) {
$flush();
}
$quote[] = $matches[1];
continue;
}
if ($quote !== []) {
$flush();
}
if (preg_match('/^ {0,3}[-*+]\s+(.+)$/', $line, $matches)) {
$type = 'ul';
} elseif (preg_match('/^ {0,3}\d+[.)]\s+(.+)$/', $line, $matches)) {
@@ -67,6 +111,9 @@ class PostContentRenderer
}
$flush();
if ($fence !== null) {
$blocks[] = '<pre class="overflow-x-auto rounded-box bg-base-200 p-4"><code>' . $this->escape(implode("\n", $codeLines)) . '</code></pre>';
}
return implode("\n", $blocks);
}