mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-24 01:17:50 +08:00
64 lines
1.3 KiB
TypeScript
64 lines
1.3 KiB
TypeScript
|
export const isValidYoutubeUrl = (url: string) => {
|
||
|
return url.match(/^(https?:\/\/)?(www\.|music\.)?(youtube\.com|youtu\.be)(.+)?$/)
|
||
|
}
|
||
|
|
||
|
export interface GetEmbedUrlOptions {
|
||
|
url: string;
|
||
|
controls?: boolean;
|
||
|
nocookie?: boolean;
|
||
|
startAt?: number;
|
||
|
}
|
||
|
|
||
|
export const getYoutubeEmbedUrl = (nocookie?: boolean) => {
|
||
|
return nocookie ? 'https://www.youtube-nocookie.com/embed/' : 'https://www.youtube.com/embed/'
|
||
|
}
|
||
|
|
||
|
export const getEmbedURLFromYoutubeURL = (options: GetEmbedUrlOptions) => {
|
||
|
const {
|
||
|
url,
|
||
|
controls,
|
||
|
nocookie,
|
||
|
startAt,
|
||
|
} = options
|
||
|
|
||
|
// if is already an embed url, return it
|
||
|
if (url.includes('/embed/')) {
|
||
|
return url
|
||
|
}
|
||
|
|
||
|
// if is a youtu.be url, get the id after the /
|
||
|
if (url.includes('youtu.be')) {
|
||
|
const id = url.split('/').pop()
|
||
|
|
||
|
if (!id) {
|
||
|
return null
|
||
|
}
|
||
|
return `${getYoutubeEmbedUrl(nocookie)}${id}`
|
||
|
}
|
||
|
|
||
|
const videoIdRegex = /v=([-\w]+)/gm
|
||
|
const matches = videoIdRegex.exec(url)
|
||
|
|
||
|
if (!matches || !matches[1]) {
|
||
|
return null
|
||
|
}
|
||
|
|
||
|
let outputUrl = `${getYoutubeEmbedUrl(nocookie)}${matches[1]}`
|
||
|
|
||
|
const params = []
|
||
|
|
||
|
if (!controls) {
|
||
|
params.push('controls=0')
|
||
|
}
|
||
|
|
||
|
if (startAt) {
|
||
|
params.push(`start=${startAt}`)
|
||
|
}
|
||
|
|
||
|
if (params.length) {
|
||
|
outputUrl += `?${params.join('&')}`
|
||
|
}
|
||
|
|
||
|
return outputUrl
|
||
|
}
|