// content-store capsule — helpers for the site-content blob.
//
// DEFAULT_DATA is a templated value: at reconstruct time the capsule tool
// replaces the marker below with the contents of site-data.json. The
// generated file is committed to the output site so the runtime is plain
// ESM with no build step.
export const DEFAULT_DATA = /* @CAPSULE_DATA */ {};
const KEY = "site:data:v1";
export async function readData(env) {
if (!env.YL_DATA) return DEFAULT_DATA;
const raw = await env.YL_DATA.get(KEY);
if (!raw) return DEFAULT_DATA;
try {
return JSON.parse(raw);
} catch {
return DEFAULT_DATA;
}
}
export async function writeData(env, data) {
if (!env.YL_DATA) throw new Error("KV not bound");
await env.YL_DATA.put(KEY, JSON.stringify(data));
}
export function nextWorkNum(works) {
let max = 0;
for (const w of works) {
const n = parseInt(w.num, 10);
if (!isNaN(n) && n > max) max = n;
}
return String(max + 1).padStart(3, "0");
}