forked from BRETT/BRETT
26 lines
731 B
TypeScript
26 lines
731 B
TypeScript
export function getYoutubeId(href: string): string | null {
|
|
try {
|
|
const url = new URL(href)
|
|
if (url.hostname === 'youtu.be') {
|
|
return url.pathname.slice(1).split('/')[0] || null
|
|
}
|
|
if (url.hostname === 'youtube.com' || url.hostname.endsWith('.youtube.com')) {
|
|
if (url.pathname === '/watch') return url.searchParams.get('v')
|
|
const m = url.pathname.match(/^\/(shorts|embed|live)\/([\w-]+)/)
|
|
if (m?.[2]) return m[2]
|
|
}
|
|
return null
|
|
}
|
|
catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
export function youtubeEmbedUrl(videoId: string) {
|
|
return `https://www.youtube.com/embed/${videoId}`
|
|
}
|
|
|
|
export function youtubeThumbUrl(videoId: string) {
|
|
return `https://i.ytimg.com/vi/${videoId}/mqdefault.jpg`
|
|
}
|