diff --git a/src/components/post/embed/embed.tsx b/src/components/post/embed/embed.tsx index 7d7d3a8e..d1d27f25 100644 --- a/src/components/post/embed/embed.tsx +++ b/src/components/post/embed/embed.tsx @@ -7,7 +7,13 @@ interface EmbedProps { const Embed = ({ url }: EmbedProps) => { const parsedUrl = new URL(url); - if (youtubeHosts.has(parsedUrl.host)) { + const isYoutubeVideoUrl = (parsedUrl: URL): boolean => { + const youtubeVideoPattern = /\/watch\?v=/; + const youtubeHostPatterns = /^(www\.)?(youtube\.com|youtu\.be|yt\.)$/; + return youtubeVideoPattern.test(parsedUrl.pathname + parsedUrl.search) || youtubeHostPatterns.test(parsedUrl.host); + }; + + if (isYoutubeVideoUrl(parsedUrl)) { return ; } if (xHosts.has(parsedUrl.host)) { @@ -46,23 +52,26 @@ interface EmbedComponentProps { const youtubeHosts = new Set(['youtube.com', 'www.youtube.com', 'youtu.be', 'www.youtu.be', 'm.youtube.com']); const YoutubeEmbed = ({ parsedUrl }: EmbedComponentProps) => { - let youtubeId; - if (parsedUrl.host.endsWith('youtu.be')) { - youtubeId = parsedUrl.pathname.replaceAll('/', ''); - } else { - youtubeId = parsedUrl.searchParams.get('v'); + let youtubeId = parsedUrl.searchParams.get('v'); + + if (!youtubeId && parsedUrl.host.includes('youtu.be')) { + youtubeId = parsedUrl.pathname.substring(1); } - return ( -