// Composes Kent's invisible software phone demo (from @kentcdodds/invisible-software, sources in // demo/) into one HTML document with the shared CSS and JS inlined. Images and fonts are either // data: URIs (asset = undefined: the offline dist/index.html embeds the document as an iframe srcdoc) // or URLs returned by asset(rel) (the hosted deck serves the document and each asset as separate // content-hashed files; see scripts/build.mjs and src/c/demo.js). // // Embedded, the page is transparent, never touches the URL, and ignores its own keys and clicks: // the deck drives it through window.__slideshow (hold, play, seekScene). import { readdirSync, readFileSync } from 'node:fs' import { join, extname } from 'node:path' const root = new URL('..', import.meta.url).pathname const dir = join(root, 'demo') const types = { '.png': 'image/png', '.svg': 'image/svg+xml', '.woff2': 'font/woff2', '.webp': 'image/webp', '.jpg': 'image/jpeg' } const dataUri = (rel) => { const type = types[extname(rel)] if (!type) throw new Error(`demo: no content type for ${rel}`) return `data:${type};base64,${readFileSync(join(dir, 'c', rel)).toString('base64')}` } export function buildDemo(asset) { const ref = (rel) => (asset ? asset(rel) : dataUri(rel)) const slideFiles = readdirSync(join(dir, 'slides')).filter((f) => f.endsWith('.html')).sort() let page = readFileSync(join(dir, 'deck.html'), 'utf8') .replace(/`) .replace('', () => slideFiles.map((f) => readFileSync(join(dir, 'slides', f), 'utf8').trim()).join('\n')) page = page .replace(/url\('((?:fonts|assets)\/[\w./-]+)'\)/g, (_, p) => `url('${ref(p)}')`) .replace(/(["'])(assets\/[\w.-]+\.(?:png|svg|jpg|webp))\1/g, (_, q, p) => `${q}${ref(p)}${q}`) // Embedded in the deck: transparent, no URL changes, no own input handling. page = page.replace('', ` `) const patches = [ ['function syncUrl(push = true) {', 'function syncUrl(push = true) {\n\treturn'], ["addEventListener('keydown', (e) => {", "addEventListener('keydown', (e) => {\n\treturn"], ["addEventListener('click', (e) => {", "addEventListener('click', (e) => {\n\treturn"], ["addEventListener('popstate', restoreFromUrl)", ''], ["addEventListener('hashchange', restoreFromUrl)", ''], // The deck owns the start frame: the demo holds its first frame once ready. ['hold(parseHash(location.hash), false)', 'hold(-1, false); window.__demoReady = true; window.dispatchEvent(new Event(\'demoready\'))'], // seekScene(label): a scene's first frame, for slides that open on a scene before its first step. ['window.__slideshow = { SLIDES,', "function seekScene(label) { cancelAnimationFrame(raf); state.playing = false; const si = SLIDES.findIndex((s) => s.label === label); seek(si, SLIDES[si].segs[0][0]) }\nwindow.__slideshow = { seekScene, play, SLIDES,"], ] for (const [from, to] of patches) { if (!page.includes(from)) throw new Error(`demo: patch target missing: ${from}`) page = page.replace(from, to) } const allowed = asset ? `data:|#|demo/` : `data:|#` const leftover = page.match(new RegExp(`(?:src|href)="(?!${allowed})[^"$]*"|url\\((?!['"]?(?:${allowed}))[^)]*\\)`)) if (leftover) throw new Error(`demo: external reference left: ${leftover[0].slice(0, 100)}`) return page }