Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .coderabbit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
reviews:
auto_review:
enabled: true
# Review stacked PRs too, not only PRs that target the default branch.
base_branches:
- '.*'
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: Lint & Tests
on:
push:
branches: [main]
# Every PR, including ones stacked on another PR's branch.
pull_request:
branches: [main]
jobs:
lint:
timeout-minutes: 10
Expand Down Expand Up @@ -57,7 +57,8 @@ jobs:

e2e:
timeout-minutes: 60
runs-on: ubuntu-latest
# Playwright 1.60 can't install browsers on ubuntu-latest (26.04) yet.
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand Down
1 change: 1 addition & 0 deletions packages/starpod/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ const ROUTES: Array<{ pattern: string; entrypoint: string }> = [
{ pattern: '/robots.txt', entrypoint: 'robots.txt.ts' },
{ pattern: '/api/contact', entrypoint: 'api/contact.ts' },
{ pattern: '/api/episodes/[page].json', entrypoint: 'api/episodes/[page].json.ts' },
{ pattern: '/api/cues/[episode].json', entrypoint: 'api/cues/[episode].json.ts' },
{
pattern: '/api/episodes/search.json',
entrypoint: 'api/episodes/search.json.ts'
Expand Down
88 changes: 88 additions & 0 deletions packages/starpod/src/lib/cast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import type { TranscriptCue } from './cues';

/** A person on an episode, as recorded in the guests/sponsors database. */
export interface CastPerson {
id: string;
img: string | null;
isHost: boolean;
name: string;
}

/**
* The episode's people in credit order: this episode's hosts first (the
* show's configured hosts in config order, then guest hosts alphabetically),
* then guests alphabetically. Whether someone hosted is the episode's own
* record (`isHost`), so a regular host who joined one episode as a guest is
* credited as a guest there.
*/
export function sortCast(
people: Array<CastPerson>,
configuredHosts: Array<string>
): Array<CastPerson> {
return [...people].sort((a, b) => {
if (a.isHost !== b.isHost) {
return a.isHost ? -1 : 1;
}
if (!a.isHost) {
return a.name.localeCompare(b.name);
}
const aIndex = configuredHosts.indexOf(a.name);
const bIndex = configuredHosts.indexOf(b.name);
if (aIndex !== -1 && bIndex !== -1) {
return aIndex - bIndex;
}
if (aIndex !== -1) {
return -1;
}
if (bIndex !== -1) {
return 1;
}
return a.name.localeCompare(b.name);
});
}

const tokens = (name: string) =>
name
.toLowerCase()
.split(/[\s.]+/)
.map((token) => token.replace(/[^a-z0-9]/g, ''))
.filter(Boolean);

/**
* Do these two names refer to the same person? Loosely: their first names
* start the same way ("Robbie" ~ "RobbieTheWagner"), or they share a surname
* ("Chuck Carpenter" ~ "Charles William Carpenter III").
*/
function sameName(a: string, b: string) {
const [aFirst = '', ...aRest] = tokens(a);
const [bFirst = '', ...bRest] = tokens(b);
if (aFirst.length >= 3 && bFirst.length >= 3 && (aFirst.startsWith(bFirst) || bFirst.startsWith(aFirst))) {
return true;
}
return aRest.some((token) => token.length >= 4 && bRest.includes(token));
}

/**
* Which transcript voice (0 or 1, the two most talkative speakers) a person
* speaks in, so their name on the stage wears the same typeface as their
* lines. People who don't speak as one of those two, or can't be matched, get
* no voice.
*/
export function voiceFor(
name: string,
cues: Array<TranscriptCue> | null
): number | undefined {
if (!cues?.length) {
return undefined;
}
const speakers = [0, 1].map((voice) => ({
voice,
speaker: cues.find((cue) => cue.v === voice && cue.n)?.n
}));
// An exact name wins over a loose one, whichever voice comes first.
const exact = tokens(name).join(' ');
const match =
speakers.find(({ speaker }) => speaker && tokens(speaker).join(' ') === exact) ??
speakers.find(({ speaker }) => speaker && sameName(name, speaker));
return match?.voice;
}
226 changes: 226 additions & 0 deletions packages/starpod/src/lib/cues.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
import type { Episode } from './rss';
import { parseCues } from './transcript';

/**
* A short, timed line of speech for the live transcript performance. `v` is
* the voice: the speaker's order of first appearance (0, 1, 2…), absent when
* the source carries no speaker names (plain captions). `n` is the name.
*/
export interface TranscriptCue {
s: number;
t: string;
v?: number;
n?: string;
}

interface Segment {
start: number;
text: string;
speaker?: string;
}

// Lines longer than this are split at sentence ends so the live line stays a
// phrase, not a paragraph.
const CUE_TARGET_CHARS = 140;

export function mergeIntoLines(segments: Array<Segment>): Array<TranscriptCue> {
const voices = new Map<string, number>();
const lines: Array<TranscriptCue> = [];
let current: TranscriptCue | null = null;
let speaker: string | undefined;

for (const segment of segments) {
const endsSentence = current ? /[.!?]["”’)]?$/.test(current.t) : false;
// Never break mid-sentence unless a line runs far past the target; a
// transcript paragraph that was cut mid-thought joins the next one.
const startNew =
!current ||
segment.speaker !== speaker ||
(current.t.length >= CUE_TARGET_CHARS / 2 && endsSentence) ||
current.t.length >= CUE_TARGET_CHARS * 1.6;

if (startNew) {
speaker = segment.speaker;
current = { s: Math.round(segment.start * 10) / 10, t: segment.text };
if (speaker) {
if (!voices.has(speaker)) {
voices.set(speaker, voices.size);
}
current.v = voices.get(speaker);
current.n = speaker;
}
lines.push(current);
} else if (current) {
current.t = `${current.t} ${segment.text}`;
}
}

return rankVoices(lines);
}

/**
* The two people who talk the most get the two signature voices (0 and 1, in
* order of first appearance); everyone else (intros, guests, ads) shares the
* supporting voices from 2 up.
*/
function rankVoices(lines: Array<TranscriptCue>): Array<TranscriptCue> {
const counts = new Map<string, number>();
const firstSeen: Array<string> = [];
for (const line of lines) {
if (!line.n) {
continue;
}
if (!counts.has(line.n)) {
firstSeen.push(line.n);
}
counts.set(line.n, (counts.get(line.n) ?? 0) + 1);
}
const leads = [...counts.entries()]
.sort((a, b) => b[1] - a[1])
.slice(0, 2)
.map(([name]) => name)
.sort((a, b) => firstSeen.indexOf(a) - firstSeen.indexOf(b));
const supporting = firstSeen.filter((name) => !leads.includes(name));
for (const line of lines) {
if (line.n) {
const lead = leads.indexOf(line.n);
line.v = lead !== -1 ? lead : 2 + (supporting.indexOf(line.n) % 4);
}
}
return lines;
}

const MARKDOWN_LINE =
/^\s*\[(?:(\d+):)?(\d{1,2}):(\d{2})\]\s*(?:(?:\*\*|__)?([^:\n[\]*_]{1,40}):(?:\*\*|__)?\s*)?(.*)$/;

function stripInlineMarkdown(text: string): string {
return text
.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
.replace(/(\*\*|__|\*|_|`)/g, '');
}

/**
* Parse a markdown transcript (`[HH:MM:SS] Speaker: text` paragraphs) into
* timed lines with voices. Long speeches split into sentences that share the
* paragraph's start time, so they still perform one phrase at a time.
*/
export function parseMarkdownCues(markdown: string): Array<TranscriptCue> {
const paragraphs: Array<{ start: number; speaker?: string; sentences: Array<string> }> = [];
for (const block of markdown.replace(/\r\n?/g, '\n').split(/\n{2,}/)) {
const match = block.replace(/\n/g, ' ').match(MARKDOWN_LINE);
if (!match) {
continue;
}
const [, h, m, sec, name, text] = match;
const sentences = (
stripInlineMarkdown(text).trim().match(/[^.!?]+[.!?]+["”’)]?|[^.!?]+$/g) ?? []
)
.map((sentence) => sentence.trim())
.filter(Boolean);
paragraphs.push({
start: Number(h ?? 0) * 3600 + Number(m) * 60 + Number(sec),
speaker: name?.trim(),
sentences
});
}

// Each sentence starts partway through its paragraph, in proportion to the
// text before it, so the live line moves at roughly the pace of speech.
const segments: Array<Segment> = [];
paragraphs.forEach((paragraph, index) => {
const total = paragraph.sentences.reduce((sum, item) => sum + item.length, 0) || 1;
const next = paragraphs[index + 1]?.start;
const span = next && next > paragraph.start ? next - paragraph.start : total / 15;
let offset = 0;
for (const sentence of paragraph.sentences) {
segments.push({
start: paragraph.start + (span * offset) / total,
text: sentence,
speaker: paragraph.speaker
});
offset += sentence.length;
}
});
return mergeIntoLines(segments);
}

type JsonSegment = {
body?: string;
speaker?: string;
start?: number;
startTime?: number;
text?: string;
};

/**
* Segments from a JSON transcript. The Podcast Namespace format names its
* fields `startTime` and `body`; `start` and `text` are accepted as well.
*/
export function jsonSegments(data: { segments?: Array<JsonSegment> }): Array<Segment> {
return (data.segments ?? []).flatMap((segment) => {
const start = segment.startTime ?? segment.start;
const text = (segment.body ?? segment.text)?.trim();
return typeof start === 'number' && text
? [{ start, text, speaker: segment.speaker || undefined }]
: [];
});
}

/**
* Timed lines from the feed's `<podcast:transcript>` (VTT/SRT cues or JSON
* segments). Returns `null` when none is available.
*/
const rssCueCache = new Map<string, Promise<Array<TranscriptCue> | null>>();

export function getRssCues(
episode: Episode
): Promise<Array<TranscriptCue> | null> {
const url = episode.transcriptUrl;
if (!url) {
return Promise.resolve(null);
}
let pending = rssCueCache.get(url);
if (!pending) {
pending = fetchRssCues(episode, url);
rssCueCache.set(url, pending);
}
return pending;
}

async function fetchRssCues(
episode: Episode,
url: string
): Promise<Array<TranscriptCue> | null> {
try {
// A deadline on the whole request, body included, so a stalled
// transcript can't hold up the build.
const response = await fetch(url, { signal: AbortSignal.timeout(10_000) });
if (!response.ok) {
return null;
}
const raw = await response.text();
const type = (
episode.transcriptType ||
response.headers.get('content-type') ||
''
).toLowerCase();

if (type.includes('json')) {
const segments = jsonSegments(JSON.parse(raw));
return segments.length ? mergeIntoLines(segments) : null;
}

const cues = parseCues(raw).map((cue) => {
// WebVTT voice spans: <v Speaker>text</v>
const voice = cue.text.match(/^<v(?:\.[^\s>]*)?\s+([^>]+)>/);
return {
start: cue.start,
text: cue.text.replace(/<[^>]+>/g, '').trim(),
speaker: voice?.[1]?.trim()
};
});
return cues.length ? mergeIntoLines(cues) : null;
} catch {
return null;
}
}
28 changes: 28 additions & 0 deletions packages/starpod/src/lib/episode-cues.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getEntry } from 'astro:content';

import { getRssCues, parseMarkdownCues, type TranscriptCue } from './cues';
import type { Episode } from './rss';

/**
* Timed lines for an episode: a markdown transcript in the site's content
* collection wins (it names speakers), then the feed's transcript. Server-only.
*/
export async function getEpisodeCues(
episode: Episode
): Promise<Array<TranscriptCue> | null> {
if (episode.episodeNumber && episode.episodeNumber !== 'Bonus') {
let entry;
try {
entry = await getEntry('transcripts', episode.episodeNumber);
} catch {
entry = undefined;
}
if (entry?.body) {
const cues = parseMarkdownCues(entry.body);
if (cues.length) {
return cues;
}
}
}
return getRssCues(episode);
}
Loading
Loading