[site/images][s]: create function to fetch youtube id and thumbnail

This commit is contained in:
khalilcodes 2022-06-16 16:40:07 +03:00
parent 2915f4d961
commit 39d02329f4
1 changed files with 21 additions and 0 deletions

21
site/utils/getYoutube.js Normal file
View File

@ -0,0 +1,21 @@
import { YOUTUBE_REGEX, YOUTUBE_ID_REGEX } from "lib/constants";
export function getYoutube(youtube) {
let youtubeThumbnail;
const youtubeId =
youtube && YOUTUBE_REGEX.test(youtube) && youtube.match(YOUTUBE_ID_REGEX)[1];
if (youtubeId) {
// get the youtube thumbnail image from https://img.youtube.com/vi/<youtube-video-id>/maxresdefault.jpg
youtubeThumbnail = youtube.replace(
YOUTUBE_REGEX,
`https://img.youtube.com/vi/${youtubeId}/maxresdefault.jpg`
);
}
return {
id: youtubeId,
thumbnail: youtubeThumbnail
};
}