@social-embed/lib
Version:
URL detection and parsing for embed providers (YouTube, other OEmbed compatible providers),
57 lines • 2 kB
TypeScript
import { EmbedProvider } from '../provider';
/**
* Regex matcher for YouTube URLs.
*
* @remarks
* This pattern captures the 11-character YouTube video ID from multiple formats:
* - `https://www.youtube.com/watch?v=...`
* - `https://youtu.be/...`
* - Variants with or without `www.` / `-nocookie`
*
* **Credit**: [Stack Overflow](https://stackoverflow.com/a/42442074)
*/
export declare const youTubeUrlRegex: RegExp;
/**
* Extracts an 11-character YouTube video ID from a given URL string.
*
* @param url - The string potentially representing a YouTube link (can be `undefined`).
* @returns The extracted video ID, or an empty string if no match is found.
*
* @example
* ```ts
* // With a watch URL:
* const videoId = getYouTubeIdFromUrl("https://www.youtube.com/watch?v=FTQbiNvZqaY");
* console.log(videoId); // "FTQbiNvZqaY"
*
* // With a shortlink:
* const shortId = getYouTubeIdFromUrl("https://youtu.be/FTQbiNvZqaY");
* console.log(shortId); // "FTQbiNvZqaY"
* ```
*
* @remarks
* If `url` is `undefined` or not a valid YouTube link, returns an empty string.
*/
export declare const getYouTubeIdFromUrl: (url: string | undefined) => string;
/**
* Constructs a YouTube embed URL from a 11-character video ID.
*
* @param id - The YouTube video ID.
* @returns An embeddable URL in the form `https://www.youtube.com/embed/<id>`.
*
* @example
* ```ts
* console.log(getYouTubeEmbedUrlFromId("FTQbiNvZqaY"));
* // "https://www.youtube.com/embed/FTQbiNvZqaY"
* ```
*/
export declare const getYouTubeEmbedUrlFromId: (id: string | undefined) => string;
/**
* A provider implementation for YouTube, satisfying the {@link EmbedProvider} interface.
*
* @remarks
* - `canParseUrl()` detects if a URL belongs to YouTube using {@link youTubeUrlRegex}.
* - `getIdFromUrl()` extracts the YouTube video ID (11 chars).
* - `getEmbedUrlFromId()` builds a playable embed URL.
*/
export declare const YouTubeProvider: EmbedProvider;
//# sourceMappingURL=youtube.d.ts.map