UNPKG

@subsocial/utils

Version:
86 lines (85 loc) 2.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLinkBrand = exports.isSocialBrandLink = exports.getEmbedUrl = exports.allowEmbedList = exports.extractVideoId = void 0; /** Extract video id of link given. Current supported link format: youtube & youtu.be */ function extractVideoId(url) { const regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/; const match = url.match(regExp); return match && match[7].length == 11 ? match[7] : false; } exports.extractVideoId = extractVideoId; exports.allowEmbedList = ['vimeo', 'youtube', 'youtu.be', 'soundcloud']; /** Generate embed url for video url given with the given embed format */ const getEmbedUrl = (url, embed) => { if (!embed) return; const urls = { vimeo: `https://player.vimeo.com/video/${url.split('/').pop()}`, youtube: `https://www.youtube.com/embed/${url.split('=').pop()}`, 'youtu.be': `https://www.youtube.com/embed/${url.split('/').pop()}`, soundcloud: `https://w.soundcloud.com/player/ ?url=${url}&auto_play=false&hide_related=true&show_comments=true&show_user=true&show_reposts=false&visual=true`, }; return urls[embed]; }; exports.getEmbedUrl = getEmbedUrl; const linkPrefix = '^(https?:\/\/)?([a-z0-9-]+\.)?'; const newSocialLinkRegExp = (brandDomain) => { return new RegExp(linkPrefix + brandDomain); }; const socialLinksRegExp = { facebook: [ newSocialLinkRegExp('facebook.com'), newSocialLinkRegExp('fb.me'), newSocialLinkRegExp('fb.com'), newSocialLinkRegExp('facebook.me') ], twitter: [ newSocialLinkRegExp('twitter.com') ], medium: [ newSocialLinkRegExp('medium.com') ], linkedIn: [ newSocialLinkRegExp('linkedin.com'), newSocialLinkRegExp('linked.in') ], gitHub: [ newSocialLinkRegExp('github.com') ], instagram: [ newSocialLinkRegExp('instagram.com'), newSocialLinkRegExp('instagr.am') ], youTube: [ newSocialLinkRegExp('youtube.com'), newSocialLinkRegExp('youtu.be') ], telegram: [ newSocialLinkRegExp('t.me'), newSocialLinkRegExp('telegram.me') ], reddit: [ newSocialLinkRegExp('reddit.com'), ] }; /** Check if the link is from the specific social brand given */ const isSocialBrandLink = (brand, link) => { if (!link) { return false; } link = link.trim().toLowerCase(); return !!socialLinksRegExp[brand].find(r => r.test(link)); }; exports.isSocialBrandLink = isSocialBrandLink; /** Get the social platform where the link is originated */ const getLinkBrand = (link) => { for (const key in socialLinksRegExp) { const brand = key; if ((0, exports.isSocialBrandLink)(brand, link)) { return brand; } } return 'website'; }; exports.getLinkBrand = getLinkBrand;