diff --git a/app/scripts/static-fallback.mjs b/app/scripts/static-fallback.mjs new file mode 100644 index 0000000..936062e --- /dev/null +++ b/app/scripts/static-fallback.mjs @@ -0,0 +1,62 @@ +export function esc(value) { + return String(value ?? '') + .replace(/&/g, '&') + .replace(/"/g, '"') + .replace(//g, '>'); +} + +export function cleanText(value) { + return String(value ?? '').replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim(); +} + +function renderLinks(links = []) { + const items = links + .filter(item => item?.href && item?.label) + .slice(0, 24) + .map(item => `
  • ${esc(item.label)}${item.text ? `${esc(item.text)}` : ''}
  • `) + .join(''); + return items ? `` : ''; +} + +function renderFacts(facts = []) { + const items = facts + .filter(item => item?.label && item?.value) + .slice(0, 12) + .map(item => `
  • ${esc(item.label)}${esc(item.value)}
  • `) + .join(''); + return items ? `` : ''; +} + +export function renderStaticFallback({ eyebrow, title, description, bodyHtml, facts, links, sections } = {}) { + const renderedSections = (sections ?? []) + .filter(section => section?.title || section?.text) + .slice(0, 8) + .map(section => ` +
    + ${section.title ? `

    ${esc(section.title)}

    ` : ''} + ${section.text ? `

    ${esc(section.text)}

    ` : ''} + ${renderLinks(section.links)} +
    `) + .join(''); + + return `
    +
    + ${eyebrow ? `

    ${esc(eyebrow)}

    ` : ''} + ${title ? `

    ${esc(title)}

    ` : ''} + ${description ? `

    ${esc(description)}

    ` : ''} + ${renderFacts(facts)} + ${renderLinks(links)} + ${bodyHtml ? `
    ${bodyHtml}
    ` : ''} + ${renderedSections} +
    +
    `; +} + +export function replaceStaticFallback(html, fallbackHtml) { + const fallbackPattern = /
    \s*(?=
    <\/div>)/; + if (fallbackPattern.test(html)) { + return html.replace(fallbackPattern, `${fallbackHtml}\n\n `); + } + return html.replace('
    ', `${fallbackHtml}\n\n
    `); +}