/* Manifest JS /* By Andrew Matlock under MIT license /* https://manifestx.dev /* /* Loader: pulls Alpine.js and Manifest plugins from the jsDelivr CDN — all /* plugins by default, or a subset if specified. */ (function () { 'use strict'; const loaderScript = document.currentScript; // Pending work that will still change the page: plugins hold() until their DOM update lands; mnfst-render's auto settle won't snapshot early while count > 0 if (!window.ManifestPending) { const holds = new Map(); let seq = 0; window.ManifestPending = { hold(label = 'work') { const id = ++seq; holds.set(id, label); return () => { holds.delete(id); }; }, get count() { return holds.size + (window.__manifestMarkdownPending || 0) + (window.__manifestUtilitiesPending || 0); }, labels() { const out = [...holds.values()]; if (window.__manifestMarkdownPending > 0) out.push('markdown'); if (window.__manifestUtilitiesPending > 0) out.push('utilities'); return out; }, // Marks a timer callback as housekeeping that never changes the page; prerender doesn't wait for it background(fn) { if (typeof fn === 'function') fn.__manifestBackground = true; return fn; }, }; } /* * Hydration contract runtime: prerendered MPA pages carry a `#__manifest_hydrate__` * diff of source-authored attributes (and data-hydrate innerHTML). Applied once * BEFORE any plugin or Alpine so downstream code sees the authored DOM, as in a * live SPA. Attributes go through an HTML parse — setAttribute throws on `@click`. */ function hydratePrerenderedPage() { if (typeof document === 'undefined' || !document.querySelector) return; // Only run on pages the prerender marked as static MPA output. const prerenderMeta = document.querySelector('meta[name="manifest:prerendered"]'); if (!prerenderMeta || prerenderMeta.getAttribute('content') === '0') return; const blob = document.getElementById('__manifest_hydrate__'); if (!blob) return; let entries; try { entries = JSON.parse(blob.textContent || '[]'); } catch (_) { entries = []; } if (!Array.isArray(entries) || entries.length === 0) { blob.remove(); return; } const escAttr = (s) => String(s == null ? '' : s) .replace(/&/g, '&') .replace(/"/g, '"'); const voidEls = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr']); // Deepest-first: a rebuilt ancestor then captures already-restored children. const items = []; for (const entry of entries) { const el = document.querySelector('[data-hydrate-id="' + entry.id + '"]'); if (!el) continue; let depth = 0; for (let p = el.parentNode; p; p = p.parentNode) depth++; items.push({ entry, el, depth }); } items.sort((a, b) => b.depth - a.depth); for (const { entry, el: initialEl } of items) { const el = document.querySelector('[data-hydrate-id="' + entry.id + '"]') || initialEl; if (!el || !el.parentNode) continue; // Case 1: explicit subtree restoration (entry.html present) — // rebuild via outerHTML so the whole subtree mirrors source. if (typeof entry.html === 'string') { const tag = el.tagName.toLowerCase(); const finalAttrs = {}; // Current attrs, then the contract diff. const cur = el.attributes; for (let i = 0; i < cur.length; i++) { if (cur[i].name !== 'data-hydrate-id') finalAttrs[cur[i].name] = cur[i].value; } if (entry.attrs) { for (const name in entry.attrs) { const v = entry.attrs[name]; if (v === null) delete finalAttrs[name]; else finalAttrs[name] = v; } } const attrString = Object.keys(finalAttrs) .map((n) => n + '="' + escAttr(finalAttrs[n]) + '"') .join(' '); const isVoid = voidEls.has(tag); const newHTML = isVoid ? '<' + tag + ' ' + attrString + '>' : '<' + tag + ' ' + attrString + '>' + entry.html + ''; const tmp = document.createElement('div'); tmp.innerHTML = newHTML; const parsed = tmp.firstElementChild; if (parsed) { try { el.parentNode.replaceChild(parsed, el); } catch (_) { } } continue; } // Case 2: attribute-only diff. Reparse with merged attrs (so // special names like @click work); innerHTML preserved. if (!entry.attrs) continue; const tag = el.tagName.toLowerCase(); const finalAttrs = {}; const cur = el.attributes; for (let i = 0; i < cur.length; i++) { if (cur[i].name !== 'data-hydrate-id') finalAttrs[cur[i].name] = cur[i].value; } for (const name in entry.attrs) { const v = entry.attrs[name]; if (v === null) delete finalAttrs[name]; else finalAttrs[name] = v; } const attrString = Object.keys(finalAttrs) .map((n) => n + '="' + escAttr(finalAttrs[n]) + '"') .join(' '); const isVoid = voidEls.has(tag); const innerHTML = isVoid ? '' : el.innerHTML; const newHTML = isVoid ? '<' + tag + ' ' + attrString + '>' : '<' + tag + ' ' + attrString + '>' + innerHTML + ''; const tmp = document.createElement('div'); tmp.innerHTML = newHTML; const parsed = tmp.firstElementChild; if (parsed) { try { el.parentNode.replaceChild(parsed, el); } catch (_) { } } } blob.remove(); } /* * Reconcile baked x-for/x-if clones kept for crawlers — their