// The cite follows the edge (UI notes 2026-10-30, #5). // // A clip's `cite` is the second the deck's clock prints, and its `citeUrl`'s // `t=` is the second the QR opens. Both were written when the clip was found, // against the window it had then. Tightening moved `start` and left them // behind: on the Ember cut the clock and the QR ran up to 78 s early on 16 // clips, and it was caught from a still. // // So the window writer (manifest.mjs patchClipEntry) moves them when an edge // move leaves them outside what plays, and the check reports any left there // (`clip-cite-off`, lib/projects/report.mjs). One rule, here, for both. // // A cite a few seconds BEFORE the clip is a lead-in, not a defect: the Ember // cut cites up to 5 s ahead on purpose (the QR lands just before the line). // More than CITE_LEAD_MAX before, or any second after the clip ends, names a // moment the clip does not play. // // A citeUrl is ours to move only when it names THIS recording -- the archive's // `?v=/` (Twitch's record id is `v`), or the platform's own // `watch?v=` / `youtu.be/`. A mirror's clock is not this one's, so its // `t=` is left as written and never judged. // // Plain ESM: the CLI runs it under bare node. /** Seconds a cite may sit before the clip and still be a lead-in. */ export const CITE_LEAD_MAX = 5; const EPS = 0.02; /** What plays: the cut when the clip has the pair (the build honours the pair or neither), else the extent. */ export function playedSpan(e) { const cs = Number(e?.cutStart); const ce = Number(e?.cutEnd); if (e?.cutStart != null && e?.cutEnd != null && Number.isFinite(cs) && Number.isFinite(ce)) return { from: cs, to: ce }; return { from: Number(e?.start), to: Number(e?.end) }; } /** The clock's second: `cite`, else the clip's start (attribution.mjs's rule). */ export const clockOf = (e) => (e?.cite ?? e?.start ?? 0); /** * Where a second sits against what plays: 0 inside (a lead-in of up to * CITE_LEAD_MAX counts as inside), negative N when it is N s too early * (measured from the clip's first played second), positive N when N s after * the clip ends. */ export function offsetFrom(at, span, lead = CITE_LEAD_MAX) { if (!Number.isFinite(at) || !Number.isFinite(span.from) || !Number.isFinite(span.to)) return 0; if (at < span.from - lead - EPS) return -(span.from - at); if (at > span.to + EPS) return at - span.to; return 0; } /** A `t=` value in seconds: `123`, `123.5`, `123s`, `1h2m3s`, `2m3s`; null when it is none of those. */ export function parseT(raw) { const s = String(raw ?? "").trim(); if (/^\d+(\.\d+)?s?$/.test(s)) return Number(s.replace(/s$/, "")); const m = /^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?$/.exec(s); if (m && (m[1] || m[2] || m[3])) return Number(m[1] ?? 0) * 3600 + Number(m[2] ?? 0) * 60 + Number(m[3] ?? 0); return null; } /** Does `u` name the clip's own recording (so the same clock)? */ export function sameRecording(u, e, channelSlug = null) { const vid = String(e?.video ?? ""); if (!vid) return false; const chan = e?.channel ?? channelSlug; const v = u.searchParams.get("v"); if (v != null) { if (chan && (v === `${chan}/${vid}` || v === `${chan}/v${vid}`)) return true; if (v === vid) return true; } if (/(^|\.)youtu\.be$/.test(u.hostname) && u.pathname === `/${vid}`) return true; return false; } /** * The second the clip's citeUrl opens, when it names this recording and * carries a `t=`; null otherwise (no citeUrl, another recording, no `t`). */ export function citeUrlSecond(e, channelSlug = null) { if (!e?.citeUrl) return null; let u; try { u = new URL(String(e.citeUrl)); } catch { return null; } if (!u.searchParams.has("t")) return null; if (!sameRecording(u, e, channelSlug)) return null; return parseT(u.searchParams.get("t")); } /** * The citeUrl with its `t=` set to `sec` (whole seconds), keeping a `123s` * spelling as `s`. A targeted edit rather than URLSearchParams: a * re-serialised URL rewrites every other parameter's escaping and churns the * manifest (resolve-windows.mjs retargetCiteUrl, the same reason). */ export function withT(citeUrl, sec) { const n = Math.max(0, Math.floor(sec)); return String(citeUrl).replace(/([?&]t=)(\d+(?:\.\d+)?)(s?)(?=&|#|$)|([?&]t=)[^&#]*/, (all, p1, _num, suffix, p4) => p1 ? `${p1}${n}${suffix}` : `${p4}${n}`, ); } /** * After an edge move: the `cite` and the citeUrl's `t=` the clip should carry, * when either now lies outside what plays. Mutates nothing; returns null when * both are fine. * * The new second is the clip's first played second, floored -- what the * header shows for a clip with no `cite` at all, and the second a derived QR * opens. A citeUrl moves to the (new) cite. * * @param {Record} e the entry AFTER the window patch * @param {{ channelSlug?: string | null }} [opts] * @returns {null | { cite?: { from: number, to: number }, citeUrl?: { from: string, to: string } }} */ export function citeFollow(e, { channelSlug = null } = {}) { const span = playedSpan(e); if (!Number.isFinite(span.from) || !Number.isFinite(span.to)) return null; const out = {}; let clock = clockOf(e); if (e.cite != null && offsetFrom(Number(e.cite), span) !== 0) { const to = Math.floor(span.from); out.cite = { from: Number(e.cite), to }; clock = to; } const t = citeUrlSecond(e, channelSlug); if (t != null && offsetFrom(t, span) !== 0) { const to = withT(e.citeUrl, clock); if (to !== e.citeUrl) out.citeUrl = { from: String(e.citeUrl), to }; } return out.cite || out.citeUrl ? out : null; } /** * After an explicit `cite` write: the citeUrl's `t=` follows when the two * agreed before (the QR opened at the clock's whole second). One that said * something else was somebody's decision and is left. * * @param {Record} e the entry AFTER the patch * @param {number} oldClock the clock's second BEFORE the patch * @param {{ channelSlug?: string | null }} [opts] * @returns {null | { citeUrl: { from: string, to: string } }} */ export function qrFollowsCite(e, oldClock, { channelSlug = null } = {}) { const t = citeUrlSecond(e, channelSlug); if (t == null || Math.floor(t) !== Math.floor(oldClock)) return null; const to = withT(e.citeUrl, clockOf(e)); return to !== e.citeUrl ? { citeUrl: { from: String(e.citeUrl), to } } : null; } /** * What the check says about one clip: where its clock and its QR (when the * citeUrl is this recording's) sit against what plays, in signed seconds (see * offsetFrom), and the patch that puts them back (`cite`, and `citeUrl` when * it would not follow the cite by itself). Null when both are fine. * * A derived QR (no citeUrl) is not judged here: it opens at the extent's * start, which is the material somebody reviewed. * * @param {Record} e * @param {{ channelSlug?: string | null }} [opts] */ export function citeOffsets(e, { channelSlug = null } = {}) { const span = playedSpan(e); if (!Number.isFinite(span.from) || !Number.isFinite(span.to)) return null; const clock = clockOf(e); const clockOff = e.cite != null ? offsetFrom(Number(clock), span) : 0; const t = citeUrlSecond(e, channelSlug); const qrOff = t != null ? offsetFrom(t, span) : 0; if (!clockOff && !qrOff) return null; const at = Math.floor(span.from); /** @type {Record} */ const patch = {}; if (clockOff) patch.cite = at; // The writer moves the QR with an explicit cite only when they agreed. const follows = clockOff && t != null && Math.floor(t) === Math.floor(clock); if (qrOff && !follows) patch.citeUrl = withT(e.citeUrl, clockOff ? at : clock); return { clockOff, qrOff, clock, qr: t, from: span.from, to: span.to, at, patch }; }