diff --git a/AGENTS.md b/AGENTS.md index facd7f18..9f06847b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -67,7 +67,7 @@ svelte-check's native API path passes when a tsconfig matches no files, which `check:a11y` in temporary workspaces and requires it to fail on that case and when the native compiler cannot start. -After `npm run build`, `npm run size` enforces 333,899 raw bytes and 109,091 gzip bytes at level 9 for root `main.js` in the Test workflow's Build and sizes worker. `npm run size:data` reports a synthetic v2 stress library with 50 feeds, 2,000 history entries, and 200 saved episodes from frozen NPR metadata, including duplicated snapshots. Its 1.5 MB document goal is advisory. Pass an additive candidate JSON path to report optional-field overhead without changing existing data. In the isolated Obsidian instance, `npm run count:writes -- start`, `report`, and `stop` record writes to PodNotes' exact `data.json` path. Stop the recorder before stopping the instance, because recorder commands start a stopped instance; plugin reloads invalidate its counts. +After `npm run build`, `npm run size` enforces 336,681 raw bytes and 110,195 gzip bytes at level 9 for root `main.js` in the Test workflow's Build and sizes worker. `npm run size:data` reports a synthetic v2 stress library with 50 feeds, 2,000 history entries, and 200 saved episodes from frozen NPR metadata, including duplicated snapshots. Its 1.5 MB document goal is advisory. Pass an additive candidate JSON path to report optional-field overhead without changing existing data. In the isolated Obsidian instance, `npm run count:writes -- start`, `report`, and `stop` record writes to PodNotes' exact `data.json` path. Stop the recorder before stopping the instance, because recorder commands start a stopped instance; plugin reloads invalidate its counts. Before opening a PR or cutting a release, run the CI-equivalent checks locally: diff --git a/scripts/size-baseline.md b/scripts/size-baseline.md index dee37744..5277c266 100644 --- a/scripts/size-baseline.md +++ b/scripts/size-baseline.md @@ -12,7 +12,7 @@ main.js raw: 307131 bytes; budget 330000 bytes; 22869 bytes remaining main.js gzip (level 9): 97161 bytes; budget 100000 bytes; 2839 bytes remaining ``` -The gzip budget is now 109,091 bytes, and the raw budget 333,899. On 2026-10-14 it rose from 100,000 for +The gzip budget is now 110,195 bytes, and the raw budget 336,681. On 2026-10-14 it rose from 100,000 for the full player's Transcript panel, which the owner approved at about 1.9 KB and which measured 2,576 bytes. The coordinator set 103,000 to keep the panel's Find and Follow chip and to leave room for fixes in flight. The @@ -136,6 +136,17 @@ main.js raw: 333899 bytes; budget 333899 bytes; 0 bytes remaining main.js gzip (level 9): 109091 bytes; budget 109091 bytes; 0 bytes remaining ``` +On 2026-11-30 both rose for issue #561: the Transcript panel shows the +episode's saved transcript note when its feed has none or the feed's does +not load. It parses the note's timestamp links, clocks, speaker labels and +escapes, and reads the note again when it changes: from 333,899 raw by 2,782 +bytes, and from 109,091 gzip by 1,104 bytes. With it: + +```text +main.js raw: 336681 bytes; budget 336681 bytes; 0 bytes remaining +main.js gzip (level 9): 110195 bytes; budget 110195 bytes; 0 bytes remaining +``` + Gzip uses Node's `gzipSync` at level 9, without a filename or timestamp. Budgets use decimal bytes. The brief's 96,533-byte reference is unexplained. This report fixes the compressor instead of comparing compressed sizes across diff --git a/scripts/size.mjs b/scripts/size.mjs index 00c1f23d..140118e0 100644 --- a/scripts/size.mjs +++ b/scripts/size.mjs @@ -9,8 +9,8 @@ try { const bundle = readFileSync(path); if (!bundle.length) throw new Error(`${path} is empty. Run npm run build first.`); const measurements = [ - { name: "raw", bytes: bundle.length, budget: 333_899 }, - { name: "gzip (level 9)", bytes: gzipSync(bundle, { level: 9 }).length, budget: 109_091 }, + { name: "raw", bytes: bundle.length, budget: 336_681 }, + { name: "gzip (level 9)", bytes: gzipSync(bundle, { level: 9 }).length, budget: 110_195 }, ]; const rows = measurements.map(({ name, bytes, budget }) => { const overage = Math.max(0, bytes - budget); diff --git a/scripts/size.test.ts b/scripts/size.test.ts index 542897ee..cf83b028 100644 --- a/scripts/size.test.ts +++ b/scripts/size.test.ts @@ -20,19 +20,19 @@ function check(name: string, contents: string | Buffer) { describe("bundle budget CLI", () => { it("allows the raw budget boundary and rejects one extra byte", () => { - expect(check("boundary.js", "a".repeat(333_899)).status).toBe(0); - const result = check("over.js", "a".repeat(333_900)); + expect(check("boundary.js", "a".repeat(336_681)).status).toBe(0); + const result = check("over.js", "a".repeat(336_682)); expect(result.status).toBe(1); - expect(result.stdout).toContain("budget 333899 bytes; OVER by 1 bytes"); + expect(result.stdout).toContain("budget 336681 bytes; OVER by 1 bytes"); }); it("rejects gzip growth even when raw bytes fit", () => { - const result = check("gzip.js", randomBytes(110_000)); + const result = check("gzip.js", randomBytes(111_000)); expect(result.status).toBe(1); expect(result.stdout).toMatch( - /gzip \(level 9\): \d+ bytes; budget 109091 bytes; OVER by \d+ bytes/, + /gzip \(level 9\): \d+ bytes; budget 110195 bytes; OVER by \d+ bytes/, ); - expect(result.stdout).toContain("budget 333899 bytes; 223899 bytes remaining"); + expect(result.stdout).toContain("budget 336681 bytes; 225681 bytes remaining"); }); it("fails on absent or empty artifacts instead of reporting a small build", () => { diff --git a/src/services/publishedTranscript/note.test.ts b/src/services/publishedTranscript/note.test.ts new file mode 100644 index 00000000..c8402b83 --- /dev/null +++ b/src/services/publishedTranscript/note.test.ts @@ -0,0 +1,155 @@ +import { describe, expect, test } from "vitest"; +import { DEFAULT_SETTINGS } from "src/constants"; +import { TranscriptTemplateEngine } from "src/TemplateEngine"; +import type { Episode } from "src/types/Episode"; +import { parseTranscriptNote } from "./note"; +import { type NonEmpty, type TranscriptCue } from "./parse"; +import { groupCues, renderTranscriptBody, timestampStyle } from "./render"; + +const episode: Episode = { + title: "Pilot", + streamUrl: "https://pod.example.com/1.mp3", + url: "", + description: "", + content: "", + podcastName: "Feed Podcast", + feedUrl: "https://pod.example.com/feed.xml", +}; +const BOLD = DEFAULT_SETTINGS.transcript.diarization.speakerTemplate; + +const cues: NonEmpty = [ + { start: 0.48, end: 4.41, speaker: "Justin", text: "Hello there." }, + { start: 4.41, end: 9, text: "Still Justin." }, + { start: 70.16, end: 75, speaker: "Michael", text: "Hi!" }, + { start: 3725.48, end: 3730, speaker: "Justin", text: "An hour in." }, +]; + +/** A note as PodNotes saves it: the default template, with frontmatter a user added above. */ +function savedNote( + transcript: NonEmpty, + { + timestamp = DEFAULT_SETTINGS.timestamp.template, + speakerTemplate = BOLD, + played = episode, + } = {}, +): string { + const body = renderTranscriptBody( + { kind: "timed", cues: transcript }, + { episode: played, speakerTemplate, timestamp: timestampStyle(timestamp) }, + ); + const note = TranscriptTemplateEngine(DEFAULT_SETTINGS.transcript.template, played, body); + return `---\ntags: [podcast]\nreviewed: 12:34\n---\n${note}`; +} + +/** The lines the note was written from: the writer's paragraphs, each at its first cue's start. */ +function written(transcript: NonEmpty, start = (seconds: number) => seconds) { + return groupCues(transcript).map((line) => ({ + start: start(line.start), + speaker: line.speaker, + text: line.text, + })); +} + +const timedCues = (markdown: string, speakerTemplate = BOLD) => { + const parsed = parseTranscriptNote(markdown, speakerTemplate); + if (parsed?.kind !== "timed") throw new Error(`Expected timed, got ${parsed?.kind}`); + return parsed.cues; +}; + +describe("parseTranscriptNote reads back the note PodNotes saved", () => { + test("linked times, at the link's exact seconds, with speakers and without the template's header", () => { + expect(timedCues(savedNote(cues))).toEqual(written(cues)); + }); + + test("an mm:ss timestamp past an hour still plays from the link's exact time", () => { + const note = savedNote(cues, { timestamp: "{{linktime:mm:ss}}" }); + expect(note).toContain("[02:05](obsidian://podnotes?"); + + expect(timedCues(note).map(({ start }) => start)).toEqual([0.48, 70.16, 3725.48]); + }); + + test("plain times, to the second, when the timestamp template asks for {{time}}", () => { + expect(timedCues(savedNote(cues, { timestamp: "{{time}} " }))).toEqual( + written(cues, Math.floor), + ); + }); + + test("plain times for a private feed, whose links PodNotes leaves out", () => { + const played = { ...episode, feedUrl: "https://pod.example.com/feed.xml?auth=SECRET" }; + const note = savedNote(cues, { played }); + expect(note).not.toContain("obsidian://"); + + expect(timedCues(note)).toEqual(written(cues, Math.floor)); + }); + + test("text the writer escaped reads as it was spoken", () => { + const hostile: NonEmpty = [ + { + start: 1, + speaker: "Dr. [Ana] #1", + text: "Run `ls` now [[Home]] #tag 50% $5 ~x^y | a::b < C:\\Users \\ done", + }, + ]; + + expect(timedCues(savedNote(hostile))).toEqual(written(hostile)); + }); + + test("a speaker template without {{speaker}}, and a label a changed template no longer matches", () => { + const note = savedNote(cues, { speakerTemplate: " - " }); + expect(note).toContain("Justin: - Hello there."); + + expect(timedCues(note, " - ")).toEqual(written(cues)); + expect(timedCues(note)[0]).toEqual({ + start: 0.48, + speaker: undefined, + text: "Justin: - Hello there. Still Justin.", + }); + }); +}); + +describe("parseTranscriptNote reads a note written by hand", () => { + test("bracketed, bare and listed clocks, with mm:ss for two parts", () => { + const note = [ + "# Notes", + "Recorded at home.", + "[00:12:34] Bracketed.", + "- 12:34 Listed, minutes and seconds.", + "* 1:02:03 Hours.", + "0:05.5 A fraction", + "carries on below its time.", + "## Part two", + "[12:35](https://example.com) A clock another link shows.", + ].join("\n"); + + expect(timedCues(note)).toEqual([ + { start: 754, speaker: undefined, text: "Bracketed." }, + { start: 754, speaker: undefined, text: "Listed, minutes and seconds." }, + { start: 3723, speaker: undefined, text: "Hours." }, + { start: 5.5, speaker: undefined, text: "A fraction carries on below its time." }, + { start: 755, speaker: undefined, text: "A clock another link shows." }, + ]); + }); + + test("a clock must stand alone at the start of a line", () => { + expect(parseTranscriptNote("12:34pm lunch\n\nA ratio of 3:45:00x.", BOLD)).toEqual({ + kind: "untimed", + paragraphs: ["12:34pm lunch", "A ratio of 3:45:00x."], + }); + }); + + test("a note without times is its paragraphs, without frontmatter or headings", () => { + const note = `---\ntitle: Pilot\n---\n# Pilot\n\nFirst \\#1 paragraph\nwraps.\n\n\\- Second.\n\n`; + + expect(parseTranscriptNote(note, BOLD)).toEqual({ + kind: "untimed", + paragraphs: ["First #1 paragraph wraps.", "- Second."], + }); + }); + + test("a note with no text has no transcript", () => { + expect(parseTranscriptNote("---\ntitle: Pilot\n---\n# Pilot\n\n", BOLD)).toBeUndefined(); + expect( + parseTranscriptNote("[00:00:01](obsidian://podnotes?time=1) ", BOLD), + ).toBeUndefined(); + }); +}); diff --git a/src/services/publishedTranscript/note.ts b/src/services/publishedTranscript/note.ts new file mode 100644 index 00000000..cba6294e --- /dev/null +++ b/src/services/publishedTranscript/note.ts @@ -0,0 +1,109 @@ +import { DEFAULT_SETTINGS } from "src/constants"; +import { + collapseWhitespace, + type ParsedTranscript, + parseClock, + timed, + type TranscriptCue, + untimed, +} from "./parse"; +import { formatSpeakerLabel } from "./render"; + +const FRONTMATTER = /^---\n[\s\S]*?\n---(?:\n|$)/; +const HEADING = /^#{1,6}(?:\s|$)/; +const LIST_MARKER = /^\s*[-*+]\s+/; +const LINK = /^\[([^\]]*)\]\(([^)\s]*)\)/; +const BARE_CLOCK = /^\[?(\d+(?::\d+){1,2}(?:[.,]\d+)?)\]?(?=\s|$)/; +/** CommonMark lets a backslash escape only ASCII punctuation, so `C:\Users` keeps its backslash. */ +const ESCAPE = /\\([!-/:-@[-`{-~])/g; + +/** + * The transcript in a saved transcript note, or undefined when the note holds no text. A line + * that starts with a time starts a timed line, and the lines before the first one, such as the + * template's title and date, are left out. A time is the seconds of the obsidian://podnotes + * link a timestamp links to, because the format the link shows can leave out the hours, or else + * a clock such as `12:34`, `1:02:03` or `[00:12:34]`. A note without one is untimed paragraphs. + */ +export function parseTranscriptNote( + markdown: string, + speakerTemplate: string, +): ParsedTranscript | undefined { + const body = markdown + .replace(/^\uFEFF/, "") + .replace(/\r\n?/g, "\n") + .replace(FRONTMATTER, ""); + const lines = body.split("\n").filter((line) => !HEADING.test(line)); + const speakerOf = speakerReader(speakerTemplate); + const cues: TranscriptCue[] = []; + for (const line of lines) { + const time = timeOf(line); + const last = cues[cues.length - 1]; + if (time) { + const { speaker, text } = speakerOf(time.rest.trimStart()); + cues.push({ start: time.start, speaker, text: unescape(text) }); + } else if (last) { + cues[cues.length - 1] = { ...last, text: `${last.text} ${unescape(line)}` }; + } + } + if (cues.length > 0) { + return timed( + cues + .map((cue) => ({ ...cue, text: collapseWhitespace(cue.text) })) + .filter((cue) => cue.text), + ); + } + return untimed( + lines + .join("\n") + .split(/\n[ \t]*\n/) + .map((paragraph) => collapseWhitespace(unescape(paragraph))) + .filter(Boolean), + ); +} + +function timeOf(line: string): { start: number; rest: string } | undefined { + const stamp = line.replace(LIST_MARKER, ""); + const link = LINK.exec(stamp); + if (link) { + const start = linkedTime(link[2]) ?? parseClock(link[1]); + return start === undefined ? undefined : { start, rest: stamp.slice(link[0].length) }; + } + const clock = BARE_CLOCK.exec(stamp); + const start = clock ? parseClock(clock[1]) : undefined; + return clock && start !== undefined ? { start, rest: stamp.slice(clock[0].length) } : undefined; +} + +function linkedTime(url: string): number | undefined { + if (!url.startsWith("obsidian://podnotes?")) return undefined; + const time = new URLSearchParams(url.slice(url.indexOf("?") + 1)).get("time"); + const seconds = time?.trim() ? Number(time) : NaN; + return Number.isFinite(seconds) && seconds >= 0 ? seconds : undefined; +} + +/** + * Reads the label PodNotes writes with the speaker template, which formatSpeakerLabel builds, + * so a note saved with another template keeps its label as text. + */ +function speakerReader(template: string): (text: string) => { speaker?: string; text: string } { + const marker = "\u0000"; + const [before = "", after = ""] = formatSpeakerLabel( + template || DEFAULT_SETTINGS.transcript.diarization.speakerTemplate, + marker, + ).split(marker); + const end = after.trimEnd(); + if (!end) return (text) => ({ text }); + const label = new RegExp(`^${escapeRegExp(before.trimStart())}(.+?)${escapeRegExp(end)}\\s*`); + return (text) => { + const found = label.exec(text); + const speaker = found && collapseWhitespace(unescape(found[1])); + return speaker ? { speaker, text: text.slice(found[0].length) } : { text }; + }; +} + +function unescape(text: string): string { + return text.replace(ESCAPE, "$1"); +} + +function escapeRegExp(text: string): string { + return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); +} diff --git a/src/services/publishedTranscript/parse.ts b/src/services/publishedTranscript/parse.ts index c6c18afb..e095e586 100644 --- a/src/services/publishedTranscript/parse.ts +++ b/src/services/publishedTranscript/parse.ts @@ -149,7 +149,7 @@ function paragraphsOf(source: string): string[] { const CLOCK = /^(?:(\d+):)?(\d{1,2}):(\d{2})(?:[.,](\d+))?$/; -function parseClock(text: string): number | undefined { +export function parseClock(text: string): number | undefined { const clock = CLOCK.exec(text.trim()); if (!clock) return undefined; const [, hours = "0", minutes, seconds, fraction = "0"] = clock; diff --git a/src/ui/PodcastView/TranscriptPanel.svelte b/src/ui/PodcastView/TranscriptPanel.svelte index ece17a75..68e1c753 100644 --- a/src/ui/PodcastView/TranscriptPanel.svelte +++ b/src/ui/PodcastView/TranscriptPanel.svelte @@ -1,5 +1,6 @@