@social-embed/lib
Advanced tools
Comparing version 0.0.1-next.9 to 0.0.1-next.10
@@ -1,32 +0,3 @@ | ||
export declare enum Provider { | ||
YouTube = "YouTube", | ||
Spotify = "Spotify", | ||
Vimeo = "Vimeo", | ||
DailyMotion = "DailyMotion" | ||
} | ||
export declare const getProviderFromUrl: (url: string) => Provider | undefined; | ||
export declare const dailyMotionUrlRegex: RegExp; | ||
export declare const getDailyMotionIdFromUrl: (url: string) => string; | ||
export declare const getDailyMotionEmbedFromId: (dailyMotionId: string) => string; | ||
export declare const vimeoUrlRegex: RegExp; | ||
export declare const getVimeoIdFromUrl: (url: string) => string; | ||
export declare const getVimeoEmbedUrlFromId: (vimeoId: string) => string; | ||
export declare const spotifyUrlRegex: RegExp; | ||
export declare const spotifySymbolRegex: RegExp; | ||
export declare const getSpotifyIdAndTypeFromUrl: (url: string) => [string, string]; | ||
export declare const youTubeUrlRegex = "^(?:(?:https?):)?(?://)?[^/]*(?:youtube(?:-nocookie)?.com|youtu.be).*[=/]([-\\w]{11})(?:\\?|=|&|$)"; | ||
export declare const getSpotifyEmbedUrlFromIdAndType: (spotifyId: string, spotifyType: string) => string; | ||
export declare const getYouTubeIdFromUrl: (url: string | undefined) => string; | ||
export declare const getYouTubeEmbedUrlFromId: (youtubeID: string | undefined) => string; | ||
export declare type ProviderKey = keyof typeof Provider; | ||
export declare type ProviderType = typeof Provider[Provider]; | ||
declare type ValueOfProvider = `${Provider}`; | ||
export declare const ProviderIdFunctionMap: { | ||
[P in ValueOfProvider]: (url: string) => string | string[]; | ||
}; | ||
export declare const ProviderIdUrlFunctionMap: { | ||
[P in ValueOfProvider]: (id: string, ...args: any) => string; | ||
}; | ||
export declare const convertUrlToEmbedUrl: (url: string) => string; | ||
export {}; | ||
export * from './providers'; | ||
export { Provider, ProviderKey, ProviderType, ProviderIdFunctionMap, ProviderIdUrlFunctionMap, getProviderFromUrl, convertUrlToEmbedUrl, } from './utils'; | ||
//# sourceMappingURL=index.d.ts.map |
'use strict'; | ||
/** | ||
* Regex matcher for YouTube URLs | ||
*/ | ||
var youTubeUrlRegex = '^(?:(?:https?):)?(?://)?[^/]*(?:youtube(?:-nocookie)?.com|youtu.be).*[=/]([-\\w]{11})(?:\\?|=|&|$)'; | ||
var getYouTubeIdFromUrl = function getYouTubeIdFromUrl(url) { | ||
if (url) { | ||
var _url$match$, _url$match; | ||
// credit: https://stackoverflow.com/a/42442074 | ||
return (_url$match$ = (_url$match = url.match(youTubeUrlRegex)) == null ? void 0 : _url$match[1]) != null ? _url$match$ : ''; | ||
} | ||
return ''; | ||
}; | ||
var getYouTubeEmbedUrlFromId = function getYouTubeEmbedUrlFromId(youtubeID) { | ||
return "https://www.youtube.com/embed/" + youtubeID; | ||
}; | ||
// Credit: https://stackoverflow.com/a/50644701, (2021-03-14: Support ?playlist) | ||
var dailyMotionUrlRegex = /^(?:(?:https?):)?(?:\/\/)?(?:www\.)?(?:(?:dailymotion\.com(?:\/embed)?\/video)|dai\.ly)\/([a-zA-Z0-9]+)(?:_[\w_-]+)?(?:\?playlist=[a-zA-Z0-9]+)?$/; | ||
var getDailyMotionIdFromUrl = function getDailyMotionIdFromUrl(url) { | ||
var _url$match$, _url$match; | ||
return (_url$match$ = (_url$match = url.match(dailyMotionUrlRegex)) == null ? void 0 : _url$match[1]) != null ? _url$match$ : ''; | ||
}; | ||
var getDailyMotionEmbedFromId = function getDailyMotionEmbedFromId(dailyMotionId) { | ||
return "https://www.dailymotion.com/embed/video/" + dailyMotionId; // ?autoplay=1 | ||
}; | ||
// regex: derived from https://gist.github.com/TrevorJTClarke/a14c37db3c11ee23a700 | ||
// Thank you @TrevorJTClarke | ||
var spotifyUrlRegex = /(?:(?:https?):)?(?:\/\/)?(?:embed\.|open\.)(?:spotify\.com\/)(?:(album|track|playlist)\/|\?uri=spotify:track:)((\w|-){22})/; | ||
var spotifySymbolRegex = /spotify:(?:(album|track|playlist):|\?uri=spotify:track:)((\w|-){22})/; | ||
var getSpotifyIdAndTypeFromUrl = function getSpotifyIdAndTypeFromUrl(url) { | ||
var _ref = url.match(spotifyUrlRegex) || url.match(spotifySymbolRegex) || [], | ||
spotifyType = _ref[1], | ||
spotifyId = _ref[2]; | ||
return [spotifyId, spotifyType]; | ||
}; | ||
var getSpotifyEmbedUrlFromIdAndType = function getSpotifyEmbedUrlFromIdAndType(spotifyId, spotifyType) { | ||
return "https://open.spotify.com/embed/" + spotifyType + "/" + spotifyId; | ||
}; | ||
// Credit: https://stackoverflow.com/a/50777192 (2021-03-14: modified / fixed to ignore unused groups) | ||
var vimeoUrlRegex = /(?:(?:https?):)?(?:\/\/)?(?:www\.|player\.)?vimeo\.com\/(?:channels\/(?:\w+\/)?|groups\/(?:[^/]*)\/videos\/|video\/|)(\d+)(?:|\/\?)/; | ||
var getVimeoIdFromUrl = function getVimeoIdFromUrl(url) { | ||
var _url$match$, _url$match; | ||
return (_url$match$ = (_url$match = url.match(vimeoUrlRegex)) == null ? void 0 : _url$match[1]) != null ? _url$match$ : ''; | ||
}; | ||
var getVimeoEmbedUrlFromId = function getVimeoEmbedUrlFromId(vimeoId) { | ||
return "https://player.vimeo.com/video/" + vimeoId; | ||
}; | ||
var _ProviderIdFunctionMa, _ProviderIdUrlFunctio; | ||
@@ -34,51 +89,3 @@ | ||
return undefined; | ||
}; // Credit: https://stackoverflow.com/a/50644701, (2021-03-14: Support ?playlist) | ||
var dailyMotionUrlRegex = /^(?:(?:https?):)?(?:\/\/)?(?:www\.)?(?:(?:dailymotion\.com(?:\/embed)?\/video)|dai\.ly)\/([a-zA-Z0-9]+)(?:_[\w_-]+)?(?:\?playlist=[a-zA-Z0-9]+)?$/; | ||
var getDailyMotionIdFromUrl = function getDailyMotionIdFromUrl(url) { | ||
var _url$match$, _url$match; | ||
return (_url$match$ = (_url$match = url.match(dailyMotionUrlRegex)) == null ? void 0 : _url$match[1]) != null ? _url$match$ : ''; | ||
}; | ||
var getDailyMotionEmbedFromId = function getDailyMotionEmbedFromId(dailyMotionId) { | ||
return "https://www.dailymotion.com/embed/video/" + dailyMotionId; // ?autoplay=1 | ||
}; // Credit: https://stackoverflow.com/a/50777192 (2021-03-14: modified / fixed to ignore unused groups) | ||
var vimeoUrlRegex = /(?:(?:https?):)?(?:\/\/)?(?:www\.|player\.)?vimeo\.com\/(?:channels\/(?:\w+\/)?|groups\/(?:[^/]*)\/videos\/|video\/|)(\d+)(?:|\/\?)/; | ||
var getVimeoIdFromUrl = function getVimeoIdFromUrl(url) { | ||
var _url$match$2, _url$match2; | ||
return (_url$match$2 = (_url$match2 = url.match(vimeoUrlRegex)) == null ? void 0 : _url$match2[1]) != null ? _url$match$2 : ''; | ||
}; | ||
var getVimeoEmbedUrlFromId = function getVimeoEmbedUrlFromId(vimeoId) { | ||
return "https://player.vimeo.com/video/" + vimeoId; | ||
}; // regex: derived from https://gist.github.com/TrevorJTClarke/a14c37db3c11ee23a700 | ||
// Thank you @TrevorJTClarke | ||
var spotifyUrlRegex = /(?:(?:https?):)?(?:\/\/)?(?:embed\.|open\.)(?:spotify\.com\/)(?:(album|track|playlist)\/|\?uri=spotify:track:)((\w|-){22})/; | ||
var spotifySymbolRegex = /spotify:(?:(album|track|playlist):|\?uri=spotify:track:)((\w|-){22})/; | ||
var getSpotifyIdAndTypeFromUrl = function getSpotifyIdAndTypeFromUrl(url) { | ||
var _ref = url.match(spotifyUrlRegex) || url.match(spotifySymbolRegex) || [], | ||
spotifyType = _ref[1], | ||
spotifyId = _ref[2]; | ||
return [spotifyId, spotifyType]; | ||
}; | ||
var youTubeUrlRegex = '^(?:(?:https?):)?(?://)?[^/]*(?:youtube(?:-nocookie)?.com|youtu.be).*[=/]([-\\w]{11})(?:\\?|=|&|$)'; | ||
var getSpotifyEmbedUrlFromIdAndType = function getSpotifyEmbedUrlFromIdAndType(spotifyId, spotifyType) { | ||
return "https://open.spotify.com/embed/" + spotifyType + "/" + spotifyId; | ||
}; | ||
var getYouTubeIdFromUrl = function getYouTubeIdFromUrl(url) { | ||
if (url) { | ||
var _url$match$3, _url$match3; | ||
// credit: https://stackoverflow.com/a/42442074 | ||
return (_url$match$3 = (_url$match3 = url.match(youTubeUrlRegex)) == null ? void 0 : _url$match3[1]) != null ? _url$match$3 : ''; | ||
} | ||
return ''; | ||
}; | ||
var getYouTubeEmbedUrlFromId = function getYouTubeEmbedUrlFromId(youtubeID) { | ||
return "https://www.youtube.com/embed/" + youtubeID; | ||
}; | ||
var ProviderIdFunctionMap = (_ProviderIdFunctionMa = {}, _ProviderIdFunctionMa[exports.Provider.DailyMotion] = getDailyMotionIdFromUrl, _ProviderIdFunctionMa[exports.Provider.Spotify] = getSpotifyIdAndTypeFromUrl, _ProviderIdFunctionMa[exports.Provider.Vimeo] = getVimeoIdFromUrl, _ProviderIdFunctionMa[exports.Provider.YouTube] = getYouTubeIdFromUrl, _ProviderIdFunctionMa); | ||
@@ -85,0 +92,0 @@ var ProviderIdUrlFunctionMap = (_ProviderIdUrlFunctio = {}, _ProviderIdUrlFunctio[exports.Provider.DailyMotion] = getDailyMotionEmbedFromId, _ProviderIdUrlFunctio[exports.Provider.Spotify] = getSpotifyEmbedUrlFromIdAndType, _ProviderIdUrlFunctio[exports.Provider.Vimeo] = getVimeoEmbedUrlFromId, _ProviderIdUrlFunctio[exports.Provider.YouTube] = getYouTubeEmbedUrlFromId, _ProviderIdUrlFunctio); |
@@ -1,2 +0,2 @@ | ||
"use strict";var o,r,e;(e=exports.Provider||(exports.Provider={})).YouTube="YouTube",e.Spotify="Spotify",e.Vimeo="Vimeo",e.DailyMotion="DailyMotion";var t=function(o){if(o)return o.match(/dailymotion/)?exports.Provider.DailyMotion:o.match(/spotify/)?exports.Provider.Spotify:o.match(/vimeo/)?exports.Provider.Vimeo:o.match(/youtu\.?be/)?exports.Provider.YouTube:void 0},i=/^(?:(?:https?):)?(?:\/\/)?(?:www\.)?(?:(?:dailymotion\.com(?:\/embed)?\/video)|dai\.ly)\/([a-zA-Z0-9]+)(?:_[\w_-]+)?(?:\?playlist=[a-zA-Z0-9]+)?$/,p=function(o){var r,e;return null!=(r=null==(e=o.match(i))?void 0:e[1])?r:""},n=function(o){return"https://www.dailymotion.com/embed/video/"+o},u=/(?:(?:https?):)?(?:\/\/)?(?:www\.|player\.)?vimeo\.com\/(?:channels\/(?:\w+\/)?|groups\/(?:[^/]*)\/videos\/|video\/|)(\d+)(?:|\/\?)/,s=function(o){var r,e;return null!=(r=null==(e=o.match(u))?void 0:e[1])?r:""},d=function(o){return"https://player.vimeo.com/video/"+o},m=/(?:(?:https?):)?(?:\/\/)?(?:embed\.|open\.)(?:spotify\.com\/)(?:(album|track|playlist)\/|\?uri=spotify:track:)((\w|-){22})/,l=/spotify:(?:(album|track|playlist):|\?uri=spotify:track:)((\w|-){22})/,a=function(o){var r=o.match(m)||o.match(l)||[];return[r[2],r[1]]},y="^(?:(?:https?):)?(?://)?[^/]*(?:youtube(?:-nocookie)?.com|youtu.be).*[=/]([-\\w]{11})(?:\\?|=|&|$)",v=function(o,r){return"https://open.spotify.com/embed/"+r+"/"+o},c=function(o){var r,e;return o&&null!=(r=null==(e=o.match(y))?void 0:e[1])?r:""},x=function(o){return"https://www.youtube.com/embed/"+o},f=((o={})[exports.Provider.DailyMotion]=p,o[exports.Provider.Spotify]=a,o[exports.Provider.Vimeo]=s,o[exports.Provider.YouTube]=c,o),b=((r={})[exports.Provider.DailyMotion]=n,r[exports.Provider.Spotify]=v,r[exports.Provider.Vimeo]=d,r[exports.Provider.YouTube]=x,r);exports.ProviderIdFunctionMap=f,exports.ProviderIdUrlFunctionMap=b,exports.convertUrlToEmbedUrl=function(o){var r=t(o);if(!r)return"";var e=b[r],i=(0,f[r])(o);if(Array.isArray(i)){var p=i.shift();return p?e.apply(void 0,[p].concat(i)):""}return e(i)},exports.dailyMotionUrlRegex=i,exports.getDailyMotionEmbedFromId=n,exports.getDailyMotionIdFromUrl=p,exports.getProviderFromUrl=t,exports.getSpotifyEmbedUrlFromIdAndType=v,exports.getSpotifyIdAndTypeFromUrl=a,exports.getVimeoEmbedUrlFromId=d,exports.getVimeoIdFromUrl=s,exports.getYouTubeEmbedUrlFromId=x,exports.getYouTubeIdFromUrl=c,exports.spotifySymbolRegex=l,exports.spotifyUrlRegex=m,exports.vimeoUrlRegex=u,exports.youTubeUrlRegex=y; | ||
"use strict";var o,r,e,t="^(?:(?:https?):)?(?://)?[^/]*(?:youtube(?:-nocookie)?.com|youtu.be).*[=/]([-\\w]{11})(?:\\?|=|&|$)",i=function(o){var r,e;return o&&null!=(r=null==(e=o.match(t))?void 0:e[1])?r:""},p=function(o){return"https://www.youtube.com/embed/"+o},n=/^(?:(?:https?):)?(?:\/\/)?(?:www\.)?(?:(?:dailymotion\.com(?:\/embed)?\/video)|dai\.ly)\/([a-zA-Z0-9]+)(?:_[\w_-]+)?(?:\?playlist=[a-zA-Z0-9]+)?$/,u=function(o){var r,e;return null!=(r=null==(e=o.match(n))?void 0:e[1])?r:""},s=function(o){return"https://www.dailymotion.com/embed/video/"+o},d=/(?:(?:https?):)?(?:\/\/)?(?:embed\.|open\.)(?:spotify\.com\/)(?:(album|track|playlist)\/|\?uri=spotify:track:)((\w|-){22})/,m=/spotify:(?:(album|track|playlist):|\?uri=spotify:track:)((\w|-){22})/,l=function(o){var r=o.match(d)||o.match(m)||[];return[r[2],r[1]]},a=function(o,r){return"https://open.spotify.com/embed/"+r+"/"+o},y=/(?:(?:https?):)?(?:\/\/)?(?:www\.|player\.)?vimeo\.com\/(?:channels\/(?:\w+\/)?|groups\/(?:[^/]*)\/videos\/|video\/|)(\d+)(?:|\/\?)/,v=function(o){var r,e;return null!=(r=null==(e=o.match(y))?void 0:e[1])?r:""},c=function(o){return"https://player.vimeo.com/video/"+o};(e=exports.Provider||(exports.Provider={})).YouTube="YouTube",e.Spotify="Spotify",e.Vimeo="Vimeo",e.DailyMotion="DailyMotion";var x=function(o){if(o)return o.match(/dailymotion/)?exports.Provider.DailyMotion:o.match(/spotify/)?exports.Provider.Spotify:o.match(/vimeo/)?exports.Provider.Vimeo:o.match(/youtu\.?be/)?exports.Provider.YouTube:void 0},f=((o={})[exports.Provider.DailyMotion]=u,o[exports.Provider.Spotify]=l,o[exports.Provider.Vimeo]=v,o[exports.Provider.YouTube]=i,o),b=((r={})[exports.Provider.DailyMotion]=s,r[exports.Provider.Spotify]=a,r[exports.Provider.Vimeo]=c,r[exports.Provider.YouTube]=p,r);exports.ProviderIdFunctionMap=f,exports.ProviderIdUrlFunctionMap=b,exports.convertUrlToEmbedUrl=function(o){var r=x(o);if(!r)return"";var e=b[r],t=(0,f[r])(o);if(Array.isArray(t)){var i=t.shift();return i?e.apply(void 0,[i].concat(t)):""}return e(t)},exports.dailyMotionUrlRegex=n,exports.getDailyMotionEmbedFromId=s,exports.getDailyMotionIdFromUrl=u,exports.getProviderFromUrl=x,exports.getSpotifyEmbedUrlFromIdAndType=a,exports.getSpotifyIdAndTypeFromUrl=l,exports.getVimeoEmbedUrlFromId=c,exports.getVimeoIdFromUrl=v,exports.getYouTubeEmbedUrlFromId=p,exports.getYouTubeIdFromUrl=i,exports.spotifySymbolRegex=m,exports.spotifyUrlRegex=d,exports.vimeoUrlRegex=y,exports.youTubeUrlRegex=t; | ||
//# sourceMappingURL=lib.cjs.production.min.js.map |
@@ -0,3 +1,57 @@ | ||
/** | ||
* Regex matcher for YouTube URLs | ||
*/ | ||
var youTubeUrlRegex = '^(?:(?:https?):)?(?://)?[^/]*(?:youtube(?:-nocookie)?.com|youtu.be).*[=/]([-\\w]{11})(?:\\?|=|&|$)'; | ||
var getYouTubeIdFromUrl = function getYouTubeIdFromUrl(url) { | ||
if (url) { | ||
var _url$match$, _url$match; | ||
// credit: https://stackoverflow.com/a/42442074 | ||
return (_url$match$ = (_url$match = url.match(youTubeUrlRegex)) == null ? void 0 : _url$match[1]) != null ? _url$match$ : ''; | ||
} | ||
return ''; | ||
}; | ||
var getYouTubeEmbedUrlFromId = function getYouTubeEmbedUrlFromId(youtubeID) { | ||
return "https://www.youtube.com/embed/" + youtubeID; | ||
}; | ||
// Credit: https://stackoverflow.com/a/50644701, (2021-03-14: Support ?playlist) | ||
var dailyMotionUrlRegex = /^(?:(?:https?):)?(?:\/\/)?(?:www\.)?(?:(?:dailymotion\.com(?:\/embed)?\/video)|dai\.ly)\/([a-zA-Z0-9]+)(?:_[\w_-]+)?(?:\?playlist=[a-zA-Z0-9]+)?$/; | ||
var getDailyMotionIdFromUrl = function getDailyMotionIdFromUrl(url) { | ||
var _url$match$, _url$match; | ||
return (_url$match$ = (_url$match = url.match(dailyMotionUrlRegex)) == null ? void 0 : _url$match[1]) != null ? _url$match$ : ''; | ||
}; | ||
var getDailyMotionEmbedFromId = function getDailyMotionEmbedFromId(dailyMotionId) { | ||
return "https://www.dailymotion.com/embed/video/" + dailyMotionId; // ?autoplay=1 | ||
}; | ||
// regex: derived from https://gist.github.com/TrevorJTClarke/a14c37db3c11ee23a700 | ||
// Thank you @TrevorJTClarke | ||
var spotifyUrlRegex = /(?:(?:https?):)?(?:\/\/)?(?:embed\.|open\.)(?:spotify\.com\/)(?:(album|track|playlist)\/|\?uri=spotify:track:)((\w|-){22})/; | ||
var spotifySymbolRegex = /spotify:(?:(album|track|playlist):|\?uri=spotify:track:)((\w|-){22})/; | ||
var getSpotifyIdAndTypeFromUrl = function getSpotifyIdAndTypeFromUrl(url) { | ||
var _ref = url.match(spotifyUrlRegex) || url.match(spotifySymbolRegex) || [], | ||
spotifyType = _ref[1], | ||
spotifyId = _ref[2]; | ||
return [spotifyId, spotifyType]; | ||
}; | ||
var getSpotifyEmbedUrlFromIdAndType = function getSpotifyEmbedUrlFromIdAndType(spotifyId, spotifyType) { | ||
return "https://open.spotify.com/embed/" + spotifyType + "/" + spotifyId; | ||
}; | ||
// Credit: https://stackoverflow.com/a/50777192 (2021-03-14: modified / fixed to ignore unused groups) | ||
var vimeoUrlRegex = /(?:(?:https?):)?(?:\/\/)?(?:www\.|player\.)?vimeo\.com\/(?:channels\/(?:\w+\/)?|groups\/(?:[^/]*)\/videos\/|video\/|)(\d+)(?:|\/\?)/; | ||
var getVimeoIdFromUrl = function getVimeoIdFromUrl(url) { | ||
var _url$match$, _url$match; | ||
return (_url$match$ = (_url$match = url.match(vimeoUrlRegex)) == null ? void 0 : _url$match[1]) != null ? _url$match$ : ''; | ||
}; | ||
var getVimeoEmbedUrlFromId = function getVimeoEmbedUrlFromId(vimeoId) { | ||
return "https://player.vimeo.com/video/" + vimeoId; | ||
}; | ||
var _ProviderIdFunctionMa, _ProviderIdUrlFunctio; | ||
var Provider; | ||
@@ -34,51 +88,3 @@ | ||
return undefined; | ||
}; // Credit: https://stackoverflow.com/a/50644701, (2021-03-14: Support ?playlist) | ||
var dailyMotionUrlRegex = /^(?:(?:https?):)?(?:\/\/)?(?:www\.)?(?:(?:dailymotion\.com(?:\/embed)?\/video)|dai\.ly)\/([a-zA-Z0-9]+)(?:_[\w_-]+)?(?:\?playlist=[a-zA-Z0-9]+)?$/; | ||
var getDailyMotionIdFromUrl = function getDailyMotionIdFromUrl(url) { | ||
var _url$match$, _url$match; | ||
return (_url$match$ = (_url$match = url.match(dailyMotionUrlRegex)) == null ? void 0 : _url$match[1]) != null ? _url$match$ : ''; | ||
}; | ||
var getDailyMotionEmbedFromId = function getDailyMotionEmbedFromId(dailyMotionId) { | ||
return "https://www.dailymotion.com/embed/video/" + dailyMotionId; // ?autoplay=1 | ||
}; // Credit: https://stackoverflow.com/a/50777192 (2021-03-14: modified / fixed to ignore unused groups) | ||
var vimeoUrlRegex = /(?:(?:https?):)?(?:\/\/)?(?:www\.|player\.)?vimeo\.com\/(?:channels\/(?:\w+\/)?|groups\/(?:[^/]*)\/videos\/|video\/|)(\d+)(?:|\/\?)/; | ||
var getVimeoIdFromUrl = function getVimeoIdFromUrl(url) { | ||
var _url$match$2, _url$match2; | ||
return (_url$match$2 = (_url$match2 = url.match(vimeoUrlRegex)) == null ? void 0 : _url$match2[1]) != null ? _url$match$2 : ''; | ||
}; | ||
var getVimeoEmbedUrlFromId = function getVimeoEmbedUrlFromId(vimeoId) { | ||
return "https://player.vimeo.com/video/" + vimeoId; | ||
}; // regex: derived from https://gist.github.com/TrevorJTClarke/a14c37db3c11ee23a700 | ||
// Thank you @TrevorJTClarke | ||
var spotifyUrlRegex = /(?:(?:https?):)?(?:\/\/)?(?:embed\.|open\.)(?:spotify\.com\/)(?:(album|track|playlist)\/|\?uri=spotify:track:)((\w|-){22})/; | ||
var spotifySymbolRegex = /spotify:(?:(album|track|playlist):|\?uri=spotify:track:)((\w|-){22})/; | ||
var getSpotifyIdAndTypeFromUrl = function getSpotifyIdAndTypeFromUrl(url) { | ||
var _ref = url.match(spotifyUrlRegex) || url.match(spotifySymbolRegex) || [], | ||
spotifyType = _ref[1], | ||
spotifyId = _ref[2]; | ||
return [spotifyId, spotifyType]; | ||
}; | ||
var youTubeUrlRegex = '^(?:(?:https?):)?(?://)?[^/]*(?:youtube(?:-nocookie)?.com|youtu.be).*[=/]([-\\w]{11})(?:\\?|=|&|$)'; | ||
var getSpotifyEmbedUrlFromIdAndType = function getSpotifyEmbedUrlFromIdAndType(spotifyId, spotifyType) { | ||
return "https://open.spotify.com/embed/" + spotifyType + "/" + spotifyId; | ||
}; | ||
var getYouTubeIdFromUrl = function getYouTubeIdFromUrl(url) { | ||
if (url) { | ||
var _url$match$3, _url$match3; | ||
// credit: https://stackoverflow.com/a/42442074 | ||
return (_url$match$3 = (_url$match3 = url.match(youTubeUrlRegex)) == null ? void 0 : _url$match3[1]) != null ? _url$match$3 : ''; | ||
} | ||
return ''; | ||
}; | ||
var getYouTubeEmbedUrlFromId = function getYouTubeEmbedUrlFromId(youtubeID) { | ||
return "https://www.youtube.com/embed/" + youtubeID; | ||
}; | ||
var ProviderIdFunctionMap = (_ProviderIdFunctionMa = {}, _ProviderIdFunctionMa[Provider.DailyMotion] = getDailyMotionIdFromUrl, _ProviderIdFunctionMa[Provider.Spotify] = getSpotifyIdAndTypeFromUrl, _ProviderIdFunctionMa[Provider.Vimeo] = getVimeoIdFromUrl, _ProviderIdFunctionMa[Provider.YouTube] = getYouTubeIdFromUrl, _ProviderIdFunctionMa); | ||
@@ -85,0 +91,0 @@ var ProviderIdUrlFunctionMap = (_ProviderIdUrlFunctio = {}, _ProviderIdUrlFunctio[Provider.DailyMotion] = getDailyMotionEmbedFromId, _ProviderIdUrlFunctio[Provider.Spotify] = getSpotifyEmbedUrlFromIdAndType, _ProviderIdUrlFunctio[Provider.Vimeo] = getVimeoEmbedUrlFromId, _ProviderIdUrlFunctio[Provider.YouTube] = getYouTubeEmbedUrlFromId, _ProviderIdUrlFunctio); |
{ | ||
"name": "@social-embed/lib", | ||
"description": "Regexes, ID extraction, for embed providers (YouTube, other OEmbed compatible providers),", | ||
"version": "0.0.1-next.9", | ||
"version": "0.0.1-next.10", | ||
"main": "dist/index.js", | ||
@@ -6,0 +6,0 @@ "module": "dist/lib.esm.js", |
@@ -33,3 +33,3 @@ # [`@social-embed/lib`](https://social-embed.git-pull.com/) · [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/social-embed/social-embed/blob/master/LICENSE) [![npm version](https://img.shields.io/npm/v/@social-embed/lib.svg?style=flat)](https://www.npmjs.com/package/@social-embed/lib) | ||
[https://oembed-components.git-pull.com/api/](https://oembed-components.git-pull.com/api/) | ||
[https://social-embed.git-pull.com/api/](https://social-embed.git-pull.com/api/) | ||
@@ -36,0 +36,0 @@ ## Try |
129
src/index.ts
@@ -1,119 +0,10 @@ | ||
export enum Provider { | ||
YouTube = 'YouTube', | ||
Spotify = 'Spotify', | ||
Vimeo = 'Vimeo', | ||
DailyMotion = 'DailyMotion', | ||
} | ||
export const getProviderFromUrl = (url: string): Provider | undefined => { | ||
if (!url) { | ||
return undefined; | ||
} | ||
if (url.match(/dailymotion/)) { | ||
return Provider.DailyMotion; | ||
} | ||
if (url.match(/spotify/)) { | ||
return Provider.Spotify; | ||
} | ||
if (url.match(/vimeo/)) { | ||
return Provider.Vimeo; | ||
} | ||
if (url.match(/youtu\.?be/)) { | ||
return Provider.YouTube; | ||
} | ||
return undefined; | ||
}; | ||
// Credit: https://stackoverflow.com/a/50644701, (2021-03-14: Support ?playlist) | ||
export const dailyMotionUrlRegex = /^(?:(?:https?):)?(?:\/\/)?(?:www\.)?(?:(?:dailymotion\.com(?:\/embed)?\/video)|dai\.ly)\/([a-zA-Z0-9]+)(?:_[\w_-]+)?(?:\?playlist=[a-zA-Z0-9]+)?$/; | ||
export const getDailyMotionIdFromUrl = (url: string): string => { | ||
return url.match(dailyMotionUrlRegex)?.[1] ?? ''; | ||
}; | ||
export const getDailyMotionEmbedFromId = (dailyMotionId: string): string => { | ||
return `https://www.dailymotion.com/embed/video/${dailyMotionId}`; // ?autoplay=1 | ||
}; | ||
// Credit: https://stackoverflow.com/a/50777192 (2021-03-14: modified / fixed to ignore unused groups) | ||
export const vimeoUrlRegex = /(?:(?:https?):)?(?:\/\/)?(?:www\.|player\.)?vimeo\.com\/(?:channels\/(?:\w+\/)?|groups\/(?:[^/]*)\/videos\/|video\/|)(\d+)(?:|\/\?)/; | ||
export const getVimeoIdFromUrl = (url: string): string => | ||
url.match(vimeoUrlRegex)?.[1] ?? ''; | ||
export const getVimeoEmbedUrlFromId = (vimeoId: string): string => | ||
`https://player.vimeo.com/video/${vimeoId}`; | ||
// regex: derived from https://gist.github.com/TrevorJTClarke/a14c37db3c11ee23a700 | ||
// Thank you @TrevorJTClarke | ||
export const spotifyUrlRegex = /(?:(?:https?):)?(?:\/\/)?(?:embed\.|open\.)(?:spotify\.com\/)(?:(album|track|playlist)\/|\?uri=spotify:track:)((\w|-){22})/; | ||
export const spotifySymbolRegex = /spotify:(?:(album|track|playlist):|\?uri=spotify:track:)((\w|-){22})/; | ||
export const getSpotifyIdAndTypeFromUrl = (url: string): [string, string] => { | ||
const [, spotifyType, spotifyId] = | ||
url.match(spotifyUrlRegex) || url.match(spotifySymbolRegex) || []; | ||
return [spotifyId, spotifyType]; | ||
}; | ||
export const youTubeUrlRegex = | ||
'^(?:(?:https?):)?(?://)?[^/]*(?:youtube(?:-nocookie)?.com|youtu.be).*[=/]([-\\w]{11})(?:\\?|=|&|$)'; | ||
export const getSpotifyEmbedUrlFromIdAndType = ( | ||
spotifyId: string, | ||
spotifyType: string | ||
): string => `https://open.spotify.com/embed/${spotifyType}/${spotifyId}`; | ||
export const getYouTubeIdFromUrl = (url: string | undefined): string => { | ||
if (url) { | ||
// credit: https://stackoverflow.com/a/42442074 | ||
return url.match(youTubeUrlRegex)?.[1] ?? ''; | ||
} | ||
return ''; | ||
}; | ||
export const getYouTubeEmbedUrlFromId = ( | ||
youtubeID: string | undefined | ||
): string => { | ||
return `https://www.youtube.com/embed/${youtubeID}`; | ||
}; | ||
export type ProviderKey = keyof typeof Provider; | ||
export type ProviderType = typeof Provider[Provider]; | ||
type ValueOfProvider = `${Provider}`; | ||
export const ProviderIdFunctionMap: { | ||
[P in ValueOfProvider]: (url: string) => string | string[]; | ||
} = { | ||
[Provider.DailyMotion]: getDailyMotionIdFromUrl, | ||
[Provider.Spotify]: getSpotifyIdAndTypeFromUrl, | ||
[Provider.Vimeo]: getVimeoIdFromUrl, | ||
[Provider.YouTube]: getYouTubeIdFromUrl, | ||
}; | ||
export const ProviderIdUrlFunctionMap: { | ||
[P in ValueOfProvider]: (id: string, ...args: any) => string; | ||
} = { | ||
[Provider.DailyMotion]: getDailyMotionEmbedFromId, | ||
[Provider.Spotify]: getSpotifyEmbedUrlFromIdAndType, | ||
[Provider.Vimeo]: getVimeoEmbedUrlFromId, | ||
[Provider.YouTube]: getYouTubeEmbedUrlFromId, | ||
}; | ||
export const convertUrlToEmbedUrl = (url: string): string => { | ||
const provider = getProviderFromUrl(url); | ||
if (!provider) return ''; | ||
const getId = ProviderIdFunctionMap[provider]; | ||
const getEmbedUrlFromId = ProviderIdUrlFunctionMap[provider]; | ||
const id = getId(url); | ||
if (Array.isArray(id)) { | ||
const _id = id.shift(); | ||
if (!_id) { | ||
return ''; | ||
} | ||
return getEmbedUrlFromId(_id, ...id); | ||
} | ||
return getEmbedUrlFromId(id); | ||
}; | ||
export * from './providers'; | ||
export { | ||
Provider, | ||
ProviderKey, | ||
ProviderType, | ||
ProviderIdFunctionMap, | ||
ProviderIdUrlFunctionMap, | ||
getProviderFromUrl, | ||
convertUrlToEmbedUrl, | ||
} from './utils'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
47910
31
403
1