Skip to content

Commit

Permalink
Optimize & Add youtube embed links support (#456)
Browse files Browse the repository at this point in the history
* Optimize & Add youtube embed links support

* Fix error in the regex matching based on feedback

* Modify the regex to keep the matching clean
  • Loading branch information
reda777 authored May 21, 2024
1 parent 05d6ab8 commit df5fbf3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
9 changes: 2 additions & 7 deletions assets/chat/js/hashlinkconverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]+)\/?$/;
Expand Down Expand Up @@ -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]}`;
}
Expand Down
5 changes: 5 additions & 0 deletions assets/chat/js/hashlinkconverter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit df5fbf3

Please sign in to comment.