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) {
|
||||
|
||||
Reference in New Issue
Block a user