UNPKG

@social-embed/lib

Version:

URL detection and parsing for embed providers (YouTube, other OEmbed compatible providers),

386 lines (385 loc) 10.5 kB
const D = (e) => { try { return !!new URL(e); } catch { return !1; } }, s = /^(?:(?:https?):)?(?:\/\/)?(?:www\.)?(?:youtube(?:-nocookie)?.com|youtu.be)(?:\/watch\?v)?[=/]([a-zA-Z0-9_-]{11})(?:\\?|=|&|$)/, U = (e) => { var r; return e ? ((r = e.match(s)) == null ? void 0 : r[1]) ?? "" : ""; }, w = (e) => `https://www.youtube.com/embed/${e}`, f = { /** @inheritdoc */ name: "YouTube", /** * Determines if a given URL is recognized as a YouTube link. * * @param url - The string to evaluate. * @returns `true` if the URL matches YouTube patterns; otherwise `false`. */ canParseUrl(e) { return s.test(e); }, /** * Extracts the 11-char YouTube ID from a recognized YouTube URL. * * @param url - The potential YouTube link. * @returns The extracted video ID or an empty string if none found. */ getIdFromUrl(e) { return U(e); }, /** * Builds a full YouTube embed URL (e.g. `https://www.youtube.com/embed/<id>`). * * @param id - The 11-char YouTube video ID. * @returns A fully embeddable `<iframe>` URL for YouTube. */ getEmbedUrlFromId(e) { return w(e); } }, i = /^(?:(?:https?):)?(?:\/\/)?(?:www\.)?(?:(?:dailymotion\.com(?:\/embed)?\/video)|dai\.ly)\/([a-zA-Z0-9]+)(?:_[\w_-]+)?(?:\?playlist=[a-zA-Z0-9]+)?$/, y = (e) => { var r; return ((r = e.match(i)) == null ? void 0 : r[1]) ?? ""; }, h = (e) => `https://www.dailymotion.com/embed/video/${e}`, b = { /** @inheritdoc */ name: "DailyMotion", /** * Checks whether a given URL matches the DailyMotion pattern. * * @param url - The URL to test for DailyMotion compatibility. * @returns `true` if the URL belongs to DailyMotion, otherwise `false`. */ canParseUrl(e) { return i.test(e); }, /** * Extracts the ID from a recognized DailyMotion URL. * * @param url - The DailyMotion URL string. * @returns The video ID found in the URL. */ getIdFromUrl(e) { return y(e); }, /** * Builds an embed URL for a given DailyMotion ID. * * @param id - The DailyMotion video ID. * @returns A fully embeddable iframe URL for DailyMotion. */ getEmbedUrlFromId(e) { return h(e); } }, n = [ "track", "album", "playlist", "artist", "show", "episode" ], d = new RegExp( `^(?:(?:https?):)?(?:\\/\\/)?(?:embed\\.|open\\.)?spotify\\.com\\/(?:(${n.join( "|" )})\\/)([-\\w]{22})(?:\\?si=[_\\-\\w]{22})?` ), m = new RegExp( `^spotify:(?:(${n.join("|")}):)([-\\w]{22})` ); function v(e) { const r = e.match(d) || e.match(m); if (!r) return ["", ""]; const t = r[1] ?? ""; return [r[2] ?? "", t]; } function I(e, ...r) { const t = typeof r[0] == "string" ? r[0] : ""; return `https://open.spotify.com/embed/${n.includes(t) ? t : "track"}/${e}`; } const F = { /** @inheritdoc */ name: "Spotify", /** * Determines if the given URL or URI matches a recognized Spotify pattern. * * @param url - A string that could point to a Spotify resource. * @returns `true` if the pattern matches, otherwise `false`. */ canParseUrl(e) { return d.test(e) || m.test(e); }, /** * Extracts the ID and type from a recognized Spotify URL or `spotify:` URI. * * @param url - The Spotify link. * @returns `[id, type]` if matched, otherwise `["", ""]`. */ getIdFromUrl(e) { return v(e); }, /** * Builds an embeddable Spotify URL from an ID plus optional arguments (e.g. type). * * @param id - The 22-char Spotify ID. * @param args - The first arg may be the content type (`track`, `album`, `playlist`, etc.). * @returns The final embed URL, e.g. `"https://open.spotify.com/embed/track/<id>"`. */ getEmbedUrlFromId(e, ...r) { return I(e, ...r); } }, c = /(?:(?:https?):)?(?:\/\/)?(?:www\.|player\.)?vimeo\.com\/(?:channels\/(?:\w+\/)?|groups\/(?:[^/]*)\/videos\/|video\/|)(\d+)(?:|\/\?)/, P = (e) => { var r; return ((r = e.match(c)) == null ? void 0 : r[1]) ?? ""; }, E = (e) => `https://player.vimeo.com/video/${e}`, z = { /** @inheritdoc */ name: "Vimeo", /** * Determines if the given URL matches the Vimeo pattern. * * @param url - The URL string to test. * @returns `true` if the URL is recognized as Vimeo; otherwise `false`. */ canParseUrl(e) { return c.test(e); }, /** * Extracts the Vimeo video ID from a recognized URL. * * @param url - The Vimeo URL. * @returns The numeric video ID. */ getIdFromUrl(e) { return P(e); }, /** * Builds an embeddable Vimeo URL from a numeric video ID. * * @param id - The numeric Vimeo ID. * @returns A fully embeddable URL for use in `<iframe src="...">`. */ getEmbedUrlFromId(e) { return E(e); } }, a = /^(?:(?:https?):)?(?:\/\/)?(?:www\.)?edpuzzle.com[=/]media\/([-\w]+)/, R = (e) => { var r; if (e) { if ((e == null ? void 0 : e.length) > 1e3) throw new Error("URL too long"); return ((r = e.match(a)) == null ? void 0 : r[1]) ?? ""; } return ""; }, T = (e) => `https://edpuzzle.com/embed/media/${e}`, $ = { /** @inheritdoc */ name: "EdPuzzle", /** * Checks if the given URL belongs to EdPuzzle. * * @param url - A string potentially matching EdPuzzle. * @returns `true` if EdPuzzle is recognized; otherwise `false`. */ canParseUrl(e) { return a.test(e); }, /** * Extracts the EdPuzzle ID from the recognized EdPuzzle URL. * * @param url - The EdPuzzle media URL. * @returns The extracted EdPuzzle ID. */ getIdFromUrl(e) { return R(e); }, /** * Builds an embeddable EdPuzzle URL from the given media ID. * * @param id - The EdPuzzle media ID. * @returns A full embed URL (e.g. `https://edpuzzle.com/embed/media/<id>`). */ getEmbedUrlFromId(e) { return T(e); } }, l = /^(?:(?:https?):)?(?:\/\/)?(?:www|fast|support\.)?(?:wistia.com|wi.st)[=/](?:medias|embed)\/([-\w]+)/, x = (e) => { var r; if (e) { if (e.length > 1e3) throw new Error("URL too long"); return ((r = e.match(l)) == null ? void 0 : r[1]) ?? ""; } return ""; }, A = (e) => `https://fast.wistia.net/embed/iframe/${e}`, L = { /** @inheritdoc */ name: "Wistia", /** * Checks if a URL is recognized as a Wistia link by testing the pattern {@link wistiaUrlRegex}. * * @param url - The string to test. * @returns `true` if it matches Wistia patterns, otherwise `false`. */ canParseUrl(e) { return l.test(e); }, /** * Extracts the Wistia media ID from a recognized Wistia URL. * * @param url - The shared Wistia link. * @returns The extracted Wistia ID (e.g. `"26sk4lmiix"`). */ getIdFromUrl(e) { return x(e); }, /** * Constructs a Wistia embed URL given a media ID. * * @param id - The Wistia media ID. * @returns An `<iframe>`-friendly URL (e.g. `https://fast.wistia.net/embed/iframe/<id>`). */ getEmbedUrlFromId(e) { return A(e); } }, u = /^(?:(?:https?):)?(?:\/\/)?(?:www\.)?loom.com[=/]share\/([-\w]+)/, S = (e) => { var r; if (e) { if (e.length > 1e3) throw new Error("URL too long"); return ((r = e.match(u)) == null ? void 0 : r[1]) ?? ""; } return ""; }, M = (e) => `https://www.loom.com/embed/${e}`, Y = { /** @inheritdoc */ name: "Loom", /** * Returns `true` if the provided URL matches the Loom share pattern. * * @param url - The URL to test for Loom compatibility. * @returns `true` if Loom recognized; otherwise `false`. */ canParseUrl(e) { return u.test(e); }, /** * Extracts the Loom video ID from a recognized Loom URL. * * @param url - The shared Loom link. * @returns The parsed Loom video ID. */ getIdFromUrl(e) { return S(e); }, /** * Builds an embeddable Loom URL from a given Loom video ID. * * @param id - The Loom ID (e.g. `"e883f70b219a49f6ba7fbeac71a72604"`). * @returns The final embed URL (e.g. `https://www.loom.com/embed/<id>`). */ getEmbedUrlFromId(e) { return M(e); } }; function V(e) { if (e) return p.findProviderByUrl(e); } function W(e) { const r = V(e); if (!r) return ""; const t = r.getIdFromUrl(e); if (Array.isArray(t)) { const [o, ...g] = t; return o ? r.getEmbedUrlFromId(o, ...g) : ""; } return r.getEmbedUrlFromId(t); } class _ { /** * Creates an instance of {@link EmbedProviderRegistry}. */ constructor() { this.providers = /* @__PURE__ */ new Map(); } /** * Registers a provider. * * @remarks * If a provider with the same name is already present, it will be overwritten. * * @param provider - The {@link EmbedProvider} instance to register. */ register(r) { this.providers.set(r.name, r); } /** * Lists all registered providers. * * @returns An array of all currently registered {@link EmbedProvider} instances. */ listProviders() { return Array.from(this.providers.values()); } /** * Retrieves a provider by name. * * @param name - A provider’s `name` property. * @returns The matching {@link EmbedProvider}, or `undefined` if not found. */ getProviderByName(r) { return this.providers.get(r); } /** * Finds the first provider that can parse the given URL. * * @param url - The URL to analyze. * @returns The matching {@link EmbedProvider}, or `undefined` if no provider recognizes it. */ findProviderByUrl(r) { for (const t of this.providers.values()) if (t.canParseUrl(r)) return t; } } const B = [ b, $, Y, F, z, L, f ], p = new _(); for (const e of B) p.register(e); export { b as DailyMotionProvider, $ as EdPuzzleProvider, Y as LoomProvider, n as SPOTIFY_TYPES, F as SpotifyProvider, z as VimeoProvider, L as WistiaProvider, f as YouTubeProvider, W as convertUrlToEmbedUrl, i as dailyMotionUrlRegex, B as defaultProviders, p as defaultRegistry, a as edPuzzleUrlRegex, h as getDailyMotionEmbedFromId, y as getDailyMotionIdFromUrl, T as getEdPuzzleEmbedUrlFromId, R as getEdPuzzleIdFromUrl, M as getLoomEmbedUrlFromId, S as getLoomIdFromUrl, V as getProviderFromUrl, I as getSpotifyEmbedUrlFromIdAndType, v as getSpotifyIdAndTypeFromUrl, E as getVimeoEmbedUrlFromId, P as getVimeoIdFromUrl, A as getWistiaEmbedUrlFromId, x as getWistiaIdFromUrl, w as getYouTubeEmbedUrlFromId, U as getYouTubeIdFromUrl, D as isValidUrl, u as loomUrlRegex, m as spotifySymbolRegex, d as spotifyUrlRegex, c as vimeoUrlRegex, l as wistiaUrlRegex, s as youTubeUrlRegex };