export type EmbedSource = { name: string url: string regex: RegExp } const sources: EmbedSource[] = [ { name: 'youtube', url: 'https://www.youtube-nocookie.com/embed/{id}', regex: /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:shorts\/|[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)(?[a-zA-Z0-9_-]{11})/, }, { name: "instagram_post", url: "https://www.instagram.com/p/{id}", regex: /https?:\/\/(?:www\.)?instagram\.com\/p\/(?[^/?#&]+)/, }, { name: "instagram_story", url: "https://www.instagram.com/stories/highlights/{id}", regex: /https?:\/\/(?:www\.)?instagram\.com\/stories\/highlights\/(?[^/?#&]+)/, }, { name: "twitter", url: "https://twitter.com/i/status/{id}", regex: /https?:\/\/(?:www\.)?(twitter\.com|x\.com)\/.+?\/status\/(?[^/?#&]+)/, }, { name: "facebook", url: "https://www.facebook.com/plugins/post.php?href={id}", regex: /https?:\/\/(?:www\.)?facebook\.com\/(?:[^/?#&]+)\/posts\/(?[^/?#&]+)/, }, { name: "tiktok", url: "https://www.tiktok.com/embed/{id}", regex: /https?:\/\/(?:www\.)?tiktok\.com\/(?:[^/?#&]+)\/video\/(?[^/?#&]+)/, }, { name: "vimeo", url: "https://player.vimeo.com/video/{id}", regex: /https?:\/\/(?:www\.)?vimeo\.com(?:\/[^/?#&]+)?\/(?[^/?#&]+)/, }, { name: "spotify", url: "https://open.spotify.com/embed/{id}", regex: /https?:\/\/(?:open\.)?spotify\.com\/(?:[^/?#&]+)\/(?[^/?#&]+)/, }, { name: "twitch_vod", url: "https://player.twitch.tv/?parent={origin}&video={id}", regex: /https?:\/\/(?:www\.)?twitch\.tv\/(?:[^/?#&]+)\/(?[^/?#&]+)/, }, { name: "twitch_clip", url: "https://clips.twitch.tv/embed?parent={origin}&clip={id}", regex: /https?:\/\/(?:www\.)?twitch\.tv\/(?:[^/?#&]+)\/clip\/(?[^/?#&]+)/, }, { name: "twitch_channel", url: "https://player.twitch.tv/?parent={origin}&channel={id}", regex: /https?:\/\/(?:www\.)?twitch\.tv\/(?[^/?#&]+)/, }, { name: "dailymotion", url: "https://www.dailymotion.com/embed/video/{id}", regex: /^.+dailymotion.com\/(video|hub)\/(?[^_]+)[^#]*(#video=([^_&]+))?/, }, ] const isEmbedable = (url: string): boolean => sources.some(source => source.regex.test(url)) const getEmbedable = (url: string): string | null => { const source = sources.find(source => source.regex.test(url)) if (!source) { return null } const match = source.regex.exec(url) if (!match || !match.groups?.id) { return null } return source.url.replace(/\{origin\}/g, window.location.origin).replace(/\{id\}/g, match.groups.id) } export { isEmbedable, getEmbedable }