youtobe video added
This commit is contained in:
22
lib/embedUrl.ts
Normal file
22
lib/embedUrl.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
export function toEmbedUrl(url: string): string {
|
||||
let videoId = '';
|
||||
|
||||
// watch?v=...
|
||||
if (url.includes('watch?v=')) {
|
||||
const params = new URL(url).searchParams;
|
||||
videoId = params.get('v') || '';
|
||||
}
|
||||
// youtu.be/...
|
||||
else if (url.includes('youtu.be/')) {
|
||||
const pathname = new URL(url).pathname; // /videoId
|
||||
videoId = pathname.split('/')[1] || '';
|
||||
}
|
||||
// shorts/...
|
||||
else if (url.includes('youtube.com/shorts/')) {
|
||||
const pathname = new URL(url).pathname; // /shorts/videoId
|
||||
videoId = pathname.split('/')[2] || '';
|
||||
}
|
||||
|
||||
return videoId ? `https://www.youtube.com/embed/${videoId}` : '';
|
||||
}
|
||||
Reference in New Issue
Block a user