61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
export type EmbedInfo = {
|
|
platform: 'youtube' | 'dailymotion' | 'soundcloud' | 'spotify'
|
|
id: string
|
|
embedUrl: string
|
|
}
|
|
|
|
export function getEmbedInfo(url: string): EmbedInfo | null {
|
|
// YouTube
|
|
const ytMatch = url.match(
|
|
/(?:youtube\.com\/(?:watch\?v=|embed\/|shorts\/)|youtu\.be\/)([a-zA-Z0-9_-]{11})/,
|
|
)
|
|
if (ytMatch) {
|
|
return {
|
|
platform: 'youtube',
|
|
id: ytMatch[1],
|
|
embedUrl: `https://www.youtube.com/embed/${ytMatch[1]}?autoplay=1`,
|
|
}
|
|
}
|
|
|
|
// Dailymotion
|
|
const dmMatch = url.match(
|
|
/(?:dailymotion\.com\/video\/|dai\.ly\/)([a-zA-Z0-9]+)/,
|
|
)
|
|
if (dmMatch) {
|
|
return {
|
|
platform: 'dailymotion',
|
|
id: dmMatch[1],
|
|
embedUrl: `https://www.dailymotion.com/embed/video/${dmMatch[1]}?autoplay=1`,
|
|
}
|
|
}
|
|
|
|
// SoundCloud
|
|
if (url.includes('soundcloud.com/')) {
|
|
return {
|
|
platform: 'soundcloud',
|
|
id: url,
|
|
embedUrl: `https://w.soundcloud.com/player/?url=${encodeURIComponent(url)}&auto_play=true&visual=true`,
|
|
}
|
|
}
|
|
|
|
// Spotify (episodes, shows, tracks)
|
|
const spotifyMatch = url.match(
|
|
/open\.spotify\.com\/(episode|show|track)\/([a-zA-Z0-9]+)/,
|
|
)
|
|
if (spotifyMatch) {
|
|
const type = spotifyMatch[1]
|
|
const id = spotifyMatch[2]
|
|
return {
|
|
platform: 'spotify',
|
|
id,
|
|
embedUrl: `https://open.spotify.com/embed/${type}/${id}?utm_source=generator&theme=0`,
|
|
}
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
export function isExternalUrl(url: string): boolean {
|
|
return getEmbedInfo(url) !== null
|
|
}
|