Add raw HTML draft editing to LaySite MCP
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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('此页面地址已存在。');
|
||||
|
||||
@@ -95,6 +95,15 @@ final class McpController
|
||||
$optional = [
|
||||
'name' => $string, 'title' => $string, 'heading' => $string, 'description' => $string, 'lead' => $string,
|
||||
'accent' => $string, 'image' => $string, 'alt' => $string,
|
||||
'raw_html' => ['oneOf' => [
|
||||
$string + ['description' => 'Replace the custom hero HTML.'],
|
||||
$object([
|
||||
'hero' => ['type' => ['string', 'null'], 'description' => 'Custom hero fragment. Null restores the template hero.'],
|
||||
'head' => $string + ['description' => 'Page Head, including style tags. Only existing scripts may be retained.'],
|
||||
'sections' => ['type' => 'object', 'maxProperties' => 50, 'additionalProperties' => $string,
|
||||
'description' => 'Section keys mapped to replacement body HTML. Preserves other section data.'],
|
||||
]),
|
||||
]],
|
||||
'header_id' => ['type' => ['integer', 'null']], 'footer_id' => ['type' => ['integer', 'null']], 'palette_id' => ['type' => ['integer', 'null']],
|
||||
'visibility' => $object(['hero' => ['type' => 'boolean'], 'header' => ['type' => 'boolean'], 'footer' => ['type' => 'boolean']]),
|
||||
'actions' => ['type' => 'array', 'maxItems' => 8, 'items' => ['type' => 'array', 'description' => '[URL, label, style, boolean opensNewTab]. Style: solid, outline, underline, plain.']],
|
||||
@@ -105,7 +114,7 @@ final class McpController
|
||||
];
|
||||
$definitions = [
|
||||
'list_pages' => ['List pages and their current revisions.', $object([])],
|
||||
'get_page' => ['Read a page and its editable sections.', $object(['slug' => $string], ['slug'])],
|
||||
'get_page' => ['Read a page, editable sections and raw_html.', $object(['slug' => $string], ['slug'])],
|
||||
'list_templates' => ['List whole-page templates and reusable components.', $object([])],
|
||||
'list_designs' => ['List saved header, footer and palette IDs. Authentication settings are private.', $object([])],
|
||||
'create_draft' => ['Create a draft from content or a template. Returns the editor URL.', $object(['slug' => $string, 'template' => $string] + $optional, ['slug', 'name'])],
|
||||
@@ -131,7 +140,7 @@ final class McpController
|
||||
try {
|
||||
$data = match ($name) {
|
||||
'list_pages' => $repo->pages(),
|
||||
'get_page' => $this->read($repo, PageDraft::slug($args['slug'] ?? null, [], includeSpecial: true)),
|
||||
'get_page' => $this->readSource($repo, PageDraft::slug($args['slug'] ?? null, [], includeSpecial: true)),
|
||||
'list_templates' => $this->templates(),
|
||||
'list_designs' => DesignLibrary::all(),
|
||||
'create_draft' => $this->create($repo, $args),
|
||||
@@ -171,6 +180,13 @@ final class McpController
|
||||
return $this->summary($repo, $slug);
|
||||
}
|
||||
|
||||
private function readSource(Repository $repo, string $slug): array
|
||||
{
|
||||
$page = $this->read($repo, $slug);
|
||||
|
||||
return $page + ['raw_html' => PageDraft::rawHtml($page)];
|
||||
}
|
||||
|
||||
private function save(Repository $repo, array $args): array
|
||||
{
|
||||
$slug = PageDraft::slug($args['slug'] ?? null);
|
||||
@@ -181,7 +197,11 @@ final class McpController
|
||||
}
|
||||
$page = PageDraft::content($args, $page);
|
||||
$replace = array_key_exists('sections', $args);
|
||||
$repo->savePage($slug, $page, $version, $replace ? $page['sections'] : [], replaceDraftSections: $replace);
|
||||
$raw = $args['raw_html'] ?? null;
|
||||
$patches = is_array($raw) ? ($raw['sections'] ?? []) : [];
|
||||
$sections = $replace ? $page['sections'] : array_filter($page['sections'],
|
||||
static fn (array $section): bool => array_key_exists($section['key'], $patches));
|
||||
$repo->savePage($slug, $page, $version, $sections, replaceDraftSections: $replace);
|
||||
|
||||
return $this->summary($repo, $slug);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user