diff --git a/packages/starpod/src/components/episode/Onward.astro b/packages/starpod/src/components/episode/Onward.astro index 9c2653a..c6eec3b 100644 --- a/packages/starpod/src/components/episode/Onward.astro +++ b/packages/starpod/src/components/episode/Onward.astro @@ -147,7 +147,12 @@ const pager = [ let leaving = ''; document.addEventListener('astro:before-preparation', (event) => { - leaving = event.from.pathname; + // The canonical path, so an episode reached by its number (/123) still + // finds its card, which links to its slug. + leaving = new URL( + document.querySelector('link[rel="canonical"]')?.href ?? + event.from.href + ).pathname; name( (event.sourceElement as HTMLElement | undefined)?.closest?.( '[data-ep-card]' diff --git a/packages/starpod/src/layouts/Layout.astro b/packages/starpod/src/layouts/Layout.astro index 501b3b2..899d27a 100644 --- a/packages/starpod/src/layouts/Layout.astro +++ b/packages/starpod/src/layouts/Layout.astro @@ -23,6 +23,7 @@ import { RailIcon } from '../components/icons'; +import { hostVoices } from '../lib/cast'; import { getShowInfo } from '../lib/rss'; import starpodConfig from 'virtual:starpod/config'; @@ -37,13 +38,15 @@ const show = await getShowInfo(); // Only built pages gather them: an on-demand page such as the 404 would fetch // the feed and query the database on every request, so its palette starts // with episodes instead. -const hostNames = starpodConfig.hosts.map((host) => host.name); -const people = Astro.isPrerendered - ? (await loadPeople()).map((person) => { - const voice = hostNames.indexOf(person.label); - return voice === 0 || voice === 1 ? { ...person, voice } : person; - }) - : []; +const everyone = Astro.isPrerendered ? await loadPeople() : []; +const voices = hostVoices( + starpodConfig.hosts.map((host) => host.name), + everyone.map((person) => person.label) +); +const people = everyone.map((person) => { + const voice = voices.get(person.label); + return voice === undefined ? person : { ...person, voice }; +}); export interface Props { /** diff --git a/packages/starpod/src/lib/cast.ts b/packages/starpod/src/lib/cast.ts index 50ece45..0645cce 100644 --- a/packages/starpod/src/lib/cast.ts +++ b/packages/starpod/src/lib/cast.ts @@ -176,6 +176,36 @@ export function matchNames( ); } +/** + * Each of `names` with its surest candidate, as `matchNames` scores them, but + * a candidate can go to any number of names: every spelling of one person + * finds their record. A name whose surest candidates tie stays unmatched. + */ +export function matchEach(names: Array, candidates: Array) { + return new Map( + [...new Set(names)].flatMap((name) => [ + ...pair([name], candidates, (one, candidate) => + likeness(one, candidate, false) + ) + ]) + ); +} + +/** + * The show's first two configured hosts among `names`, matched as the About + * page matches them (`matchNames`), each with the transcript voice they wear: + * name → 0 or 1. + */ +export function hostVoices(hosts: Array, names: Array) { + const recordOf = matchNames(hosts, names); + return new Map( + hosts.slice(0, 2).flatMap((host, voice) => { + const name = recordOf.get(host); + return name ? [[name, voice] as const] : []; + }) + ); +} + /** * Who on a page is speaking: `names` are the people a page shows (however * each element names them), `speakers` the playing transcript's. Names that diff --git a/packages/starpod/src/lib/people.ts b/packages/starpod/src/lib/people.ts index 9a1b7d7..756e62e 100644 --- a/packages/starpod/src/lib/people.ts +++ b/packages/starpod/src/lib/people.ts @@ -17,7 +17,7 @@ import avatar from '../assets/avatar.svg?url'; import { createDb } from '../db'; import { HostOrGuest, Person } from '../db/schema'; import { dasherize } from '../utils/dasherize'; -import { matchNames, type CastPerson } from './cast'; +import { matchEach, type CastPerson } from './cast'; import { getAllEpisodes } from './rss'; interface Records { @@ -79,7 +79,7 @@ export function peopleBySlug(): Promise>> { fromDatabase(), getAllEpisodes() ]); - const recordOf = matchNames( + const recordOf = matchEach( episodes.flatMap( (episode) => episode.people?.map((person) => person.name) ?? [] ), diff --git a/tests/e2e/episode.spec.ts b/tests/e2e/episode.spec.ts index aeb5bfa..cdde8fb 100644 --- a/tests/e2e/episode.spec.ts +++ b/tests/e2e/episode.spec.ts @@ -994,3 +994,32 @@ test.describe('the stage on phones', () => { } }); }); + +test('going back to an episode left by its number morphs into its card', async ({ + page +}) => { + await page.goto( + '/throwback-frameworks-tailwind-fandom-and-css-with-jhey-tompkins' + ); + const previous = page.locator('.ep-pager-link[rel="prev"]'); + const slugPath = await previous.getAttribute('href'); + const number = (await previous.innerText()).match(/Ep (\d+)/)?.[1]; + expect(number, 'the previous card names its episode number').toBeTruthy(); + + await page.evaluate((href) => { + const link = document.createElement('a'); + link.href = href; + link.id = 'by-number'; + link.textContent = 'by number'; + document.body.append(link); + }, `/${number}`); + await page.locator('#by-number').dispatchEvent('click'); + await expect(page).toHaveURL(new RegExp(`/${number}$`)); + + await page.goBack(); + await expect(page).toHaveURL(/jhey-tompkins$/); + await expect( + page.locator(`[data-ep-card][href="${slugPath}"] .ep-card-art`), + 'the card for the episode left is named so the stage flies home into it' + ).toHaveAttribute('data-ep-named', ''); +}); diff --git a/tests/unit/cast.test.ts b/tests/unit/cast.test.ts index 3314246..514b187 100644 --- a/tests/unit/cast.test.ts +++ b/tests/unit/cast.test.ts @@ -1,6 +1,8 @@ import { describe, expect, it } from 'vitest'; import { factGivesCount, + hostVoices, + matchEach, matchNames, matchPageNames, ordinal, @@ -239,6 +241,43 @@ describe('matchNames', () => { }); }); +describe('matchEach', () => { + it('gives every spelling of one person their record', () => { + const records = matchEach( + ['Robbie Wagner', 'RobbieTheWagner', 'Chuck Carpenter'], + ['Robbie Wagner', 'Chuck Carpenter', 'Adam Argyle'] + ); + expect(Object.fromEntries(records)).toEqual({ + 'Robbie Wagner': 'Robbie Wagner', + RobbieTheWagner: 'Robbie Wagner', + 'Chuck Carpenter': 'Chuck Carpenter' + }); + }); + + it('leaves a name that could be either of two records unmatched', () => { + const records = matchEach( + ['Adam', 'Adam Argyle'], + ['Adam Argyle', 'Adam Rackis'] + ); + expect(Object.fromEntries(records)).toEqual({ + 'Adam Argyle': 'Adam Argyle' + }); + }); +}); + +describe('hostVoices', () => { + it('gives the first two configured hosts their voices under the names the episodes use', () => { + const voices = hostVoices( + ['Robbie Wagner', 'Chuck Carpenter', 'Adam Argyle'], + ['RobbieTheWagner', 'Chuck Carpenter', 'Adam Argyle', 'Travis Wagner'] + ); + expect(Object.fromEntries(voices)).toEqual({ + RobbieTheWagner: 0, + 'Chuck Carpenter': 1 + }); + }); +}); + describe('matchPageNames', () => { const speakers = ['Robbie Wagner', 'Adam Argyle']; const guests = ['Travis Wagner', 'Lane Wagner', 'Adam Rackis', 'Adam Stacoviak'];