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:
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
phpb_requires_authenticated();
|
||||
|
||||
$respond = static function (int $status, array $payload): void {
|
||||
http_response_code($status);
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
|
||||
};
|
||||
|
||||
if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
|
||||
$respond(405, ['error' => 'Preview requests must use POST.']);
|
||||
return;
|
||||
}
|
||||
if (!phpb_validate_csrf()) {
|
||||
$respond(403, ['error' => 'The preview request could not be verified.']);
|
||||
return;
|
||||
}
|
||||
|
||||
$body = $_POST['body'] ?? null;
|
||||
if (!is_string($body)) {
|
||||
$respond(400, ['error' => 'A Markdown body is required.']);
|
||||
return;
|
||||
}
|
||||
if (strlen($body) > 1000000) {
|
||||
$respond(413, ['error' => 'The Markdown body is too large to preview.']);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$html = (new \Plugi\Extensions\Blog\PostContentRenderer())->render($body);
|
||||
$respond(200, ['html' => $html]);
|
||||
} catch (\Throwable) {
|
||||
$respond(500, ['error' => 'The Markdown preview could not be rendered.']);
|
||||
}
|
||||
Reference in New Issue
Block a user