From 39d02329f49a75cca9d1caea2f2772a9f5eba016 Mon Sep 17 00:00:00 2001 From: khalilcodes Date: Thu, 16 Jun 2022 16:40:07 +0300 Subject: [PATCH] [site/images][s]: create function to fetch youtube id and thumbnail --- site/utils/getYoutube.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 site/utils/getYoutube.js diff --git a/site/utils/getYoutube.js b/site/utils/getYoutube.js new file mode 100644 index 0000000..4eef886 --- /dev/null +++ b/site/utils/getYoutube.js @@ -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//maxresdefault.jpg + youtubeThumbnail = youtube.replace( + YOUTUBE_REGEX, + `https://img.youtube.com/vi/${youtubeId}/maxresdefault.jpg` + ); + } + + return { + id: youtubeId, + thumbnail: youtubeThumbnail + }; +}