fix: Passing of start parameter to YouTube embed

This commit is contained in:
Tom Moor
2023-04-05 12:48:48 -04:00
parent 9573026fdd
commit 9a7ecd7403
2 changed files with 17 additions and 3 deletions

View File

@@ -9,6 +9,12 @@ describe("YouTube", () => {
).toBeTruthy();
});
test("to be enabled on video link with timestamp", () => {
expect(
"https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=123s".match(match)
).toBeTruthy();
});
test("to be enabled on embed link", () => {
expect(
"https://www.youtube.com/embed?v=dQw4w9WgXcQ".match(match)

View File

@@ -5,13 +5,21 @@ import { EmbedProps as Props } from ".";
function YouTube(props: Props) {
const { matches } = props.attrs;
const videoId = matches[1];
const queryParameters = matches[2];
let start;
try {
const url = new URL(props.attrs.href);
const searchParams = new URLSearchParams(url.search);
start = searchParams.get("t")?.replace(/s$/, "");
} catch {
// noop
}
return (
<Frame
{...props}
src={`https://www.youtube.com/embed/${videoId}?modestbranding=1${
queryParameters ? `&${queryParameters}` : ""
start ? `&start=${start}` : ""
}`}
title={`YouTube (${videoId})`}
/>
@@ -19,7 +27,7 @@ function YouTube(props: Props) {
}
YouTube.ENABLED = [
/(?:https?:\/\/)?(?:www\.)?youtu\.?be(?:\.com)?\/?.*(?:watch|embed)?(?:.*v=|v\/|\/)([a-zA-Z0-9_-]{11})(?:\?(.*))?$/i,
/(?:https?:\/\/)?(?:www\.)?youtu\.?be(?:\.com)?\/?.*(?:watch|embed)?(?:.*v=|v\/|\/)([a-zA-Z0-9_-]{11})([\&\?](.*))?$/i,
];
export default YouTube;