UNPKG

better-video-embeds

Version:

Pass in a url that should be a video embed link...and get back a properly formated embed link. (Works for YouTube, Vimeo, Wistia and more!)

40 lines (30 loc) 1.5 kB
var getDailyMotion = function getDailyMotion(dirtyUrl) { var dailyMotionRegex = /(?:dailymotion\.com(?:\/video|\/hub)|dai\.ly)\/([0-9a-z]+)(?:[\-_0-9a-zA-Z]+#video=([a-z0-9]+))?/g; var result = dailyMotionRegex.exec(dirtyUrl); return result && result[1] && "https://www.dailymotion.com/embed/video/" + result[1] || dirtyUrl; }; var getWistia = function getWistia(dirtyUrl) { var wistiaResults = dirtyUrl.split('/'); var result = wistiaResults[wistiaResults.length - 1]; return result ? "https://fast.wistia.net/embed/iframe/" + result : dirtyUrl; }; var getEmbedUrl = function getEmbedUrl(dirtyUrl, params) { if (!dirtyUrl) return ''; var vimeo = /vimeo.*\/(\d+)/i.exec(dirtyUrl); if (vimeo && vimeo[1]) { return "https://player.vimeo.com/video/" + vimeo[1]; } if (dirtyUrl.includes('dailymotion') || dirtyUrl.includes('dai.ly')) { return getDailyMotion(dirtyUrl); } else if (dirtyUrl.includes('wistia')) { return getWistia(dirtyUrl); } var youtubeRegex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|&v=)([^#&?]*).*/; var matchesYouTube = dirtyUrl.match(youtubeRegex); if (matchesYouTube && matchesYouTube[2].length === 11) { return "https://www.youtube.com/embed/" + matchesYouTube[2] + (params && params.includeModestYouTubeBranding ? '?rel=0&showinfo=0&modestbranding=0&enablejsapi=1' : ''); } // fallback to just returning the link return dirtyUrl; }; export default getEmbedUrl; //# sourceMappingURL=better-video-embeds.esm.js.map