feat: add youtube button for video id extraction

This commit is contained in:
Julien Calixte
2025-11-01 21:54:50 +01:00
parent a8ab50e28b
commit 7b2611129a
2 changed files with 85 additions and 0 deletions

39
src/utils/youtube.ts Normal file
View File

@@ -0,0 +1,39 @@
export const extractYouTubeId = (input: string) => {
if (!input) {
return null
}
let url: URL
try {
url = new URL(input)
} catch {
return input.trim()
}
const host = url.hostname.toLowerCase()
const pathSegments = url.pathname.split("/").filter(Boolean)
if (host.includes("youtu.be")) {
return pathSegments[0] ?? null
}
if (!host.includes("youtube.com")) {
return null
}
const vParam = url.searchParams.get("v")
if (vParam) {
return vParam
}
if (
pathSegments.length >= 2 &&
["embed", "shorts", "live", "watch"].includes(pathSegments[0])
) {
return pathSegments[1]
}
return null
}