From 74701ffe3fed8e924d9bd1bc6e9ef656bc97a892 Mon Sep 17 00:00:00 2001 From: Xylight Date: Sat, 8 Jun 2024 14:47:03 -0700 Subject: [PATCH] fix: youtube embeds not working --- src/lib/components/lemmy/post/helpers.ts | 10 +++++++--- .../lemmy/post/media/PostIframe.svelte | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/lib/components/lemmy/post/helpers.ts b/src/lib/components/lemmy/post/helpers.ts index ed1567df..f7d62aea 100644 --- a/src/lib/components/lemmy/post/helpers.ts +++ b/src/lib/components/lemmy/post/helpers.ts @@ -35,14 +35,18 @@ export const optimizeImageURL = ( try { const url = new URL(urlStr) url.searchParams.append('format', 'webp') - url.searchParams.append('thumbnail', findClosestNumber([128, 256, 512, 1024], width).toString()) + url.searchParams.append( + 'thumbnail', + findClosestNumber([128, 256, 512, 1024], width).toString() + ) return url.toString() } catch (e) { return urlStr } } -const YOUTUBE_REGEX = /^(?:(?:https?:)?\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/ +const YOUTUBE_REGEX = + /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|shorts\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/ export const isYoutubeLink = (url?: string): RegExpMatchArray | null => { if (!url) return null @@ -69,4 +73,4 @@ export const iframeType = (post: Post): IframeType => { if (isVideo(post.url)) return 'video' if (isYoutubeLink(post.url)) return 'youtube' return 'none' -} \ No newline at end of file +} diff --git a/src/lib/components/lemmy/post/media/PostIframe.svelte b/src/lib/components/lemmy/post/media/PostIframe.svelte index 7df04d58..47ecc988 100644 --- a/src/lib/components/lemmy/post/media/PostIframe.svelte +++ b/src/lib/components/lemmy/post/media/PostIframe.svelte @@ -12,6 +12,18 @@ } } } + + function youtubeVideoID(url: string): string | null { + const regex = + /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|shorts\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/ + const match = url.match(regex) + + if (match && match[1]) { + return match[1] + } + + return null + }