Build a full-width CMS canvas and restore featured home products

This commit is contained in:
2026-11-21 21:33:15 +09:00
parent 00d7d6f9ad
commit e7c7a8928e
16 changed files with 590 additions and 122 deletions
+32
View File
@@ -8,6 +8,7 @@ use DOMDocument;
use DOMElement;
use DOMNode;
use League\CommonMark\CommonMarkConverter;
use RuntimeException;
final class Html
{
@@ -49,6 +50,37 @@ final class Html
return str_replace(['<h1>', '</h1>'], ['<h2>', '</h2>'], $html);
}
public static function applyCanvasEdits(string $body, array $edits): string
{
if (!array_is_list($edits) || count($edits) > 512) {
throw new RuntimeException('正文增量无效。');
}
$size = strlen($body);
$cursor = 0;
$result = '';
foreach ($edits as $edit) {
if (!is_array($edit) || !array_is_list($edit) || count($edit) !== 3) {
throw new RuntimeException('正文增量无效。');
}
[$offset, $remove, $insert] = $edit;
if (!is_int($offset) || !is_int($remove) || !is_string($insert)
|| $offset < $cursor || $offset > $size || $remove < 0 || $remove > $size - $offset) {
throw new RuntimeException('正文增量无效。');
}
$result .= substr($body, $cursor, $offset - $cursor) . $insert;
$cursor = $offset + $remove;
if (strlen($result) > 300000) {
throw new RuntimeException('正文太长。');
}
}
$result .= substr($body, $cursor);
if (strlen($result) > 300000 || !mb_check_encoding($result, 'UTF-8')) {
throw new RuntimeException('正文太长或编码无效。');
}
return $result;
}
public static function sanitize(string $html): string
{
$document = new DOMDocument('1.0', 'UTF-8');