fix: youtube embeds not working

This commit is contained in:
Xylight committed 2024-06-08 14:47:03 -07:00
1 parent f5b380f005
commit 74701ffe3f
2 files changed
+21 -5

No files matched your search

+7 -3
View File
@@ -35,14 +35,18 @@ export const optimizeImageURL = (
try { try {
const url = new URL(urlStr) const url = new URL(urlStr)
url.searchParams.append('format', 'webp') 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() return url.toString()
} catch (e) { } catch (e) {
return urlStr 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 => { export const isYoutubeLink = (url?: string): RegExpMatchArray | null => {
if (!url) return null if (!url) return null
@@ -69,4 +73,4 @@ export const iframeType = (post: Post): IframeType => {
if (isVideo(post.url)) return 'video' if (isVideo(post.url)) return 'video'
if (isYoutubeLink(post.url)) return 'youtube' if (isYoutubeLink(post.url)) return 'youtube'
return 'none' return 'none'
} }
@@ -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
}
</script> </script>
<script lang="ts"> <script lang="ts">
@@ -36,8 +48,8 @@
if (type == 'youtube') { if (type == 'youtube') {
const url = new URL(inputUrl) const url = new URL(inputUrl)
const videoID = url.searchParams.get('v')
url.searchParams.delete('v') const videoID = youtubeVideoID(inputUrl)
return `https://${youtubeDomain( return `https://${youtubeDomain(
$userSettings.embeds.youtube $userSettings.embeds.youtube