feat(youtube): enhance embed URL generation to support video lists (#6096)

* feat(youtube): enhance embed URL generation to support video lists

* chore: add changeset

* feat(youtube): update embed URL generation to support playlists
This commit is contained in:
Simon He 2025-03-28 19:44:52 +08:00 committed by GitHub
parent 81009d2160
commit 151de93d05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 5 deletions

View File

@ -0,0 +1,5 @@
---
"@tiptap/extension-youtube": minor
---
feat(youtube): enhance embed URL generation to support video lists

View File

@ -27,7 +27,10 @@ export interface GetEmbedUrlOptions {
rel?: number;
}
export const getYoutubeEmbedUrl = (nocookie?: boolean) => {
export const getYoutubeEmbedUrl = (nocookie?: boolean, isPlaylist?:boolean) => {
if (isPlaylist) {
return 'https://www.youtube-nocookie.com/embed/videoseries?list='
}
return nocookie ? 'https://www.youtube-nocookie.com/embed/' : 'https://www.youtube.com/embed/'
}
@ -73,14 +76,14 @@ export const getEmbedUrlFromYoutubeUrl = (options: GetEmbedUrlOptions) => {
return `${getYoutubeEmbedUrl(nocookie)}${id}`
}
const videoIdRegex = /(?:v=|shorts\/)([-\w]+)/gm
const videoIdRegex = /(?:(v|list)=|shorts\/)([-\w]+)/gm
const matches = videoIdRegex.exec(url)
if (!matches || !matches[1]) {
if (!matches || !matches[2]) {
return null
}
let outputUrl = `${getYoutubeEmbedUrl(nocookie)}${matches[1]}`
let outputUrl = `${getYoutubeEmbedUrl(nocookie, matches[1] === 'list')}${matches[2]}`
const params = []
@ -153,7 +156,7 @@ export const getEmbedUrlFromYoutubeUrl = (options: GetEmbedUrlOptions) => {
}
if (params.length) {
outputUrl += `?${params.join('&')}`
outputUrl += `${matches[1] === 'v' ? '?' : '&'}${params.join('&')}`
}
return outputUrl