diff options
author | Aryan Goharzad <arrygoo@gmail.com> | 2023-01-19 13:53:11 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-19 12:53:11 -0600 |
commit | f10a8308d9f6bfb907c8a2458cbf78b4cfad88d2 (patch) | |
tree | 0cb50ba6736ea67773e76f9000d07095a654bb6d /src/lib/extractYoutubeMeta.ts | |
parent | 9230d52ff596056429a773298b2728619afe3432 (diff) | |
download | voidsky-f10a8308d9f6bfb907c8a2458cbf78b4cfad88d2.tar.zst |
Fixes youtube embed issues (#50)
* fixes youtube embed * move extractMetaHtml test to its own file * tests cleanup * Add fallback for youtube meta data * lint * Check for youtube in the url domain * use hostname instead of full url to check for link domain * checks only for domain
Diffstat (limited to 'src/lib/extractYoutubeMeta.ts')
-rw-r--r-- | src/lib/extractYoutubeMeta.ts | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/lib/extractYoutubeMeta.ts b/src/lib/extractYoutubeMeta.ts new file mode 100644 index 000000000..566e3be46 --- /dev/null +++ b/src/lib/extractYoutubeMeta.ts @@ -0,0 +1,26 @@ +export const extractYoutubeMeta = (html: string): Record<string, string> => { + const res: Record<string, string> = {} + const youtubeTitleRegex = /"videoDetails":.*"title":"([^"]*)"/i + const youtubeDescriptionRegex = + /"videoDetails":.*"shortDescription":"([^"]*)"/i + const youtubeThumbnailRegex = /"videoDetails":.*"url":"(.*)(default\.jpg)/i + + const youtubeTitleMatch = youtubeTitleRegex.exec(html) + const youtubeDescriptionMatch = youtubeDescriptionRegex.exec(html) + const youtubeThumbnailMatch = youtubeThumbnailRegex.exec(html) + + if (youtubeTitleMatch && youtubeTitleMatch.length >= 1) { + res.title = decodeURI(youtubeTitleMatch[1]) + } + if (youtubeDescriptionMatch && youtubeDescriptionMatch.length >= 1) { + res.description = decodeURI(youtubeDescriptionMatch[1]).replace( + /\\n/g, + '\n', + ) + } + if (youtubeThumbnailMatch && youtubeThumbnailMatch.length >= 2) { + res.image = youtubeThumbnailMatch[1] + 'default.jpg' + } + + return res +} |