Add raw HTML draft editing to LaySite MCP

This commit is contained in:
2026-11-28 17:42:32 +08:00
parent 214098c2e1
commit 1c927d360c
7 changed files with 125 additions and 11 deletions
+53
View File
@@ -77,9 +77,62 @@ final class PageDraft
$page['quicklinks'] = array_map(static fn (array $section): array => ['#' . $section['key'], $section['title']], array_slice($page['sections'], 0, 20));
}
return array_key_exists('raw_html', $input) ? self::applyRawHtml($page, $input['raw_html']) : $page;
}
public static function rawHtml(array $page): array
{
return [
'hero' => $page['presentation']['heroHtml'] ?? null,
'head' => $page['head_html'] ?? '',
'sections' => (object) array_column($page['sections'], 'body', 'key'),
];
}
private static function applyRawHtml(array $page, mixed $input): array
{
if (is_string($input)) {
$input = ['hero' => $input];
}
if (!is_array($input) || array_diff(array_keys($input), ['hero', 'head', 'sections'])) {
throw new RuntimeException('raw_html 须为首屏 HTML 字符串或包含 hero、head、sections 的对象。');
}
if (array_key_exists('hero', $input)) {
if ($input['hero'] === null) {
unset($page['presentation']['heroHtml']);
} else {
$page['presentation']['heroHtml'] = self::sanitizeRawHtml($input['hero']);
}
}
if (array_key_exists('head', $input)) {
$page['head_html'] = PageHead::normalize($input['head'], restrictScriptsTo: $page['head_html'] ?? '');
}
if (array_key_exists('sections', $input)) {
$patches = $input['sections'];
if (!is_array($patches) || count($patches) > 50) {
throw new RuntimeException('raw_html.sections 须为区块锚点与 HTML 的映射,最多 50 项。');
}
$positions = array_flip(array_column($page['sections'], 'key'));
foreach ($patches as $key => $html) {
if (!isset($positions[$key])) {
throw new RuntimeException('raw_html.sections 中的区块不存在。');
}
$page['sections'][$positions[$key]]['body'] = self::sanitizeRawHtml($html);
}
}
return $page;
}
private static function sanitizeRawHtml(mixed $value): string
{
if (!is_string($value) || strlen($value) > 300000 || !mb_check_encoding($value, 'UTF-8') || str_contains($value, "\0")) {
throw new RuntimeException('HTML 片段须为 UTF-8,最多 300 KB。');
}
return Html::sanitize($value);
}
private static function sections(mixed $items): array
{
if (!is_array($items) || !array_is_list($items) || count($items) > 50) {
+15 -2
View File
@@ -18,13 +18,26 @@ final class PageHead
'script' => ['src', 'type', 'async', 'defer', 'integrity', 'crossorigin', 'referrerpolicy'],
];
public static function normalize(mixed $value): string
public static function normalize(mixed $value, ?string $restrictScriptsTo = null): string
{
if (!is_string($value) || strlen($value) > self::MAX_LENGTH || !mb_check_encoding($value, 'UTF-8') || str_contains($value, "\0")) {
throw new RuntimeException('Head 代码须使用 UTF-8,最多 32 KB。');
}
$value = trim($value);
self::parse($value);
$entries = self::parse($value);
if ($restrictScriptsTo !== null) {
$existing = self::parse($restrictScriptsTo);
foreach ($entries as $entry) {
if ($entry[0] !== 'script') {
continue;
}
$index = array_search($entry, $existing, true);
if ($index === false) {
throw new RuntimeException('MCP 只能保留本页已有的脚本。请在管理后台添加或修改脚本。');
}
unset($existing[$index]);
}
}
return $value;
}
+4 -2
View File
@@ -61,14 +61,16 @@ final class Repository
public static function insertPage(PDO $pdo, string $slug, string $name, array $template = [], ?int $layoutId = null, string $type = 'page'): void
{
$query = $pdo->prepare("INSERT OR IGNORE INTO pages (slug, name, title, description, heading, accent,
lead, image, alt, actions, quicklinks, theme, visibility, header_id, footer_id, palette_id, layout_id, page_type, status, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
lead, image, alt, actions, quicklinks, theme, visibility, header_id, footer_id, palette_id, layout_id, page_type, status, updated_at,
presentation, head_html)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$query->execute([
$slug, $name, $template['title'] ?? $name, $template['description'] ?? '', $template['heading'] ?? $name,
$template['accent'] ?? '', $template['lead'] ?? '', $template['image'] ?? '', $template['alt'] ?? '',
Database::json($template['actions'] ?? []), Database::json($template['quicklinks'] ?? []),
$template['theme'] ?? '', Database::json($template['visibility'] ?? []), $template['header_id'] ?? null,
$template['footer_id'] ?? null, $template['palette_id'] ?? null, $layoutId, $type, $layoutId === null ? 'draft' : 'published', self::now(),
CanvasStyle::encode($template['presentation'] ?? []), $template['head_html'] ?? '',
]);
if ($query->rowCount() !== 1) {
throw new RuntimeException('此页面地址已存在。');