/**
* 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)}Skip to the pageCotton
${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 = ``;
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
`,
)
.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 }[]) =>
`
${list
.map(({ name: n, icon: src }) => {
const a = APPS.find((x) => x.name === n && x.page);
const pic = src ? `` : ``;
return `
`,
);
}
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 = /
(.*?)<\/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(/
\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("
\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
\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.