From 8d8b4aa4ee2d3d9a184839623f58cdea8b559cda Mon Sep 17 00:00:00 2001 From: Christian Bager Bach Houmann Date: Mon, 30 Nov 2026 17:47:09 +0100 Subject: [PATCH 1/4] feat(transcripts): show the saved transcript note in the player's Transcript panel When an episode's feed publishes no transcript, or its transcript does not load, the Transcript panel now reads the episode's transcript note from the vault and shows its lines, with times to play from, Follow, Find and speaker names. The panel's head reads "Saved note" and keeps Open note. A feed transcript still comes first. The note's times come from the obsidian://podnotes links PodNotes writes, so a format such as mm:ss that leaves out the hours still plays from the right second. A note without links falls back to a leading clock such as [00:12:34], 12:34 or 1:02:03, and a note without any time shows as paragraphs. Frontmatter, headings and the template's header are left out, and the escapes PodNotes writes are undone. The panel reads the note again when it is saved, moved or edited, so a transcription that just finished shows at once. Only the feed's transcript is cached. Fixes #561 --- src/services/publishedTranscript/note.test.ts | 155 +++++++++++++++++ src/services/publishedTranscript/note.ts | 109 ++++++++++++ src/services/publishedTranscript/parse.ts | 2 +- src/ui/PodcastView/TranscriptPanel.svelte | 71 ++++++-- src/ui/PodcastView/TranscriptPanel.test.ts | 157 ++++++++++++++++-- src/ui/PodcastView/transcript.test.ts | 95 ++++++++++- src/ui/PodcastView/transcript.ts | 58 ++++++- tests/e2e/journey-transcript.test.ts | 78 +++++++++ 8 files changed, 677 insertions(+), 48 deletions(-) create mode 100644 src/services/publishedTranscript/note.test.ts create mode 100644 src/services/publishedTranscript/note.ts 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..e9baab13 100644 --- a/src/ui/PodcastView/TranscriptPanel.svelte +++ b/src/ui/PodcastView/TranscriptPanel.svelte @@ -1,5 +1,5 @@