// The Research results' facets, keys and citation text: pure and // browser-safe, so the server that builds a result, the page that filters it // and the tests agree. (The session key needs node:crypto: session.mjs.) /** * What a record's text is, from its `transcriptFormat` (the per-video * transcript.cues.json field the index spreads into every shard record): * `vtt` is the platform's captions, `whisper-json` / `chough-json` our own * transcription. A record without one (an older build) is unknown, never * guessed. * * @param {unknown} format * @returns {"transcript" | "captions" | "unknown"} */ export function trackOf(format) { if (format === "vtt") return "captions"; if (typeof format === "string" && /-json$/.test(format)) return "transcript"; return "unknown"; } /** "YYYYMMDD" → "YYYY", or "" when there is no date. */ export const yearOf = (d) => (typeof d === "string" && /^\d{4}/.test(d) ? d.slice(0, 4) : ""); /** "YYYYMMDD" → "YYYY-MM-DD" (as it is; anything else unchanged). */ export const isoDate = (d) => (typeof d === "string" && /^\d{8}$/.test(d) ? `${d.slice(0, 4)}-${d.slice(4, 6)}-${d.slice(6, 8)}` : d ?? ""); /** A hit's reading-log key: `/`, the slug the viewer uses. */ export const hitKey = (h) => `${h.channelSlug}/${h.videoId}`; /** * Facets over the result rows: per year (every year between the first and the * last, zeros kept, so the histogram's gaps are visible), per channel (most * first), per track and per state. * * @param {{ channelSlug: string, channelName: string, uploadDate: string, track: string, state: string }[]} hits */ export function facetsOf(hits) { const years = new Map(); const channels = new Map(); /** @type {Record} */ const tracks = {}; /** @type {Record} */ const states = {}; for (const h of hits) { const y = yearOf(h.uploadDate); if (y) years.set(y, (years.get(y) ?? 0) + 1); const c = channels.get(h.channelSlug) ?? { slug: h.channelSlug, name: h.channelName, n: 0 }; c.n += 1; channels.set(h.channelSlug, c); tracks[h.track] = (tracks[h.track] ?? 0) + 1; states[h.state] = (states[h.state] ?? 0) + 1; } const ys = [...years.keys()].map(Number).sort((a, b) => a - b); const span = []; for (let y = ys[0]; ys.length && y <= ys[ys.length - 1]; y++) span.push({ year: String(y), n: years.get(String(y)) ?? 0 }); return { years: span, channels: [...channels.values()].sort((a, b) => b.n - a.n || a.name.localeCompare(b.name)), tracks, states, }; } /** * A citation in the house format (mcp/src/instructions.ts; common's * authoringRules): the quoted words, then `[ @ <m:ss>](<moment url>)` * for a video, `[post by <author>, <date>](<url>)` for a post. A video with * no link (no viewer origin and no platform URL) is cited by its title and * time alone, never with an invented link. * * @param {{ quote?: string, title: string, clock?: string, url?: string | null, post?: { author: string, date: string } }} c */ export function citationText(c) { const quote = c.quote ? `"${c.quote.replace(/\s+/g, " ").trim()}" ` : ""; const label = c.post ? `post by ${c.post.author}, ${c.post.date}` : `${c.title} @ ${c.clock ?? "0:00"}`; return c.url ? `${quote}[${label}](${c.url})` : `${quote}${label}`; }