From 9a7ecd7403e9388e497a1cc5ff0fda19c677a662 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 5 Apr 2023 12:48:48 -0400 Subject: [PATCH] fix: Passing of start parameter to YouTube embed --- shared/editor/embeds/YouTube.test.ts | 6 ++++++ shared/editor/embeds/YouTube.tsx | 14 +++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/shared/editor/embeds/YouTube.test.ts b/shared/editor/embeds/YouTube.test.ts index 60aebb2f5..4fd0e1c3a 100644 --- a/shared/editor/embeds/YouTube.test.ts +++ b/shared/editor/embeds/YouTube.test.ts @@ -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) diff --git a/shared/editor/embeds/YouTube.tsx b/shared/editor/embeds/YouTube.tsx index d64bd1b25..32d5bd841 100644 --- a/shared/editor/embeds/YouTube.tsx +++ b/shared/editor/embeds/YouTube.tsx @@ -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 ( @@ -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;