v3: comments say what the code can't — the documentation pass wanted JSDoc on every export and every field of an exported interface, which filled the code with restatements ("The dump.", "Whether it is loading.", "The props of X.", "Renders the X.") and module docs that told a reader nothing ("The hot and new feeds, and the props every main feed of the index takes."); some were wrong as well as vague. Every comment in api/, shared/ and src/ is rewritten under one rule, now stated in the README's "Comments" section: every module and export still has a JSDoc, and it gives context (who uses it and where), usage (what a caller must know: a precondition, what it leaves to the caller, a side effect) or its place in the chain (where its input comes from, where its output goes); a field, parameter, private function or local gets one only when it adds to its parent's, and context about a whole type goes on the type; a type's doc is phrased from the value's side ("Returned to Y by X when Z", "Passed to Y by X"); full sentences, as short as they can be, no label-colon-list; // goes wherever the code isn't obvious and says why, and the section banners and narration go. Every claim was checked against the code, which turned up some forty comments that were wrong: DumpCard's claimed it was only used by the hot and new feeds, the WebSocket welcome claimed to be a socket's first message (presence comes first), UpdateUserRequest said null removes a description (it keeps it), deletedDumpIds leaves out that it also holds dumps made private, THEME_COLOR claimed to mirror index.html's, and the like. The routers' JSDoc now says where they are mounted and what becomes of a thrown APIException. doc-lint.ts lets a field without a doc through; a module or export without one still fails. No code changes; 204 backend and 628 frontend tests pass, and doc:lint, deno check, deno lint and tsc are clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
/**
|
||||
* The YouTube link provider: videos, playlists and channels.
|
||||
* Previews YouTube videos, playlists and channels, for
|
||||
* `rich-content-service.ts`. Videos and playlists are read from YouTube's
|
||||
* oEmbed endpoint, channels from their page's OG tags.
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
@@ -9,7 +11,10 @@ import type { RichContentProvider } from "./provider.ts";
|
||||
import { extractOgTag } from "../html.ts";
|
||||
import { fetchWithTimeout, readTextCapped } from "../fetch.ts";
|
||||
|
||||
/** Returns the id of the video a youtu.be or youtube.com URL points at. */
|
||||
/**
|
||||
* Returns the id of the video a URL points at, on youtu.be, youtube.com or
|
||||
* www.youtube.com only; `m.` and `music.` links go to the generic provider.
|
||||
*/
|
||||
function extractVideoId(url: string): string | null {
|
||||
try {
|
||||
const u = new URL(url);
|
||||
@@ -32,7 +37,10 @@ function extractVideoId(url: string): string | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Returns the `list` parameter of a URL: the playlist it plays. */
|
||||
/**
|
||||
* Returns the `list` parameter of a URL, whatever its host: a link to another
|
||||
* site with a `list` parameter is taken for a YouTube playlist.
|
||||
*/
|
||||
function extractPlaylistId(url: string): string | null {
|
||||
try {
|
||||
return new URL(url).searchParams.get("list");
|
||||
@@ -42,9 +50,8 @@ function extractPlaylistId(url: string): string | null {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the playback offset in seconds of a YouTube URL.
|
||||
* Accepts `t` (plain seconds, `90s`, or `1h2m3s` / `20m5s` forms) and `start`
|
||||
* (plain seconds). Returns null when absent or unparseable.
|
||||
* Returns the start time of a YouTube URL in seconds, from `t` (`90`, `90s`,
|
||||
* `1h2m3s`) or `start`; null when there is none that parses.
|
||||
*/
|
||||
function extractStartSeconds(url: string): number | null {
|
||||
let raw: string | null;
|
||||
@@ -66,8 +73,8 @@ function extractStartSeconds(url: string): number | null {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path of a channel URL: /channel/UC…, /@handle, /c/name or
|
||||
* /user/name.
|
||||
* Returns the path of a youtube.com channel URL (`/channel/UC…`, `/@handle`,
|
||||
* `/c/name` or `/user/name`); null for any other URL.
|
||||
*/
|
||||
function extractChannelPath(url: string): string | null {
|
||||
try {
|
||||
@@ -90,7 +97,7 @@ function extractChannelPath(url: string): string | null {
|
||||
|
||||
/**
|
||||
* Fetches the title and thumbnail of `url` from YouTube's oEmbed endpoint;
|
||||
* empty when that fails.
|
||||
* empty when that fails, since the preview can do without them.
|
||||
*/
|
||||
async function fetchOEmbed(
|
||||
url: string,
|
||||
@@ -109,14 +116,16 @@ async function fetchOEmbed(
|
||||
}
|
||||
await res.body?.cancel();
|
||||
} catch {
|
||||
// oembed failed — carry on
|
||||
// YouTube unreachable, or its answer not JSON.
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
/**
|
||||
* The provider for YouTube videos (with their playlist and start time in the embed),
|
||||
* playlists (as a `videoseries` embed) and channels (not embeddable).
|
||||
* Comes first in the service's list. A video's embed keeps the link's playlist
|
||||
* and start time, a playlist embeds as a `videoseries`, and a channel gets no
|
||||
* embed. Its `fetch` never rejects: whatever YouTube doesn't answer is left
|
||||
* out.
|
||||
*/
|
||||
export const youtubeProvider: RichContentProvider = {
|
||||
name: "youtube",
|
||||
@@ -134,9 +143,8 @@ export const youtubeProvider: RichContentProvider = {
|
||||
const listId = extractPlaylistId(url);
|
||||
const channelPath = extractChannelPath(url);
|
||||
|
||||
// ── Channel ───────────────────────────────────────────────────────────────
|
||||
if (channelPath && !videoId) {
|
||||
// oEmbed doesn't support channel URLs — scrape og:image from the page instead
|
||||
// oEmbed doesn't take channel URLs.
|
||||
let title: string | undefined;
|
||||
let thumbnailUrl: string | undefined;
|
||||
try {
|
||||
@@ -149,7 +157,7 @@ export const youtubeProvider: RichContentProvider = {
|
||||
await res.body?.cancel();
|
||||
}
|
||||
} catch {
|
||||
// scrape failed — carry on with undefined values
|
||||
// YouTube unreachable: the preview goes without.
|
||||
}
|
||||
return {
|
||||
type: "youtube",
|
||||
@@ -157,11 +165,9 @@ export const youtubeProvider: RichContentProvider = {
|
||||
url,
|
||||
title,
|
||||
thumbnailUrl,
|
||||
// channels are not embeddable as a player
|
||||
};
|
||||
}
|
||||
|
||||
// ── Playlist (no specific video) ──────────────────────────────────────────
|
||||
if (listId && !videoId) {
|
||||
const { title, thumbnailUrl } = await fetchOEmbed(url);
|
||||
return {
|
||||
@@ -175,7 +181,7 @@ export const youtubeProvider: RichContentProvider = {
|
||||
};
|
||||
}
|
||||
|
||||
// ── Video (with optional playlist context) ────────────────────────────────
|
||||
// Built, not taken from oEmbed, so it is there even when oEmbed fails.
|
||||
const thumbnailUrl = `https://img.youtube.com/vi/${videoId}/hqdefault.jpg`;
|
||||
const { title } = await fetchOEmbed(url);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user