// A PIECE's id (plans/umtool-redesign.md, "Data and engine rules"). // // A piece is the unit the operator works on: an article plus the video // project(s) made from it, or a project that belongs to no article. Its id is // NAMESPACED, because the bare spellings collide: `resolveTarget` // (lib/annotations/targets.mjs) reads a two-segment `x/y` as an article when // such a report exists and as a project otherwise -- and a project id is a // path, so `articles/zeta` can be either. // // a:/ an article; `site` may be `articles` (the store's) // p: a project under REPORTS_ROOT, by its path id // c: a research case (reserved: research boards, R2) // s: a recurring report's series (R5): its editions are // articles of the store, the series is their piece // // Pure, no imports with side effects: a client component may parse an id. /** A site or report id: lib/paths.mjs SEGMENT_RE (a test holds them equal). */ export const PIECE_SEGMENT_RE = /^[a-z0-9][a-z0-9-]{0,63}$/; /** One segment of a project id: lib/projects/walk.mjs isSegment (a test holds them equal). */ const PROJECT_SEGMENT_RE = /^[A-Za-z0-9][A-Za-z0-9._-]*$/; const projectSegment = (v) => PROJECT_SEGMENT_RE.test(v) && !v.includes(".."); export const PIECE_KINDS = ["article", "project", "case", "series"]; const PREFIX = { article: "a", project: "p", case: "c", series: "s" }; /** * Parse a piece id, or null when it is not one. Never guesses: a bare * `site/report` or project id is null. * * @param {unknown} id * @returns {{ kind: "article", site: string, report: string } | { kind: "project", project: string } | { kind: "case", case: string } | { kind: "series", series: string } | null} */ export function parsePieceId(id) { if (typeof id !== "string" || id.length > 512) return null; const m = /^([apcs]):(.+)$/.exec(id); if (!m) return null; const [, ns, rest] = m; if (ns === "a") { const parts = rest.split("/"); if (parts.length !== 2 || !parts.every((s) => PIECE_SEGMENT_RE.test(s))) return null; return { kind: "article", site: parts[0], report: parts[1] }; } if (ns === "p") { const parts = rest.split("/"); if (!parts.length || !parts.every(projectSegment)) return null; return { kind: "project", project: rest }; } if (!PIECE_SEGMENT_RE.test(rest)) return null; return ns === "s" ? { kind: "series", series: rest } : { kind: "case", case: rest }; } /** True when `id` is a piece id. */ export const isPieceId = (id) => parsePieceId(id) !== null; /** * The id for a parsed piece. Throws on parts that would not parse back: an id * that cannot round-trip is a bug at the caller, not a value to pass along. * * @param {{ kind: "article", site: string, report: string } | { kind: "project", project: string } | { kind: "case", case: string } | { kind: "series", series: string }} p */ export function formatPieceId(p) { const id = p?.kind === "article" ? `${PREFIX.article}:${p.site}/${p.report}` : p?.kind === "project" ? `${PREFIX.project}:${p.project}` : p?.kind === "case" ? `${PREFIX.case}:${p.case}` : p?.kind === "series" ? `${PREFIX.series}:${p.series}` : null; if (id === null || !isPieceId(id)) throw new Error(`not a piece: ${JSON.stringify(p)}`); return id; } /** `a:/`. */ export const articlePieceId = (site, report) => formatPieceId({ kind: "article", site, report }); /** `p:`. */ export const projectPieceId = (project) => formatPieceId({ kind: "project", project }); /** `c:`. */ export const casePieceId = (slug) => formatPieceId({ kind: "case", case: slug }); /** `s:`. */ export const seriesPieceId = (id) => formatPieceId({ kind: "series", series: id }); /** * An article decision's `project` (`sites//`, lib/decisions.ts * noteDecisions) back to its article, or null. Only meaningful for a row whose * `projectKind` is "article": a project may itself live under a `sites/` folder. * * @param {string} project */ export function articleOfDecisionProject(project) { const m = /^sites\/([^/]+)\/([^/]+)$/.exec(String(project ?? "")); if (!m || !PIECE_SEGMENT_RE.test(m[1]) || !PIECE_SEGMENT_RE.test(m[2])) return null; return { site: m[1], report: m[2] }; }