424 lines · 19.4 KB · TypeScriptRaw
/** * Every page of the site: the chosen design (site-v2, its build.py ported here as it is), driven from the registry * (apps.json), the site's words (copy.ts) and the long texts (content/*.html). `renderSite()` returns each page's * path in dist/ and its HTML; build.ts writes them, the tests read them. */import { createHash } from "node:crypto";import { readFileSync } from "node:fs";import path from "node:path";import { fileURLToPath } from "node:url";import { APP_PAGES, APPS, type App, type HomeShot } from "./registry.ts";import { APP, FOOT, HOME, INVITE, LONG, NAV, NOT_FOUND, STATUS, YOURS } from "./copy.ts";import { withContact } from "./contact.ts";import { accountDeleteOn, forDelete } from "./switches.ts";import { LOOSE, QUOTE, RING, esc, keepTogether } from "./html.ts";import { status, waiting } from "./state.ts";export const ORIGIN = "https://cottondb.com";const SITE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");/** Live's account-delete switch, as this build follows it (switches.ts); renderSite sets it. */let deleteOn = accountDeleteOn();const content = (name: string) => forDelete(readFileSync(path.join(SITE_ROOT, "content", name), "utf8"), deleteOn);/** * A page's preview picture's address, with its content's version (SHARE CARD, 24 Nov 2026): WhatsApp and the other * apps that draw a shared link keep a picture by its address, so a card remade under the same name went on showing * the old one. A new picture is a new address. */export function ogImage(name: string): string { const bytes = readFileSync(path.join(SITE_ROOT, "static", "og", `${name}.png`)); return `${ORIGIN}/og/${name}.png?v=${createHash("sha256").update(bytes).digest("hex").slice(0, 10)}`;}/** The browser bar on pages that are not an app's. */const HOME_THEME: [string, string] = ["#f3f3f0", "#151311"];interface Head { title: string; description: string; /** The page's own path, for the canonical link and the preview. */ path: string; /** The preview picture in /og (1200×630). */ og: string; theme: [string, string]; bodyClass: string; /** The home page: its mark is the current page. */ home?: boolean;}function page(h: Head, main: string): string { const url = ORIGIN + h.path; const nav = NAV.map(([href, label]) => ` <a href="${href}">${label}</a>`).join("\n"); const apps = APPS.filter((a) => a.page).map((a) => `<a href="/${a.id}">${esc(a.name)}</a>`).join(""); const links = FOOT.links.map(([href, label]) => `<a href="${href}">${label}</a>`).join(""); return withContact( keepTogether(`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>${esc(h.title)}</title><meta name="description" content="${esc(h.description)}"><meta name="color-scheme" content="light dark"><meta name="theme-color" content="${h.theme[0]}" media="(prefers-color-scheme: light)"><meta name="theme-color" content="${h.theme[1]}" media="(prefers-color-scheme: dark)"><meta name="format-detection" content="telephone=no"><meta name="referrer" content="no-referrer"><link rel="canonical" href="${url}"><meta property="og:type" content="website"><meta property="og:site_name" content="Cotton"><meta property="og:title" content="${esc(h.title)}"><meta property="og:description" content="${esc(h.description)}"><meta property="og:url" content="${url}"><meta property="og:image" content="${ogImage(h.og)}"><meta property="og:image:width" content="1200"><meta property="og:image:height" content="630"><meta name="twitter:card" content="summary_large_image"><link rel="icon" href="/favicon.svg" type="image/svg+xml"><link rel="preload" href="/fonts/Fraunces.woff2" as="font" type="font/woff2" crossorigin><link rel="stylesheet" href="/cotton.css"></head><body class="${h.bodyClass}"><a class="skip" href="#main">Skip to the page</a><header class="top wrap"> <a class="mark" href="/"${h.home ? ' aria-current="page"' : ""}>Cotton</a> <nav aria-label="Pages">${nav} </nav></header>${main}<footer class="foot wrap"> ${LOOSE} <div class="foot-grid"> <div> <a class="mark" href="/">Cotton</a> <p class="made">${FOOT.made}</p> </div> <nav aria-label="Everything on this site"> ${apps} ${links} <a href="mailto:{{contact}}">{{contact}}</a> </nav> </div> <p class="legal"><span>© 2026 DECEMBERFM, INC</span><span>${FOOT.legal}</span></p></footer></body></html>`), );}/** A picture's size, read from its WebP header: VP8 (lossy), VP8L (lossless) or VP8X (extended). */export function webpSize(file: string): { w: number; h: number } { const b = readFileSync(file); const kind = b.toString("ascii", 12, 16); if (kind === "VP8 ") return { w: b.readUInt16LE(26) & 0x3fff, h: b.readUInt16LE(28) & 0x3fff }; if (kind === "VP8L") { const bits = b.readUInt32LE(21); return { w: (bits & 0x3fff) + 1, h: ((bits >> 14) & 0x3fff) + 1 }; } if (kind === "VP8X") return { w: 1 + b.readUIntLE(24, 3), h: 1 + b.readUIntLE(27, 3) }; throw new Error(`${file}: not a WebP picture`);}const shotFile = (a: App, f: string) => path.join(SITE_ROOT, "static", "shots", a.id, `${f}.webp`);function screen(a: App, file: string, alt: string, lazy: boolean, dark?: string): string { const { w, h } = webpSize(shotFile(a, file)); const set = (f: string) => `/shots/${a.id}/${f}.webp 1x, /shots/${a.id}/${f}@2x.webp 2x`; const img = `<img class="screen" src="/shots/${a.id}/${file}.webp" srcset="${set(file)}" width="${w}" height="${h}" alt="${esc(alt)}"${lazy ? " loading=lazy" : ""}>`; return dark ? `<picture><source media="(prefers-color-scheme: dark)" srcset="${set(dark)}">${img}</picture>` : img;}function icon(a: App, lazy = false, morph = true): string { return `<img class="icon" src="/icons/${a.id}.webp" width="256" height="256" alt=""${lazy ? " loading=lazy" : ""}${morph ? ` style="view-transition-name:icon-${a.id}"` : ""}>`;}/** The app's name as its sheet and its page wear it. *//** The app's short name with its touch, as the home page's sheets wear it. */function name(a: App): string { const n = esc(a.name); if (a.flourish === "ring") return `<span class="ring draw">${n}${RING}</span>`; if (a.flourish === "cursor") return `${n}<span class="cursor" aria-hidden="true"></span>`; if (a.flourish === "stop") return `${n}<span class="stop">.</span>`; return n;}function title(a: App, onHome: boolean): string { const name = esc(a.name); if (a.flourish === "ring") return `Cotton <span class="ring draw">${name}${onHome ? RING : RING.replace(' class="draw-path"', "")}</span>`; if (a.flourish === "cursor") return `${esc(a.full)}<span class="cursor" aria-hidden="true"></span>`; if (a.flourish === "stop") return `${esc(a.full)}<span class="stop">.</span>`; return esc(a.full);}const paras = (list: readonly string[], cls = "say") => list.map((x) => `\n <p class="${cls}">${x}</p>`).join("");/** One band of the home page: its heading, then what it says. */const band = (id: string, heading: string, body: string, cls = "band") => `<section class="${cls} wrap" id="${id}" aria-labelledby="${id}-h">\n <h2 id="${id}-h">${heading}</h2>${body}\n</section>`;/** The owner's photos with About me, each sized so nothing moves as it loads (cotton.css lays the row out). */const photos = () => `\n <div class="about-pics">${HOME.photos.map((p) => `<img src="${p.src}" width="${p.width}" height="${p.height}" alt="${esc(p.alt)}" loading=lazy>`).join("")}</div>`;function home(): string { const apps = APPS.filter((a) => a.page); const sheets = apps .map( (a) => ` <article class="sheet ${a.id}"> <div class="sheet-text"> ${a.flourish === "quote" ? `${QUOTE}\n ` : ""}<h2><a href="/${a.id}">${name(a)}</a></h2> ${status(a)} <p class="sub">${esc(a.line)}</p> </div> <div class="sheet-screens"><div class="rail">${a.home.map((s: HomeShot) => ` ${screen(a, s.file, s.alt, !s.eager, s.dark)}`).join("\n")} </div></div> </article>`, ) .join(""); const H = HOME; const L = H.letter; /** A row of the letter's apps: each icon with its name below, a link to the app's page where it has one. */ const row = (list: readonly { name: string; icon: string | null }[]) => `<ul class="letter-apps">${list .map(({ name: n, icon: src }) => { const a = APPS.find((x) => x.name === n && x.page); const pic = src ? `<img class="icon" src="${src}" width="256" height="256" alt="">` : `<span class="icon empty"></span>`; return `<li>${a ? `<a href="/${a.id}">${pic}<span>${esc(n)}</span></a>` : `<span>${pic}<span>${esc(n)}</span></span>`}</li>`; }) .join("")}</ul>`; return page( { title: H.title, description: H.description, path: "/", og: "home", theme: HOME_THEME, bodyClass: "home", home: true }, `<main id="main"><section class="letter wrap" aria-labelledby="letter-h"> <div class="letter-words"> <h1 id="letter-h">${esc(L.hello)}</h1>${L.says.map((x) => ` <p>${esc(x)}</p>`).join("\n")} </div> <div class="letter-rows"> <p class="letter-lead">${L.building.lead}</p> ${row(L.building.apps)} <p class="letter-lead">${L.next.lead}</p> ${row(L.next.apps)} </div></section><div class="wrap rule">${LOOSE}</div><section class="band wrap" id="apps" aria-labelledby="apps-h"><h2 id="apps-h">${H.apps.heading}</h2><div class="sheets">${sheets}</div>${paras([...(apps.some(waiting) ? [H.apps.tested] : []), H.apps.more])}</section><div class="wrap rule">${LOOSE}</div>${band("about", H.about.heading, `${paras(H.about.says)}${photos()}`)}</main>`, );}function appPage(a: App): string { const figs = a.shots .map((s, i) => `<figure>${screen(a, s.file, s.alt, i > 1)}</figure>`) .join(""); const others = APPS.filter((o) => o.page && o.id !== a.id) .map((o) => `<a href="/${o.id}">${icon(o, true)}<span>${esc(o.name)}<small>${esc(o.line.replace(/\.$/, ""))}</small></span></a>`) .join(""); const words = APP_PAGES[a.id]!; const fill = (t: string) => t .replace("{{why}}", words.why.trim() ? esc(words.why.trim()) : `[[blank: ${APP.whyToCome}]]`) .replace("{{has}}", esc(words.has.trim())) .replace("{{coming}}", esc(words.coming.trim())) .replace("{{account}}", esc(words.account.trim())); return page( { title: `${a.full}: ${a.line.replace(/\.$/, "").toLowerCase()}`, description: a.description, path: `/${a.id}`, og: a.id, theme: a.look.theme, bodyClass: `app ${a.id}`, }, `<main id="main"><section class="app-head wrap" id="get"> ${icon(a)} <h1>${title(a, false)}</h1> ${fill(`<p class="why">{{why}}</p>`)} ${status(a)}${waiting(a) ? `\n <p class="now">${STATUS.tested}</p>` : ""}</section><div class="strip" role="group" aria-label="Pictures of ${esc(a.full)}" tabindex="0">${figs}</div>${fill(`<section class="part wrap" id="has" aria-labelledby="has-h"> <h2 id="has-h">${APP.has}</h2> <p class="lead">{{has}}</p></section><section class="part wrap" id="coming" aria-labelledby="coming-h"> <h2 id="coming-h">${APP.coming}</h2> <p class="lead">{{coming}}</p></section><section class="part plain wrap" id="account" aria-label="${APP.account}"> <p class="lead">{{account}}</p></section>`)}<section class="part wrap" aria-labelledby="others-h"> <h2 id="others-h">${APP.others}</h2> <div class="others">${others}</div></section></main>`, );}export const slug = (s: string): string => s.toLowerCase().replace(/<[^>]+>/g, "").replace(/&[a-z]+;/g, "").replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");/** * A long text carried over from the first site (the policy, terms, help), drawn as the design draws its own long * page: the title and opening in the page head, then one chapter per section (its heading beside its text), with a * list of the sections when there are more than three. Every section keeps its anchor; a section's own id wins. */function longPage(file: "privacy" | "terms" | "support", p: string): string { const text = content(`${file}.html`); const h1 = /<h1>(.*?)<\/h1>/.exec(text)![1]!; const rest = text.slice(text.indexOf("</h1>") + 5); const firstH2 = rest.indexOf("<h2"); const opening = rest.slice(0, firstH2).trim(); const chapters: { id: string; title: string; body: string }[] = []; for (const m of rest.slice(firstH2).split(/(?=<h2\b)/)) { const hm = /^<h2(?: id="([^"]+)")?>(.*?)<\/h2>/.exec(m.trim())!; chapters.push({ id: hm[1] ?? slug(hm[2]!), title: hm[2]!, body: m.trim().slice(hm[0].length).trim() }); } // The opening: an "Updated …" line as the page's date, then its sentences as the lede. const paras = [...opening.matchAll(/<p>([\s\S]*?)<\/p>/g)].map((m) => m[1]!); const updated = paras.find((x) => /^Updated /.test(x)); const lede = paras.filter((x) => x !== updated); const toc = chapters.length > 3 ? `\n <nav class="toc" aria-label="${LONG.toc}"><p>${LONG.toc}</p><ul>${chapters.map((c) => `<li><a href="#${c.id}">${c.title}</a></li>`).join("")}</ul></nav>` : ""; const head = `<section class="page-head wrap"> <h1>${h1}</h1>${updated ? `\n <p class="updated">${updated}</p>` : ""}${lede.map((x) => `\n <p class="lede">${x}</p>`).join("")}${toc}</section>`; const body = chapters .map((c) => `<section class="chapter" id="${c.id}">\n <h2>${c.title}</h2>\n <div class="text">\n${c.body}\n </div>\n</section>`) .join("\n\n"); const words = LONG[file]; return page( { title: words.title, description: words.description, path: p, og: "home", theme: HOME_THEME, bodyClass: "long" }, `\n<main id="main">\n${head}\n\n<div class="wrap">\n\n${body}\n\n</div>\n</main>\n`, );}/** * /delete (Play asks that deleting an account be reachable on the web without the app): the privacy policy's own * chapter on deleting, as it reads for live's switch (asking by email while deleting is off; the apps and the web * page once it is on), so the two never say different things. Its h3s become the page's chapters. While the switch * is on, live's door answers /delete with its own form (apps/business/src/delete_page.ts) and this page is the * site's copy of the same words. */function deletePage(): string { const policy = content("privacy.html"); const from = policy.indexOf("<h2>Deleting your data and your account</h2>"); const to = policy.indexOf("<h2", from + 4); const chapter = policy.slice(from, to).replace(/^<h2>.*?<\/h2>/, "").trim(); const firstH3 = chapter.indexOf("<h3"); const lede = [...chapter.slice(0, firstH3).matchAll(/<p>([\s\S]*?)<\/p>/g)].map((m) => m[1]!); const sections = chapter .slice(firstH3) .split(/(?=<h3\b)/) .map((m) => { const hm = /^<h3(?: id="([^"]+)")?>(.*?)<\/h3>/.exec(m.trim())!; return `<section class="chapter" id="${hm[1] ?? slug(hm[2]!)}">\n <h2>${hm[2]}</h2>\n <div class="text">\n${m.trim().slice(hm[0].length).trim()}\n </div>\n</section>`; }) .join("\n\n"); const words = LONG.delete; const head = `<section class="page-head wrap">\n <h1>${words.h1}</h1>${lede.map((x) => `\n <p class="lede">${x}</p>`).join("")}\n</section>`; return page( { title: words.title, description: words.description, path: "/delete", og: "home", theme: HOME_THEME, bodyClass: "long" }, `\n<main id="main">\n${head}\n\n<div class="wrap">\n\n${sections}\n\n</div>\n</main>\n`, );}/** Every page: its file in dist/ and its HTML. *//** * /listings, the preview's only (build.ts --preview): each app's store words as they wait in assets/store, the Play * and App Store fields one after the other with their lengths, for the owner to read before any is pasted in a * console. Linked from nowhere; never in the live build. */const STORE_ROOT = path.join(SITE_ROOT, "../../assets/store");const LISTED: [string, string][] = [["Cotton Days", "days"], ["Cotton Words", "words"], ["Cotton Story", "story"], ["Cotton Comms", "comms"]];const FIELDS: [string, string, string, number][] = [ ["Google Play", "play/{app}/metadata/en-US/title.txt", "Title", 30], ["Google Play", "play/{app}/metadata/en-US/short_description.txt", "Short description", 80], ["Google Play", "play/{app}/metadata/en-US/full_description.txt", "Full description", 4000], ["App Store", "appstore/{app}/name.txt", "Name", 30], ["App Store", "appstore/{app}/subtitle.txt", "Subtitle", 30], ["App Store", "appstore/{app}/promotional_text.txt", "Promotional text", 170], ["App Store", "appstore/{app}/description.txt", "Description", 4000], ["App Store", "appstore/{app}/keywords.txt", "Keywords", 100],];export function listingsPage(): string { const read = (rel: string): string | null => { try { return readFileSync(path.join(STORE_ROOT, rel), "utf8").trim(); } catch { return null; } }; const body = LISTED.map(([full, app]) => { let store = ""; const parts = FIELDS.map(([where, rel, label, most]) => { const text = read(rel.replace("{app}", app)); const head = where !== store ? `\n <h3>${where}</h3>` : ""; store = where; if (text === null) return `${head}\n <p><strong>${label}</strong>: not written yet.</p>`; const n = [...text].length; const lines = text.split(/\n+/).map((l) => `<p>${esc(l)}</p>`).join("\n"); return `${head}\n <p><strong>${label}</strong> (${n} of ${most})</p>\n <blockquote>\n${lines}\n </blockquote>`; }).join(""); return `<section class="chapter" id="${app}">\n <h2>${full}</h2>\n <div class="text">${parts}\n </div>\n</section>`; }).join("\n\n"); const toc = `<nav class="toc" aria-label="Apps"><p>Apps</p><ul>${LISTED.map(([f, a]) => `<li><a href="#${a}">${f}</a></li>`).join("")}</ul></nav>`; return page( { title: "Store listings", description: "The store words for each Cotton app, as they wait to be pasted.", path: "/listings", og: "home", theme: HOME_THEME, bodyClass: "long" }, `\n<main id="main">\n<section class="page-head wrap">\n <h1>Store listings</h1>\n <p class="lede">The preview only: each app's words for Google Play and the App Store, as they wait in the repository. Nothing here is live.</p>\n ${toc}\n</section>\n\n<div class="wrap">\n\n${body}\n\n</div>\n</main>\n`, );}export function renderSite(opts: { accountDelete?: boolean } = {}): Record<string, string> { deleteOn = opts.accountDelete ?? accountDeleteOn(); const out: Record<string, string> = { "index.html": home(), "yours.html": page( { title: YOURS.title, description: YOURS.description, path: "/yours", og: "home", theme: HOME_THEME, bodyClass: "long" }, `\n<main id="main">\n${content("yours.html")}</main>\n`, ), "privacy.html": longPage("privacy", "/privacy"), "terms.html": longPage("terms", "/terms"), "support.html": longPage("support", "/support"), "delete.html": deletePage(), "invite.html": page( { title: INVITE.title, description: INVITE.description, path: "/c", og: "home", theme: HOME_THEME, bodyClass: "long" }, `\n<main id="main">\n<section class="page-head wrap">\n <h1>${INVITE.h1}</h1>\n <p class="lede">${INVITE.text}</p>\n <ol>\n${INVITE.steps.map((s) => ` <li>${s}</li>`).join("\n")}\n </ol>\n <p><a href="/comms#get">${INVITE.get}</a></p>\n</section>\n</main>\n`, ), "404.html": page( { title: NOT_FOUND.title, description: NOT_FOUND.description, path: "/404", og: "home", theme: HOME_THEME, bodyClass: "long" }, `\n<main id="main">\n<section class="page-head wrap">\n <h1>${NOT_FOUND.h1}</h1>\n <p class="lede">${NOT_FOUND.text}</p>\n</section>\n</main>\n`, ), }; for (const a of APPS) if (a.page) out[`${a.id}.html`] = appPage(a); return out;}