oembed-parser
Advanced tools
Comparing version
{ | ||
"version": "1.3.1", | ||
"version": "1.3.2", | ||
"name": "oembed-parser", | ||
@@ -30,3 +30,3 @@ "description": "Get oEmbed data from given URL.", | ||
"debug": "^4.1.1", | ||
"tap": "^14.5.0" | ||
"tap": "^14.6.1" | ||
}, | ||
@@ -33,0 +33,0 @@ "keywords": [ |
@@ -53,3 +53,13 @@ # oembed-parser | ||
#### .setProviderList(Array of provider definitions) | ||
Sets the list of providers to use, overriding the defaults. | ||
This can be useful for whitelisting only certain providers, or for adding | ||
custom providers. | ||
For the expected format, see the | ||
[default list](https://raw.githubusercontent.com/ndaidong/oembed-parser/master/src/utils/providers.json). | ||
#### Provider list | ||
@@ -56,0 +66,0 @@ |
@@ -7,4 +7,7 @@ // main | ||
fetchEmbed, | ||
providersFromList, | ||
} = require('./utils'); | ||
const defaultProviderList = require('./utils/providers.json'); | ||
let providers = providersFromList(defaultProviderList); | ||
@@ -15,3 +18,3 @@ const extract = async (url, params) => { | ||
} | ||
const p = findProvider(url); | ||
const p = findProvider(url, providers); | ||
if (!p) { | ||
@@ -25,8 +28,13 @@ throw new Error(`No provider found with given url "${url}"`); | ||
const hasProvider = (url) => { | ||
return findProvider(url) !== null; | ||
return findProvider(url, providers) !== null; | ||
}; | ||
const setProviderList = (list) => { | ||
providers = providersFromList(list); | ||
}; | ||
module.exports = { | ||
extract, | ||
hasProvider, | ||
setProviderList, | ||
}; |
// utils -> findProvider | ||
const providerList = require('./providers.json'); | ||
const getHostname = (url) => { | ||
const match = url.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/i); | ||
if (match && match.length > 2 && typeof match[2] === 'string' && match[2].length > 0) { | ||
return match[2]; | ||
} | ||
return null; | ||
}; | ||
const providers = providerList.map((item) => { | ||
const { | ||
provider_name, // eslint-disable-line camelcase | ||
provider_url, // eslint-disable-line camelcase | ||
endpoints, | ||
} = item; | ||
const endpoint = endpoints[0]; | ||
const { | ||
schemes = [], | ||
url, | ||
} = endpoint; | ||
const hostname = getHostname(url); | ||
const domain = hostname ? hostname.replace('www.', '') : ''; | ||
return { | ||
provider_name, // eslint-disable-line camelcase | ||
provider_url, // eslint-disable-line camelcase | ||
schemes, | ||
domain, | ||
url, | ||
}; | ||
}).filter((item) => { | ||
return item.domain !== ''; | ||
}); | ||
const findProvider = (url) => { | ||
const findProvider = (url, providers) => { | ||
const candidates = providers.filter((provider) => { | ||
@@ -43,0 +5,0 @@ const { |
@@ -6,2 +6,3 @@ module.exports = { | ||
logger: require('./logger'), | ||
providersFromList: require('./providersFromList'), | ||
}; |
@@ -20,2 +20,3 @@ /** | ||
hasProvider, | ||
setProviderList, | ||
} = AP; | ||
@@ -243,1 +244,24 @@ | ||
}); | ||
test(`Testing .setProviderList() method`, (t) => { | ||
const customProviderOnly = [ | ||
{ | ||
provider_name: 'Example', // eslint-disable-line camelcase | ||
provider_url: 'http://www.example.org', // eslint-disable-line camelcase | ||
endpoints: [ | ||
{ | ||
schemes: [ | ||
'http://www.example.org/media/*', | ||
], | ||
url: 'http://www.example.org/oembed', | ||
}, | ||
], | ||
}, | ||
]; | ||
t.ok(isFunction(setProviderList), 'setProviderList must be a function'); | ||
setProviderList(customProviderOnly); | ||
t.equals(hasProvider('https://www.youtube.com/watch?v=zh9NgGf3cxU'), false, 'YouTube URL has no oEmbed provider'); | ||
t.equals(hasProvider('http://www.example.org/media/abcdef'), true, 'www.example.org URL has oEmbed provider'); | ||
t.end(); | ||
}); |
106747
1.48%20
5.26%3425
0.97%81
14.08%