-
-
Notifications
You must be signed in to change notification settings - Fork 29
Make timestamps in transcripts clickable, use RSS transcripts #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import type { ComponentChildren, JSX } from 'preact'; | ||
|
|
||
| import { currentEpisode, seekToEpisode } from '../state'; | ||
|
|
||
| type Props = { | ||
| episode: NonNullable<(typeof currentEpisode)['value']>; | ||
| children: ComponentChildren; | ||
| }; | ||
|
|
||
| /** | ||
| * Wraps a server-rendered markdown transcript and makes its timestamp buttons | ||
| * (injected by the `rehype-transcript-timestamps` plugin) seek the player. | ||
| * | ||
| * The markdown HTML is rendered by Astro and passed in as children, so a single | ||
| * delegated click handler on the container drives every `[data-seek]` button | ||
| * rather than hydrating each one. | ||
| */ | ||
| export default function MarkdownTranscript({ episode, children }: Props) { | ||
| function handleClick(event: JSX.TargetedMouseEvent<HTMLElement>) { | ||
| const button = (event.target as HTMLElement).closest('[data-seek]'); | ||
| if (!button) { | ||
| return; | ||
| } | ||
|
|
||
| const seconds = Number(button.getAttribute('data-seek')); | ||
| if (Number.isFinite(seconds)) { | ||
| seekToEpisode(episode, seconds); | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <article | ||
| class="transcript prose prose-neutral dark:prose-invert line-clamp-4" | ||
| onClick={handleClick} | ||
| > | ||
| {children} | ||
| </article> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { currentEpisode, seekToEpisode } from '../state'; | ||
| import type { TranscriptParagraph } from '../../lib/transcript'; | ||
|
|
||
| type Props = { | ||
| episode: NonNullable<(typeof currentEpisode)['value']>; | ||
| paragraphs: Array<TranscriptParagraph>; | ||
| }; | ||
|
|
||
| function formatTimestamp(seconds: number): string { | ||
| const total = Math.floor(seconds); | ||
| const hours = Math.floor(total / 3600); | ||
| const minutes = Math.floor((total % 3600) / 60); | ||
| const secs = total % 60; | ||
| const pad = (value: number) => value.toString().padStart(2, '0'); | ||
|
|
||
| return hours > 0 | ||
| ? `${hours}:${pad(minutes)}:${pad(secs)}` | ||
| : `${minutes}:${pad(secs)}`; | ||
| } | ||
|
|
||
| /** | ||
| * Renders an RSS-sourced transcript as paragraphs, each prefixed with a | ||
| * clickable timestamp that jumps the audio player to that moment (starting the | ||
| * episode first if it isn't the one currently loaded). | ||
| */ | ||
| export default function Transcript({ episode, paragraphs }: Props) { | ||
| function jumpTo(start: number) { | ||
| seekToEpisode(episode, start); | ||
| } | ||
|
|
||
| return ( | ||
| <article class="transcript prose prose-neutral dark:prose-invert line-clamp-4"> | ||
| {paragraphs.map((paragraph, index) => { | ||
| const start = paragraph.start; | ||
|
|
||
| return ( | ||
| <p key={index}> | ||
| {start !== undefined && ( | ||
| <button | ||
| type="button" | ||
| onClick={() => jumpTo(start)} | ||
| class="mr-2 align-baseline font-mono text-sm font-medium text-violet-600 tabular-nums no-underline transition-colors hover:text-violet-500 dark:text-cyan-400 dark:hover:text-cyan-300" | ||
| aria-label={`Play from ${formatTimestamp(start)}`} | ||
| > | ||
| {formatTimestamp(start)} | ||
| </button> | ||
| )} | ||
| {paragraph.text} | ||
| </p> | ||
| ); | ||
| })} | ||
| </article> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // Rehype plugin that turns bracketed transcript timestamps like `[00:01:23]` | ||
| // or `[04:12]` into clickable buttons that seek the audio player. Only the | ||
| // markdown transcripts in `src/content/transcripts` contain this pattern, so | ||
| // this plugin effectively only affects them. | ||
| // | ||
| // The button keeps the original bracketed label as its text and carries the | ||
| // resolved offset (in seconds) in `data-seek`; the client-side markdown | ||
| // transcript island reads that attribute to drive the player. The class list | ||
| // mirrors the RSS transcript timestamps (`Transcript.tsx`) so both look the | ||
| // same. Those utilities are already emitted by Tailwind from that component, | ||
| // so listing them here adds no new CSS. | ||
| const TIMESTAMP = /\[(\d{1,2}):(\d{2})(?::(\d{2}))?\]/g; | ||
|
|
||
| const TIMESTAMP_CLASS = [ | ||
| 'transcript-timestamp', | ||
| 'mr-1', | ||
| 'align-baseline', | ||
| 'font-mono', | ||
| 'text-sm', | ||
| 'font-medium', | ||
| 'text-violet-600', | ||
| 'tabular-nums', | ||
| 'no-underline', | ||
| 'transition-colors', | ||
| 'hover:text-violet-500', | ||
| 'dark:text-cyan-400', | ||
| 'dark:hover:text-cyan-300' | ||
| ]; | ||
|
|
||
| function toSeconds(hours, minutes, seconds) { | ||
| // `[MM:SS]` arrives as (minutes, seconds) with `seconds` undefined. | ||
| return seconds === undefined | ||
| ? Number(hours) * 60 + Number(minutes) | ||
| : Number(hours) * 3600 + Number(minutes) * 60 + Number(seconds); | ||
| } | ||
|
|
||
| function timestampButton(label, seconds) { | ||
| return { | ||
| type: 'element', | ||
| tagName: 'button', | ||
| properties: { | ||
| type: 'button', | ||
| className: [...TIMESTAMP_CLASS], | ||
| 'data-seek': String(seconds), | ||
| 'aria-label': `Play from ${label}` | ||
| }, | ||
| children: [{ type: 'text', value: label }] | ||
| }; | ||
| } | ||
|
|
||
| function splitTextNode(value) { | ||
| const nodes = []; | ||
| let lastIndex = 0; | ||
| let match; | ||
|
|
||
| TIMESTAMP.lastIndex = 0; | ||
| while ((match = TIMESTAMP.exec(value)) !== null) { | ||
| const [label, a, b, c] = match; | ||
| if (match.index > lastIndex) { | ||
| nodes.push({ type: 'text', value: value.slice(lastIndex, match.index) }); | ||
| } | ||
| nodes.push(timestampButton(label, toSeconds(a, b, c))); | ||
| lastIndex = match.index + label.length; | ||
| } | ||
|
|
||
| if (lastIndex < value.length) { | ||
| nodes.push({ type: 'text', value: value.slice(lastIndex) }); | ||
| } | ||
|
|
||
| return nodes; | ||
| } | ||
|
|
||
| function transform(node) { | ||
| if (!node.children || node.children.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| const nextChildren = []; | ||
| for (const child of node.children) { | ||
| if ( | ||
| child.type === 'text' && | ||
| // Cheap guard so we only rebuild text nodes that actually contain a | ||
| // timestamp. | ||
| child.value.includes('[') && | ||
| TIMESTAMP.test(child.value) | ||
| ) { | ||
| nextChildren.push(...splitTextNode(child.value)); | ||
| } else { | ||
| transform(child); | ||
| nextChildren.push(child); | ||
| } | ||
| } | ||
|
|
||
| node.children = nextChildren; | ||
| } | ||
|
|
||
| export default function rehypeTranscriptTimestamps() { | ||
| return (tree) => { | ||
| transform(tree); | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.