import { createHash } from "node:crypto";
// what every page of a repo shows the same way (its sidebar) is stored once per content
// under the repo's url, and a page names the files it reads back as it hydrates
export const SHARED_DIR = "_shared";
export const sharedOf = (value) => {
const body = JSON.stringify(value);
return { hash: createHash("sha256").update(body).digest("hex").slice(0, 16), body };
};
export const sharedPath = (hash) => `${SHARED_DIR}/${hash}.json`;
// the newest copy of a shared value under a name that stays put (the sidebar of a ref), for
// a page the renderer didn't keep, which has no copy to name
export const LATEST_DIR = "_latest";
export const latestPath = (name) => `${LATEST_DIR}/${name}.json`;
// the routes a repo has pages for, relative to its url: the SPA loads those as documents and
// renders the rest itself
export const ROUTES = "routes";
// the repo's open ticket counts, which every page's tabs show
export const COUNTS = "counts";
const NAMED = /_shared\/[0-9a-f]{16}\.json/g;
// a page kept from an older render names an older copy of the same thing, so an incremental
// run matches a fresh one only with the names masked
export const maskShared = (html) => html.replace(NAMED, "_shared/-.json");
// the files a repo's pages name, which gc keeps
export const sharedNamed = (pages, latest = {}) =>
new Set([...Object.values(pages).flatMap((page) => page.shared ?? []), ...Object.values(latest)]);