Refine admin dock and access card editing
This commit is contained in:
+30
-3
@@ -37,6 +37,11 @@ function getByPath(source, path) {
|
||||
return path.split(".").reduce((value, key) => value?.[key], source);
|
||||
}
|
||||
|
||||
function isTruthy(value) {
|
||||
if (Array.isArray(value)) return value.length > 0;
|
||||
return Boolean(value);
|
||||
}
|
||||
|
||||
function escapeHtml(value) {
|
||||
return String(value)
|
||||
.replaceAll("&", "&")
|
||||
@@ -83,6 +88,24 @@ function renderTemplateTokens(html, block) {
|
||||
});
|
||||
}
|
||||
|
||||
function renderIfBlocks(html, block) {
|
||||
let rendered = html;
|
||||
let previous = null;
|
||||
|
||||
while (rendered !== previous) {
|
||||
previous = rendered;
|
||||
rendered = rendered.replace(
|
||||
/\{\{#if\s+([a-zA-Z0-9_.-]+)\s*\}\}((?:(?!\{\{#if|\{\{\/if\}\})[\s\S])*)\{\{\/if\}\}/g,
|
||||
(_match, path, innerHtml) => {
|
||||
const value = getByPath(block, path);
|
||||
return isTruthy(value) ? renderTemplate(innerHtml, block) : "";
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return rendered;
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -91,10 +114,14 @@ function renderEachBlocks(html, block) {
|
||||
throw new Error(`Each token "${path}" in block "${block.id}" must point to an array`);
|
||||
}
|
||||
|
||||
return items.map((item) => renderTemplateTokens(innerHtml, item ?? {})).join("");
|
||||
return items.map((item) => renderTemplate(innerHtml, item ?? {})).join("");
|
||||
});
|
||||
}
|
||||
|
||||
function renderTemplate(html, block) {
|
||||
return renderTemplateTokens(renderIfBlocks(renderEachBlocks(html, block), block), block);
|
||||
}
|
||||
|
||||
function scopeDuplicateIds(html, scope) {
|
||||
const ids = [...html.matchAll(/\sid="([^"]+)"/g)].map((match) => match[1]);
|
||||
if (!ids.length) {
|
||||
@@ -124,7 +151,7 @@ async function renderBlock(block, renderCounts) {
|
||||
const renderCount = renderCounts.get(templateName) ?? 0;
|
||||
renderCounts.set(templateName, renderCount + 1);
|
||||
|
||||
const html = renderTemplateTokens(renderEachBlocks(await readTemplate(templateName), block), block);
|
||||
const html = renderTemplate(await readTemplate(templateName), block);
|
||||
|
||||
if (renderCount === 0 && block.scopeIds !== true) {
|
||||
return html;
|
||||
@@ -144,7 +171,7 @@ async function renderRegion(region) {
|
||||
return blocks.join("");
|
||||
}
|
||||
|
||||
let output = renderTemplateTokens(renderEachBlocks(shell, { id: page.slug, ...page }), { id: page.slug, ...page });
|
||||
let output = renderTemplate(shell, { id: page.slug, ...page });
|
||||
for (const region of page.regions ?? []) {
|
||||
const token = REGION_TOKEN[region.id];
|
||||
if (!token) {
|
||||
|
||||
Reference in New Issue
Block a user