fix #2443: add parseYoutubeStart() to convert start times (#2447)

* fix #2443: add parseYoutubeStart() to convert start times

* handle null parameter

* add spaces for lint

* switch to regex, handles more cases, simply don't touch unexpected input

* force braces for lint
This commit is contained in:
Scroogey-SN 2025-08-29 17:54:39 +00:00 committed by GitHub
parent cbcb8cb96c
commit acd5b69087
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -78,6 +78,17 @@ export function parseInternalLinks (href) {
return {}
}
export function parseYoutubeStart (t) {
// https://stackoverflow.com/questions/17379268/youtube-dropped-t-start-time-support-in-direct-url-and-embed-videos
// https://developers.google.com/youtube/player_parameters#start
if (!t || !t.match(/^([0-9]+[smh])+$/g)) return t
let r = 0
for (const m of t.matchAll(/([0-9]+)([smh])/g)) {
r += parseInt(m[1]) * Math.pow(60, 'smh'.indexOf(m[2]))
}
return r.toString()
}
export function parseEmbedUrl (href) {
if (!href) return null
@ -132,7 +143,7 @@ export function parseEmbedUrl (href) {
id: searchParams.get('v'),
meta: {
href,
start: searchParams.get('t')
start: parseYoutubeStart(searchParams.get('t'))
}
}
}
@ -152,7 +163,7 @@ export function parseEmbedUrl (href) {
id: pathname.slice(1), // remove leading slash
meta: {
href,
start: searchParams.get('t')
start: parseYoutubeStart(searchParams.get('t'))
}
}
}