/** * @module helpers/utils */ import { isURL } from '../checker/is-url'; import { parseQuery } from './parse-query'; const createYoutubeIframe = ({ videoId, shorts = false }: { videoId: string; shorts?: boolean; }): string => { const width = shorts ? 315 : 560; const height = shorts ? 560 : 315; const src = `https://www.youtube.com/embed/${videoId}`; return ``; }; /** * Javascript url pattern converter replace youtube/vimeo url in embed code. */ export const convertMediaUrlToVideoEmbed = ( url: string, width: number = 560, height: number = 315 ): string => { if (!isURL(url)) { return url; } const parser: HTMLAnchorElement = document.createElement('a'), pattern1: RegExp = /(?:http?s?:\/\/)?(?:www\.)?(?:vimeo\.com)\/?(.+)/g; parser.href = url; if (!width) { width = 400; } if (!height) { height = 345; } const protocol: string = parser.protocol || ''; switch (parser.hostname) { case 'www.vimeo.com': case 'vimeo.com': return pattern1.test(url) ? url.replace( pattern1, '' ) : url; case 'youtube.com': case 'www.youtube.com': { switch (true) { case url.includes('shorts'): { const videoId = parser.pathname.replace('/shorts/', ''); if (!videoId) return url; return createYoutubeIframe({ videoId, shorts: true }); } case url.includes('live'): { const videoId = parser.pathname.replace('/live/', ''); if (!videoId) return url; return createYoutubeIframe({ videoId }); } case url.includes('watch'): { const query = parseQuery(parser.search); const videoId = query.v; if (!videoId) return url; return createYoutubeIframe({ videoId }); } default: return url; } } case 'youtu.be': case 'www.youtu.be': { const videoId = parser.pathname.substring(1); if (videoId) { return createYoutubeIframe({ videoId }); } return url; } case 'www.instagram.com': { const endPoint = parser.pathname; const instagramUrl = `https://${parser.hostname}${endPoint}embed`; return ``; } case 'tv.naver.com': { const naverTvUrl = `https://${ parser.hostname }${parser.pathname.replace('v', 'embed')}`; return ``; } case 'www.twitch.tv': { const endPoint = parser.pathname; const isVideo = endPoint.split('/')[1] === 'videos'; const videoId = endPoint.split('/')[2]; const currentHost = window.location.host; if (isVideo) { return ``; } } case 'vod.afreecatv.com': { const iframeSrc = `${parser.protocol}//${parser.hostname}${parser.pathname}/embed?showChat=false&autoPlay=false&mutePlay=true`; return ``; } } return url; };