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
+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;
}