// WHO PUBLISHES A SET OF REPORTS — the one thing the reports stage // (./composeReports.ts), prepare (./reportMedia.ts), export // (./reportExports.ts) and check (bin/reports-check.ts) need to know about // whoever publishes the reports they work on (release 26, // plans/reports-on-homepage.md, "The model"): // // a SITE (its pages) the ARTICLES (the homepage) // files sites//reports// reports// // published site.json `pages` reports/index.json, narrowed // to `audience: "public"` for a // public build // may cite its `channels` + `citedChannels` any channel in the corpus (D4) // posts the visibility rule, by the the same rule, by the // site's audience build's audience // corpus link its own corpus, relative (none the listed public site that // on a cited site or for a publishes the channel, // cited-only channel) absolute; none without one // served under /pages// /articles// // staging .export-index/sites// .export-index/articles/ // history identity the site's title, PROJECT_NAME, // noreply@.invalid noreply@articles.invalid // // A host is built once per run and carries no I/O of its own but the // articles' corpus check (a channel directory exists). import { existsSync, statSync } from "node:fs"; import { X_TRENDING_NOT_CITABLE } from "../lib/xTrending/config"; import { isXTrendingLibrarySync } from "../lib/xTrending/library-server"; import path from "node:path"; import type { Paths } from "../lib/paths"; import { PROJECT_NAME, PROJECT_URL } from "../lib/project"; import { isCitedOnlyChannel, isCitedSite, isPrivateSite, notInSiteMessage, parseSiteUrl, siteCitableChannelSlugs, type Site, } from "../lib/siteSchema"; import { publishedArticleIds } from "../lib/report/registry"; import { ARTICLES_REPORTS_BASE, SITE_REPORTS_BASE, type ReportBase } from "../lib/report/routes"; import { isReportId, type ReportAudience } from "../lib/report/schema"; import { articleReportStore, siteReportStore, storeReportFile, type ReportStore } from "../lib/report/store"; export type ReportHost = ReportStore & { // Where the build serves these reports (lib/report/routes.ts): a site's // `/pages/`, the homepage's `/articles/`. Every published path and href of // theirs hangs off it. base: ReportBase; // A history commit's author (./reportHistory.ts) and an export's heading. title: string; // The public base a feed, an export's links and the history clone use // (absolute, no trailing slash), or undefined: none. siteUrl?: string; // The site's accent (slides), when it has one. accent?: unknown; // Whose post-visibility rule applies (lib/postsVisibility.ts). audience: ReportAudience; // The published reports, in order. published: readonly string[]; // How a message names the list: `the site lists report "x"`. listedBy: (reportId: string) => string; // Why a channel may not be cited here, or null when it may. citableProblem: (slug: string) => string | null; // Where a moment's record opens in an archive: "" (this site's own corpus, // a site-root link), an absolute archive base, or null (no link). corpusBase: (slug: string) => string | null; // The site, for a site host. site?: Site; }; // A site's pages. export function siteReportHost( paths: Pick & Partial>, site: Site, ): ReportHost { const citable = siteCitableChannelSlugs(site); // An X trending library is never citable (lane x-trending): the site writer // refuses one as a member, and a site.json written by hand is caught here. const library = new Map(); const isLibrary = (slug: string) => { if (!library.has(slug)) library.set(slug, isXTrendingLibrarySync(paths.channelsDir, slug)); return library.get(slug) === true; }; const cited = isCitedSite(site); return { ...siteReportStore(paths, site.siteId), base: SITE_REPORTS_BASE, title: site.siteTitle?.trim() || site.siteId, siteUrl: parseSiteUrl(site.siteUrl), accent: site.accent, audience: isPrivateSite(site) ? "private" : "public", published: site.pages ?? [], listedBy: (id) => `the site lists report "${id}"`, citableProblem: (slug) => isLibrary(slug) ? X_TRENDING_NOT_CITABLE(slug) : citable.has(slug) ? null : notInSiteMessage(slug), // No corpus page to link a moment to: a cited site publishes none, and a // cited-only channel (`citedChannels`) is not in the site's corpus. corpusBase: (slug) => (cited || isCitedOnlyChannel(site, slug) ? null : ""), site, }; } export type ArticlesHostOptions = { // The build's audience: "public" carries only `audience: "public"` articles, // "private" every published one. Default "private". audience?: ReportAudience; // The homepage's public base. Default PROJECT_URL; null = none. siteUrl?: string | null; // channel slug → the base URL of the listed public site that publishes it // (the homepage's `channel-sites.json` with each site's `siteUrl`); a // moment of a channel it does not answer for gets no archive link. channelSiteUrl?: (slug: string) => string | undefined; // The published ids, when the caller has them (a check of named drafts); // default index.json narrowed to the audience. published?: readonly string[]; }; export function articleNotInCorpusMessage(slug: string): string { return `channel "${slug}" is not a channel of the corpus (no channels/${slug}/)`; } // The articles, as the homepage publishes them. export async function articlesReportHost( paths: Pick, opts: ArticlesHostOptions = {}, ): Promise { const audience = opts.audience ?? "private"; const isChannel = new Map(); const library = new Map(); return { ...articleReportStore(paths), base: ARTICLES_REPORTS_BASE, title: PROJECT_NAME, siteUrl: opts.siteUrl === null ? undefined : (parseSiteUrl(opts.siteUrl ?? PROJECT_URL) ?? undefined), audience, published: opts.published ?? (await publishedArticleIds(audience, paths)), listedBy: (id) => `reports/index.json lists article "${id}"`, citableProblem: (slug) => { // The whole corpus is citable — but never an X trending library, which // is private (lane x-trending). if (!library.has(slug)) library.set(slug, isXTrendingLibrarySync(paths.channelsDir, slug)); if (library.get(slug)) return X_TRENDING_NOT_CITABLE(slug); if (!isChannel.has(slug)) { let dir = false; try { dir = /^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(slug) && statSync(path.join(paths.channelsDir, slug)).isDirectory(); } catch { dir = false; } isChannel.set(slug, dir); } return isChannel.get(slug) ? null : articleNotInCorpusMessage(slug); }, corpusBase: (slug) => parseSiteUrl(opts.channelSiteUrl?.(slug)) ?? null, }; } // A record's link into an archive: `/?v=&…`, or undefined when // the host has none for the channel. export function hostCorpusLink(host: ReportHost, channel: string, params: Record): string | undefined { const base = host.corpusBase(channel); if (base === null) return undefined; return `${base}/?${new URLSearchParams(params).toString()}`; } // The articles as the tools that WORK on them see them — prepare, export, // check, the editor's Articles tab: every published article (the private // build's set, which holds the public one's), plus each id in `include` that // is not published yet (a draft being readied). Its prepared media and // exports therefore serve both homepage builds. An included id with no // report.json is refused. // // `only: true` (release 27 S7) is `include` alone, published or not: the // host one article's EXPORT is resolved over, so another article's gap does // not refuse it. Never prepare over it: the articles' media cache is one // directory, and a prepare prunes every clip its run does not cite. export async function articlesWorkHost( paths: Pick, include: readonly string[] = [], opts: { only?: boolean } = {}, ): Promise { for (const id of include) { if (!isReportId(id)) throw new Error(`"${id}" is not an article id`); if (!existsSync(storeReportFile(articleReportStore(paths), id))) { throw new Error(`no article "${id}" (no reports/${id}/report.json)`); } } if (opts.only) return articlesReportHost(paths, { audience: "private", published: [...new Set(include)] }); const published = await publishedArticleIds("private", paths); return articlesReportHost(paths, { audience: "private", published: [...published, ...include.filter((id) => !published.includes(id))], }); } // The problems that are `articleId`'s own: a problem of its report, or of a // moment one of its citations cites (`#`). export function isArticleProblem(p: { report?: string; citations?: readonly string[] }, articleId: string): boolean { return p.report === articleId || (p.citations ?? []).some((c) => c.startsWith(`${articleId}#`)); }