// RESPELL A REPORT — the edit `archilyzer reports respell` plans and applies // (lib/citations/respell.ts says what a respelling is). // // What changes, and how a reading shows: // - a citation's `quote` and the label of every `[…](cite:)` link: the // respelling as it says (bracketed when it asks) — these are the quotes; // - the rest of the report's own words (title, subtitle, summary, method, a // section's title and body, a claim's text, findings and gist, a timeline // entry's title and body, slide points and lines): plain — the report's words // are not a quote, so nothing is marked; // - `respellings`: the new ones merged in (same reading: heard forms joined), so // every quote check applies them from now on. // What does not: ids, links, sources (a document's words as the source has them), a // claim's `sourceQuote`, verdicts, dates. // // Pure: the caller reads and writes the files. import { applyRespellings, cleanRespellings, mergeRespellings, type Respelling } from "../citations/respell.mjs"; export { mergeRespellings }; import type { Report } from "./schema"; export type RespellChange = { // Where, as a path into the document ("sections[2].claims[0].findings"). at: string; before: string; after: string; }; // The same one-level-of-brackets link shape convertShared reads. const LINK_RE = /\[((?:[^[\]]|\[[^[\]]*\])*)\]\(\s*(]+>?(?:\s+"[^"]*")?)\s*\)/g; // Markdown: link labels of `cite:` links as quotes ("display"), everything else plain. export function respellMarkdown(md: string, respellings: readonly Respelling[]): string { let out = ""; let last = 0; for (const m of md.matchAll(LINK_RE)) { const at = m.index ?? 0; out += applyRespellings(md.slice(last, at), respellings, "plain"); const href = m[2]; const isCite = /^ { if (typeof value !== "string" || value === "") return value; const after = how === "markdown" ? respellMarkdown(value, rs) : applyRespellings(value, rs, how === "quote" ? "display" : "plain"); if (after !== value) changes.push({ at, before: value, after }); return after; }; out.title = fix("title", out.title, "plain")!; out.subtitle = fix("subtitle", out.subtitle, "plain"); out.summary = fix("summary", out.summary, "markdown"); out.method = fix("method", out.method, "markdown"); type Slide = { title?: string; points?: string[] }; const fixSlide = (at: string, sl: Slide | undefined) => { if (!sl) return; if (sl.title !== undefined) sl.title = fix(`${at}.title`, sl.title, "plain"); sl.points = sl.points?.map((p, k) => fix(`${at}.points[${k}]`, p, "markdown")!); }; if (out.slides) { fixSlide("slides", out.slides); if (out.slides.closing !== undefined) out.slides.closing = fix("slides.closing", out.slides.closing, "markdown"); } out.sections.forEach((s, i) => { s.title = fix(`sections[${i}].title`, s.title, "plain")!; fixSlide(`sections[${i}].slide`, s.slide); if (s.body !== undefined) s.body = fix(`sections[${i}].body`, s.body, "markdown"); s.claims?.forEach((c, j) => { const at = `sections[${i}].claims[${j}]`; if (c.title !== undefined) c.title = fix(`${at}.title`, c.title, "plain"); c.text = fix(`${at}.text`, c.text, "markdown")!; fixSlide(`${at}.slide`, c.slide); if (c.findings !== undefined) c.findings = fix(`${at}.findings`, c.findings, "markdown"); if (c.gist !== undefined) c.gist = fix(`${at}.gist`, c.gist, "markdown"); }); }); out.entries?.forEach((e, i) => { e.title = fix(`entries[${i}].title`, e.title, "plain")!; e.body = fix(`entries[${i}].body`, e.body, "markdown")!; }); for (const [id, c] of Object.entries(out.citations ?? {})) { if ("quote" in c && typeof c.quote === "string") { (c as { quote: string }).quote = fix(`citations.${id}.quote`, c.quote, "quote")!; } } if (rs.length) out.respellings = mergeRespellings(out.respellings, rs); return { report: out, changes }; }