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

fix: youtube preview image #441

Merged
merged 1 commit into from
Dec 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const ViewBlock = ({ data, index, isEditMode = false }) => {
const videoID = data.url.match(/.be\//)
? data.url.match(/^.*\.be\/(.*)/)?.[1]
: data.url.match(/^.*\?v=(.*)$/)?.[1];
placeholder = 'https://img.youtube.com/vi/' + videoID + '/sddefault.jpg';
placeholder = 'https://img.youtube.com/vi/' + videoID + '/hqdefault.jpg';
} else if (data.url.match('vimeo')) {
const videoID = data.url.match(/^.*\.com\/(.*)/)[1];
placeholder = 'https://vumbnail.com/' + videoID + '.jpg';
Expand Down
2 changes: 1 addition & 1 deletion src/components/ItaliaTheme/View/Commons/EmbeddedVideo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const EmbeddedVideo = ({ video_url, title, id }) => {
defaultActive: false,
autoplay: false,
aspectRatio: '16:9',
placeholder: 'https://img.youtube.com/vi/' + video_id + '/sddefault.jpg',
placeholder: 'https://img.youtube.com/vi/' + video_id + '/hqdefault.jpg',
tabIndex: 0,
onKeyPress: onKeyDown,
ref: ref,
Expand Down
8 changes: 3 additions & 5 deletions src/helpers/videoUrlHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import { isInternalURL, flattenToAppURL } from '@plone/volto/helpers';
// test against all yt link formats I am aware of, not easy, can't find official docs.
// Try all possible formats, match an alphanumeric token in the expected position, length should be 10-20 chars, that's the ID.
// Reference sheet used and improved with support to shorts: https://gist.github.com/rodrigoborgesdeoliveira/987683cfbfcc8d800192da1e73adc486
const ytReg =
/^(?:http|https?:)?(?:\/\/)?(?:(?:www\.|m\.)?youtube(?:-nocookie)?\.com\/(?:(?:watch)?\?(?:feature=\w*&)?vi?=|embed\/|vi?\/|e\/|shorts\/)|youtu.be\/)([\w\-]{10,20})/i;
const ytReg = /^(?:http|https?:)?(?:\/\/)?(?:(?:www\.|m\.)?youtube(?:-nocookie)?\.com\/(?:(?:watch)?\?(?:feature=\w*&)?vi?=|embed\/|vi?\/|e\/|shorts\/)|youtu.be\/)([\w\-]{10,20})/i;

// Vimeo IDs should be only digits, let's try to get only a group of digits and suppose that's the video ID, scrap everything after.
// Docs link, watch for changes https://developer.vimeo.com/api/common-formats
const vimeoReg =
/(?:http|https)?:?\/?\/?(?:www\.)?(?:player\.)?vimeo\.com\/(?:(?:channels\/(?:[\w*\/]*\/))|(?:groups\/(?:[\w*\/]*\/))|(?:videos\/|video\/|albums\/))?(\d+)/i;
const vimeoReg = /(?:http|https)?:?\/?\/?(?:www\.)?(?:player\.)?vimeo\.com\/(?:(?:channels\/(?:[\w*\/]*\/))|(?:groups\/(?:[\w*\/]*\/))|(?:videos\/|video\/|albums\/))?(\d+)/i;

// Use regex against url to get video ID, video urls supported are youtube and vimeo.
// Return videoID and placeholder (if needed), null if no match found.
Expand All @@ -25,7 +23,7 @@ export const videoUrlHelper = (url, previewImage) => {
if (ytVideoMatch) {
videoID = ytVideoMatch[1];
if (!placeholder)
placeholder = 'https://img.youtube.com/vi/' + videoID + '/sddefault.jpg';
placeholder = 'https://img.youtube.com/vi/' + videoID + '/hqdefault.jpg';

return [videoID, placeholder];
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/videoUrlHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ describe('videoUrlHelper', () => {
);

expect(placeholder).toBe(
'https://img.youtube.com/vi/' + YT_TEST_ID + '/sddefault.jpg',
'https://img.youtube.com/vi/' + YT_TEST_ID + '/hqdefault.jpg',
);
});
it('generates vimeo placeholder correctly', async () => {
Expand Down