-
-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: handle www and non-www for providers (#245)
- Loading branch information
Showing
4 changed files
with
75 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict' | ||
|
||
const { memoizeOne } = require('@metascraper/helpers') | ||
const { forEach, get } = require('lodash') | ||
const pReflect = require('p-reflect') | ||
const got = require('got') | ||
|
||
const jsonOembed = memoizeOne($ => | ||
$('link[type="application/json+oembed"]').attr('href') | ||
) | ||
|
||
const fromHTML = async ({ url, meta, htmlDom, ...opts }) => { | ||
const oembedUrl = jsonOembed(htmlDom) | ||
if (!oembedUrl) return null | ||
const oembedUrlObj = new URL(oembedUrl) | ||
forEach(opts, (value, key) => oembedUrlObj.searchParams.append(key, value)) | ||
const { value } = await pReflect(got(oembedUrlObj.toString(), { json: true })) | ||
return get(value, 'body.html', null) | ||
} | ||
|
||
fromHTML.test = $ => !!jsonOembed($) | ||
|
||
module.exports = fromHTML |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict' | ||
|
||
const { extract, hasProvider } = require('oembed-parser') | ||
const { memoizeOne } = require('@metascraper/helpers') | ||
const pReflect = require('p-reflect') | ||
const { get } = require('lodash') | ||
|
||
const fromProvider = async ({ url, meta, htmlDom, ...opts }) => { | ||
const { value } = await pReflect(extract(findProviderUrl(url), opts)) | ||
return get(value, 'html', null) | ||
} | ||
|
||
const findProviderUrl = memoizeOne(url => { | ||
let providerUrl | ||
|
||
// build up a list of URL variations to test against because the oembed | ||
// providers list is not always up to date with scheme or www vs non-www | ||
const baseUrl = url.replace(/^\/\/|^https?:\/\/(?:www\.)?/, '') | ||
const testUrls = [ | ||
`http://${baseUrl}`, | ||
`https://${baseUrl}`, | ||
`http://www.${baseUrl}`, | ||
`https://www.${baseUrl}` | ||
] | ||
|
||
for (const testUrl of testUrls) { | ||
if (hasProvider(testUrl)) { | ||
providerUrl = testUrl | ||
break | ||
} | ||
} | ||
|
||
return providerUrl | ||
}) | ||
|
||
fromProvider.test = url => !!findProviderUrl(url) | ||
|
||
module.exports = fromProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,21 @@ | ||
'use strict' | ||
|
||
const { extract, hasProvider } = require('oembed-parser') | ||
const { memoizeOne } = require('@metascraper/helpers') | ||
const { forEach, get } = require('lodash') | ||
const pReflect = require('p-reflect') | ||
const got = require('got') | ||
|
||
const jsonOembed = memoizeOne($ => { | ||
const el = $('link[type="application/json+oembed"]') | ||
return el.attr('href') | ||
}) | ||
const fromProvider = require('./from-provider') | ||
const fromHTML = require('./from-html') | ||
|
||
const fromProvider = async ({ url, meta, htmlDom, ...opts }) => { | ||
const { value } = await pReflect(extract(url, opts)) | ||
return get(value, 'html', null) | ||
} | ||
|
||
const fromHTML = async ({ url, meta, htmlDom, ...opts }) => { | ||
const oembedUrl = jsonOembed(htmlDom) | ||
if (!oembedUrl) return null | ||
|
||
const oembedUrlObj = new URL(oembedUrl) | ||
forEach(opts, (value, key) => oembedUrlObj.searchParams.append(key, value)) | ||
|
||
const { value } = await pReflect(got(oembedUrlObj.toString(), { json: true })) | ||
return get(value, 'body.html', null) | ||
} | ||
const htmlTest = fromHTML.test.bind(fromHTML) | ||
const providerTest = fromProvider.test.bind(fromProvider) | ||
|
||
const isValidUrl = memoizeOne( | ||
({ url, htmlDom: $ }) => !!jsonOembed($) || hasProvider(url) | ||
const test = memoizeOne( | ||
({ url, htmlDom: $ }) => htmlTest($) || providerTest(url) | ||
) | ||
|
||
module.exports = () => { | ||
const rules = { iframe: [fromHTML, fromProvider] } | ||
rules.test = isValidUrl | ||
rules.test = test | ||
return rules | ||
} | ||
|
||
module.exports.isValidUrl = isValidUrl | ||
module.exports.test = test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters