Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Youtube Shorts URLs in the front-end #803

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/pages/VideoLinkAddModal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ALLOWED_VIDEO_SOURCES_REGEX } from '@/pages/VideoLinkAddModal';

describe('ALLOWED_VIDEO_SOURCES_REGEX', () => {
const tests = {
'https://www.youtube.com/watch?v=12345678901': true,
'http://youtu.be/12345678901?si=6N2ks_X0YOE_0Pmg': true,
'http://youtudbe/12345678901?si=6N2ks_X0YOE_0Pmg': false,
'https://www.youtube.com/shorts/ViOS7SeT0HE': true,
'https://vimeo.com/789006133': true,
'http://www.vimeo.com/789006133': true,
'http://miveo.com/789006133': false,
};

test.each(Object.entries(tests))(
'can test whether video URL %p being valid is %p',
(url, expected) =>
expect(ALLOWED_VIDEO_SOURCES_REGEX.test(url)).toBe(expected),
);
});
12 changes: 9 additions & 3 deletions src/pages/VideoLinkAddModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ type Props = {
onClose: () => void;
};

const ALLOWED_VIDEO_SOURCES_REGEX: RegExp =
/^http(s?):\/\/(www\.)?((youtube\.com\/watch\?v=([^/#&?]*))|(vimeo\.com\/([^/#&?]*))|(youtu\.be\/([^/#&?]*)))/;
const ALLOWED_VIDEO_SOURCES_REGEX = new RegExp(
'^(https?://(www\\.)?' + // Protocol
'(youtube.com/watch\\?v=|' + // YouTube
'youtu\\.be/|' + // Shortened YouTube
'vimeo\\.com/|' + // Vimeo
'youtube\\.com/shorts/)' + // YouTube Shorts
'[^/#&?]+)', // Video ID
);

type FormData = {
link: string;
Expand Down Expand Up @@ -87,4 +93,4 @@ const VideoLinkAddModal = ({ visible, onConfirm, onClose }: Props) => {
);
};

export { VideoLinkAddModal };
export { ALLOWED_VIDEO_SOURCES_REGEX, VideoLinkAddModal };
Loading