Extend admin content templates

This commit is contained in:
dcconstructions
2026-06-26 11:10:18 +03:00
parent 931ac628b4
commit e4bfd93a67
13 changed files with 4268 additions and 45 deletions
+6
View File
@@ -9,6 +9,7 @@ const repoRoot = join(dirname(fileURLToPath(import.meta.url)), "..");
const port = Number(process.env.PORT || 8090);
const host = process.env.HOST || "127.0.0.1";
const pagePath = join(repoRoot, "content", "pages", "home.json");
const blockTemplatesPath = join(repoRoot, "content", "block-templates", "home.json");
const uploadRoot = join(repoRoot, "assets", "uploads");
const MIME = {
@@ -223,6 +224,11 @@ const server = createServer(async (req, res) => {
return;
}
if (req.method === "GET" && url.pathname === "/api/block-templates/home") {
send(res, 200, await readFile(blockTemplatesPath, "utf8"), "application/json; charset=utf-8");
return;
}
if (req.method === "PUT" && url.pathname === "/api/page/home") {
const body = await readBody(req);
const parsed = JSON.parse(body);
+13 -1
View File
@@ -83,6 +83,18 @@ function renderTemplateTokens(html, block) {
});
}
function renderEachBlocks(html, block) {
return html.replace(/\{\{#each\s+([a-zA-Z0-9_.-]+)\s*\}\}([\s\S]*?)\{\{\/each\}\}/g, (_match, path, innerHtml) => {
const items = getByPath(block, path);
if (!Array.isArray(items)) {
throw new Error(`Each token "${path}" in block "${block.id}" must point to an array`);
}
return items.map((item) => renderTemplateTokens(innerHtml, item ?? {})).join("");
});
}
function scopeDuplicateIds(html, scope) {
const ids = [...html.matchAll(/\sid="([^"]+)"/g)].map((match) => match[1]);
if (!ids.length) {
@@ -112,7 +124,7 @@ async function renderBlock(block, renderCounts) {
const renderCount = renderCounts.get(templateName) ?? 0;
renderCounts.set(templateName, renderCount + 1);
const html = renderTemplateTokens(await readTemplate(templateName), block);
const html = renderTemplateTokens(renderEachBlocks(await readTemplate(templateName), block), block);
if (renderCount === 0 && block.scopeIds !== true) {
return html;