From d50504706f8178e33f553c48e68e78ca90146275 Mon Sep 17 00:00:00 2001 From: reda777 <60011372+reda777@users.noreply.github.com> Date: Fri, 19 Apr 2024 13:32:28 +0100 Subject: [PATCH 1/3] Optimize & Add youtube embed links support --- assets/chat/js/hashlinkconverter.js | 9 ++------- assets/chat/js/hashlinkconverter.test.js | 5 +++++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/assets/chat/js/hashlinkconverter.js b/assets/chat/js/hashlinkconverter.js index 65a14b84..d597257c 100644 --- a/assets/chat/js/hashlinkconverter.js +++ b/assets/chat/js/hashlinkconverter.js @@ -7,8 +7,7 @@ const MISSING_VIDEO_ID_ERROR = 'Invalid Youtube link - Missing video id'; class HashLinkConverter { constructor() { this.hasHttp = /^http[s]?:\/{0,2}/; - this.youtubeLiveRegex = /^live\/([A-Za-z0-9-_]{11})$/; - this.youtubeShortsRegex = /^shorts\/([A-Za-z0-9-_]{11})$/; + this.youtubeRegex = /^(shorts|live|embed)\/([A-Za-z0-9-_]{11})$/; this.twitchClipRegex = /^[^/]+\/clip\/([A-Za-z0-9-_]*)$/; this.twitchVODRegex = /^videos\/(\d+)$/; this.rumbleEmbedRegex = /^embed\/([a-z0-9]+)\/?$/; @@ -42,11 +41,7 @@ class HashLinkConverter { return `#twitch-clip/${pathname}`; case 'www.youtube.com': case 'youtube.com': - match = pathname.match(this.youtubeLiveRegex); - if (match) { - return `#youtube/${match[1]}`; - } - match = pathname.match(this.youtubeShortsRegex); + match = pathname.match(this.youtubeRegex); if (match) { return `#youtube/${match[1]}`; } diff --git a/assets/chat/js/hashlinkconverter.test.js b/assets/chat/js/hashlinkconverter.test.js index a3776e6b..1d5c9c94 100644 --- a/assets/chat/js/hashlinkconverter.test.js +++ b/assets/chat/js/hashlinkconverter.test.js @@ -59,6 +59,11 @@ describe('Valid embeds', () => { 'https://youtube.com/shorts/Bg1JpTqc0iA?si=7pzY1RnY2fEe9A8_', '#youtube/Bg1JpTqc0iA', ], + [ + 'Youtube embed link', + 'https://www.youtube.com/embed/Akala8bkIu8', + '#youtube/Akala8bkIu8', + ], [ 'Rumble embed', 'https://rumble.com/embed/v26pcdc/?pub=4', From 3e85e1ed5509fa7017c3d889e274113b50186e5d Mon Sep 17 00:00:00 2001 From: reda777 <60011372+reda777@users.noreply.github.com> Date: Tue, 21 May 2024 10:59:33 +0100 Subject: [PATCH 2/3] Fix error in the regex matching based on feedback --- assets/chat/js/hashlinkconverter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/chat/js/hashlinkconverter.js b/assets/chat/js/hashlinkconverter.js index d597257c..1e316845 100644 --- a/assets/chat/js/hashlinkconverter.js +++ b/assets/chat/js/hashlinkconverter.js @@ -43,7 +43,7 @@ class HashLinkConverter { case 'youtube.com': match = pathname.match(this.youtubeRegex); if (match) { - return `#youtube/${match[1]}`; + return `#youtube/${match[2]}`; } videoId = url.searchParams.get('v'); timestamp = url.searchParams.get('t'); From b671cfa5c84a50160a029e1dc7ddb3a303dbebd1 Mon Sep 17 00:00:00 2001 From: reda777 <60011372+reda777@users.noreply.github.com> Date: Tue, 21 May 2024 19:38:07 +0100 Subject: [PATCH 3/3] Modify the regex to keep the matching clean --- assets/chat/js/hashlinkconverter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/chat/js/hashlinkconverter.js b/assets/chat/js/hashlinkconverter.js index 1e316845..ab0f0bc7 100644 --- a/assets/chat/js/hashlinkconverter.js +++ b/assets/chat/js/hashlinkconverter.js @@ -7,7 +7,7 @@ const MISSING_VIDEO_ID_ERROR = 'Invalid Youtube link - Missing video id'; class HashLinkConverter { constructor() { this.hasHttp = /^http[s]?:\/{0,2}/; - this.youtubeRegex = /^(shorts|live|embed)\/([A-Za-z0-9-_]{11})$/; + this.youtubeRegex = /^(?:shorts|live|embed)\/([A-Za-z0-9-_]{11})$/; this.twitchClipRegex = /^[^/]+\/clip\/([A-Za-z0-9-_]*)$/; this.twitchVODRegex = /^videos\/(\d+)$/; this.rumbleEmbedRegex = /^embed\/([a-z0-9]+)\/?$/; @@ -43,7 +43,7 @@ class HashLinkConverter { case 'youtube.com': match = pathname.match(this.youtubeRegex); if (match) { - return `#youtube/${match[2]}`; + return `#youtube/${match[1]}`; } videoId = url.searchParams.get('v'); timestamp = url.searchParams.get('t');