// THE BOARD, as data: its groups and order, the moves the keys and drag make, // the export in the house format, and the words compare re-verify uses. // Pure and browser-safe (no node built-ins), so the page, the routes and the // tests agree. The store is cases.mjs. import { isoDate } from "./facets.mjs"; import { isLocalOnly } from "./rules.mjs"; /** The name of the group of quotes in no section. */ export const UNSORTED = "Unsorted"; /** * The board in order: Unsorted first (when it holds anything, or when there * are no sections at all), then each section with its quotes. * * @param {{ sections: { id: string, title: string }[], quotes: { id: string, section: string | null }[] }} doc */ export function groupsOf(doc) { const byId = new Map(doc.sections.map((s) => [s.id, { id: s.id, title: s.title, quotes: [] }])); const unsorted = { id: null, title: UNSORTED, quotes: [] }; for (const q of doc.quotes) (q.section && byId.get(q.section) ? byId.get(q.section) : unsorted).quotes.push(q); const out = [...byId.values()]; return unsorted.quotes.length || out.length === 0 ? [unsorted, ...out] : out; } /** Every group, Unsorted included even when empty (the moves walk it). */ function allGroups(doc) { const g = groupsOf(doc); return g[0]?.id === null ? g : [{ id: null, title: UNSORTED, quotes: [] }, ...g]; } const arrangement = (doc, groups) => ({ sections: doc.sections.map((s) => ({ id: s.id, title: s.title })), order: groups.flatMap((g) => g.quotes.map((q) => ({ id: q.id, section: g.id }))), }); /** * Move a quote one place down (+1) or up (-1) in board order; past the end of * its section it goes into the next (or the previous) one. Answers the new * arrangement for PATCH, or null when it cannot move. */ export function moveQuote(doc, qid, dir) { const groups = allGroups(doc).map((g) => ({ ...g, quotes: [...g.quotes] })); const gi = groups.findIndex((g) => g.quotes.some((q) => q.id === qid)); if (gi < 0) return null; const g = groups[gi]; const i = g.quotes.findIndex((q) => q.id === qid); const [q] = g.quotes.splice(i, 1); if (dir > 0) { if (i < g.quotes.length) g.quotes.splice(i + 1, 0, q); else if (gi < groups.length - 1) groups[gi + 1].quotes.unshift(q); else return null; } else { if (i > 0) g.quotes.splice(i - 1, 0, q); else if (gi > 0) groups[gi - 1].quotes.push(q); else return null; } return arrangement(doc, groups); } /** Put a quote in a section (null = Unsorted) at an index of it (drag and drop). */ export function placeQuote(doc, qid, section, index) { const groups = allGroups(doc).map((g) => ({ ...g, quotes: g.quotes.filter((q) => q.id !== qid) })); const q = doc.quotes.find((x) => x.id === qid); const to = groups.find((g) => g.id === (section ?? null)); if (!q || !to) return null; const at = Math.max(0, Math.min(to.quotes.length, Number.isInteger(index) ? index : to.quotes.length)); to.quotes.splice(at, 0, q); return arrangement(doc, groups); } /** Move a section one place down (+1) or up (-1); null when it cannot. */ export function moveSection(doc, sid, dir) { const i = doc.sections.findIndex((s) => s.id === sid); const j = i + (dir > 0 ? 1 : -1); if (i < 0 || j < 0 || j >= doc.sections.length) return null; const sections = doc.sections.map((s) => ({ id: s.id, title: s.title })); [sections[i], sections[j]] = [sections[j], sections[i]]; return { sections, order: doc.quotes.map((q) => ({ id: q.id, section: q.section })) }; } // ── the words ── /** Words as a re-verify compares them: case, punctuation and spacing aside. */ export function wordsOf(text) { return String(text ?? "") .toLowerCase() .replace(/[‘’ʼ`]/g, "'") .replace(/[^\p{L}\p{N}' ]+/gu, " ") .replace(/'+/g, "'") .replace(/\s+/g, " ") .trim(); } /** Do `inside`'s words hold `quote`'s, in order and whole? */ export function holdsWords(inside, quote) { const q = wordsOf(quote); if (!q) return false; return ` ${wordsOf(inside)} `.includes(` ${q} `); } // ── the export: cited markdown in the house format ── const clean = (s) => String(s ?? "").replace(/\s+/g, " ").trim(); // A claim never carries quotation marks (common's CLAIM_QUOTES_RULE): every // quoted span before a link becomes THAT link's quote. const claimText = (s) => clean(s).replace(/["“”]/g, "").replace(/[.:;,\s]+$/, ""); // Inside the quote, a double quote would end it early for the converter. const quoteText = (s) => clean(s).replace(/["“”]/g, "'"); /** The citation link of a quote, in the house format (facets.mjs citationText's label). */ export function citeLink(q) { const c = q.cite; const label = c.kind === "post" ? `post by ${clean(c.author || c.channelName)}, ${isoDate(c.date)}` : `${clean(c.title).replace(/[[\]]/g, "")} @ ${c.clock || "0:00"}`; return c.url ? `[${label}](${c.url})` : label; } /** One finding: the claim in plain words, the quote, its link; our note under it. */ export function findingOf(q) { const claim = claimText(q.claim); const lines = [`- ${claim ? `${claim}: ` : ""}"${quoteText(q.text)}" — ${citeLink(q)}`]; const asides = []; if (clean(q.note)) asides.push(clean(q.note).replace(/["“”]/g, "'")); if (!q.speakerChecked && q.cite.kind !== "post") asides.push("Speaker not checked"); if (q.verify?.status === "drifted") asides.push(`Drifted: the transcript read differently at the last re-verify (${(q.verify.at || "").slice(0, 10)})`); if (q.verify?.status === "missing") asides.push("Missing: the recording was not found at the last re-verify"); if (q.verify?.enOrig) asides.push(`The en-orig track differs here: check the audio`); if (asides.length) lines.push(` Note: ${asides.join(". ")}.`); return lines.join("\n"); } /** * The board as cited markdown, the shape a /sweep report has (it converts as * one: common's convertSweep): `# title`, the summary, a `##` per section with * one finding per quote, and `## Coverage`. * * @param {object} doc the case * @param {{ sourceLabel?: string, read?: number }} [ctx] */ export function exportMarkdown(doc, ctx = {}) { const out = [`# ${clean(doc.title)}`, ""]; const notes = String(doc.notes ?? "").trim(); if (notes) out.push(notes.replace(/^#+\s*/gm, ""), ""); const recordings = new Set(doc.quotes.map((q) => `${q.cite.channel}/${q.cite.video}`)); out.push( `${doc.quotes.length} ${doc.quotes.length === 1 ? "quote" : "quotes"} from ${recordings.size} ${recordings.size === 1 ? "source" : "sources"}, pinned on a research board${doc.scope.query ? ` from the search “${clean(doc.scope.query).replace(/["“”]/g, "'")}”` : ""}. The quotes are verbatim snapshots; the claims and notes are ours.`, "", ); const groups = groupsOf(doc).filter((g) => g.quotes.length); const named = doc.sections.length > 0; for (const g of groups) { out.push(`## ${g.id === null ? (named ? UNSORTED : "Findings") : clean(g.title)}`, ""); for (const q of g.quotes) out.push(findingOf(q)); out.push(""); } out.push("## Coverage", "", coverageLine(doc, ctx), ""); return out.join("\n"); } /** The coverage paragraph: the scope, what was searched and read, what is pinned. */ export function coverageLine(doc, ctx = {}) { const s = doc.scope; const where = ctx.sourceLabel || doc.search?.sourceLabel || (s.source ? s.source.replace(/^(local|remote|hub):/, "") : "an unrecorded source"); const scope = [ s.channels?.length ? `channels ${s.channels.join(", ")}` : "every channel", s.dateFrom || s.dateTo ? `${s.dateFrom ? `from ${s.dateFrom}` : ""}${s.dateFrom && s.dateTo ? " " : ""}${s.dateTo ? `to ${s.dateTo}` : ""}` : "", s.states?.length ? `states ${s.states.join(", ")}` : "", s.mediaType ? `${s.mediaType}s only` : "", s.posts ? "posts included" : "", ].filter(Boolean); const parts = [`Scope: ${where}, ${scope.join(", ")}.`]; const se = doc.search; if (se && se.total >= 0 && se.session) { parts.push( `Searched for ${se.query ? `“${se.query}”` : "the question"}: ${se.total} ${se.total === 1 ? "recording" : "recordings"}, ${se.complete ? "complete" : `PARTIAL (not reached: ${se.notReached.join(", ") || "unknown"})`}.`, ); if (Number.isFinite(ctx.read)) parts.push(`Read ${ctx.read} of ${se.total}.`); } else { parts.push("No search is recorded for this board."); } const drift = doc.quotes.filter((q) => q.verify?.status === "drifted" || q.verify?.status === "missing").length; const unchecked = doc.quotes.filter((q) => !q.speakerChecked && q.cite.kind !== "post").length; parts.push( `${doc.quotes.length} pinned.` + (drift ? ` ${drift} drifted or missing at the last re-verify.` : "") + (unchecked ? ` ${unchecked} with the speaker not checked.` : ""), ); return parts.join(" "); } /** Quotes from a local-only channel (Candace's): never in anything published. */ export const localOnlyQuotes = (doc) => doc.quotes.filter((q) => q.cite.localOnly || isLocalOnly(q.cite.channel)); /** A safe file name for the download. */ export const exportFileName = (doc) => `${doc.id}.md`;