/** * 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]) => ` ${label}`).join("\n"); const apps = APPS.filter((a) => a.page).map((a) => `${esc(a.name)}`).join(""); const links = FOOT.links.map(([href, label]) => `${label}`).join(""); return withContact( keepTogether(` ${esc(h.title)}
Cotton
${main} `), ); } /** 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 = `${esc(alt)}`; return dark ? `${img}` : img; } function icon(a: App, lazy = false, morph = true): string { return ``; } /** 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 `${n}${RING}`; if (a.flourish === "cursor") return `${n}`; if (a.flourish === "stop") return `${n}.`; return n; } function title(a: App, onHome: boolean): string { const name = esc(a.name); if (a.flourish === "ring") return `Cotton ${name}${onHome ? RING : RING.replace(' class="draw-path"', "")}`; if (a.flourish === "cursor") return `${esc(a.full)}`; if (a.flourish === "stop") return `${esc(a.full)}.`; return esc(a.full); } const paras = (list: readonly string[], cls = "say") => list.map((x) => `\n

${x}

`).join(""); /** One band of the home page: its heading, then what it says. */ const band = (id: string, heading: string, body: string, cls = "band") => `
\n

${heading}

${body}\n
`; /** The owner's photos with About me, each sized so nothing moves as it loads (cotton.css lays the row out). */ const photos = () => `\n
${HOME.photos.map((p) => `${esc(p.alt)}`).join("")}
`; function home(): string { const apps = APPS.filter((a) => a.page); const sheets = apps .map( (a) => `
${a.flourish === "quote" ? `${QUOTE}\n ` : ""}

${name(a)}

${status(a)}

${esc(a.line)}

${a.home.map((s: HomeShot) => ` ${screen(a, s.file, s.alt, !s.eager, s.dark)}`).join("\n")}
`, ) .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 }[]) => ``; return page( { title: H.title, description: H.description, path: "/", og: "home", theme: HOME_THEME, bodyClass: "home", home: true }, `

${esc(L.hello)}

${L.says.map((x) => `

${esc(x)}

`).join("\n")}

${L.building.lead}

${row(L.building.apps)}

${L.next.lead}

${row(L.next.apps)}
${LOOSE}

${H.apps.heading}

${sheets}
${paras([...(apps.some(waiting) ? [H.apps.tested] : []), H.apps.more])}
${LOOSE}
${band("about", H.about.heading, `${paras(H.about.says)}${photos()}`)}
`, ); } function appPage(a: App): string { const figs = a.shots .map((s, i) => `
${screen(a, s.file, s.alt, i > 1)}
`) .join(""); const others = APPS.filter((o) => o.page && o.id !== a.id) .map((o) => `${icon(o, true)}${esc(o.name)}${esc(o.line.replace(/\.$/, ""))}`) .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}`, }, `
${icon(a)}

${title(a, false)}

${fill(`

{{why}}

`)} ${status(a)}${waiting(a) ? `\n

${STATUS.tested}

` : ""}
${figs}
${fill(`

${APP.has}

{{has}}

${APP.coming}

{{coming}}

{{account}}

`)}

${APP.others}

${others}
`, ); } 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>/.exec(text)![1]!; const rest = text.slice(text.indexOf("

") + 5); const firstH2 = rest.indexOf("(.*?)<\/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(/

([\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

` : ""; const head = `

${h1}

${updated ? `\n

${updated}

` : ""}${lede.map((x) => `\n

${x}

`).join("")}${toc}
`; const body = chapters .map((c) => `
\n

${c.title}

\n
\n${c.body}\n
\n
`) .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
\n${head}\n\n
\n\n${body}\n\n
\n
\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("

Deleting your data and your account

"); const to = policy.indexOf(".*?<\/h2>/, "").trim(); const firstH3 = chapter.indexOf("([\s\S]*?)<\/p>/g)].map((m) => m[1]!); const sections = chapter .slice(firstH3) .split(/(?= { const hm = /^(.*?)<\/h3>/.exec(m.trim())!; return `
\n

${hm[2]}

\n
\n${m.trim().slice(hm[0].length).trim()}\n
\n
`; }) .join("\n\n"); const words = LONG.delete; const head = `
\n

${words.h1}

${lede.map((x) => `\n

${x}

`).join("")}\n
`; return page( { title: words.title, description: words.description, path: "/delete", og: "home", theme: HOME_THEME, bodyClass: "long" }, `\n
\n${head}\n\n
\n\n${sections}\n\n
\n
\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

${where}

` : ""; store = where; if (text === null) return `${head}\n

${label}: not written yet.

`; const n = [...text].length; const lines = text.split(/\n+/).map((l) => `

${esc(l)}

`).join("\n"); return `${head}\n

${label} (${n} of ${most})

\n
\n${lines}\n
`; }).join(""); return `
\n

${full}

\n
${parts}\n
\n
`; }).join("\n\n"); const toc = ``; 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
\n
\n

Store listings

\n

The preview only: each app's words for Google Play and the App Store, as they wait in the repository. Nothing here is live.

\n ${toc}\n
\n\n
\n\n${body}\n\n
\n
\n`, ); } export function renderSite(opts: { accountDelete?: boolean } = {}): Record { deleteOn = opts.accountDelete ?? accountDeleteOn(); const out: Record = { "index.html": home(), "yours.html": page( { title: YOURS.title, description: YOURS.description, path: "/yours", og: "home", theme: HOME_THEME, bodyClass: "long" }, `\n
\n${content("yours.html")}
\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
\n
\n

${INVITE.h1}

\n

${INVITE.text}

\n
    \n${INVITE.steps.map((s) => `
  1. ${s}
  2. `).join("\n")}\n
\n

${INVITE.get}

\n
\n
\n`, ), "404.html": page( { title: NOT_FOUND.title, description: NOT_FOUND.description, path: "/404", og: "home", theme: HOME_THEME, bodyClass: "long" }, `\n
\n
\n

${NOT_FOUND.h1}

\n

${NOT_FOUND.text}

\n
\n
\n`, ), }; for (const a of APPS) if (a.page) out[`${a.id}.html`] = appPage(a); return out; }