fix(openGraph): URL api error handling

This commit is contained in:
curben 2019-09-04 02:31:06 +01:00
parent 1d5966e191
commit 07ab8c08d7
No known key found for this signature in database
GPG Key ID: 5D9DB57A25D34EE3
1 changed files with 11 additions and 14 deletions

View File

@ -4,9 +4,9 @@
/* /*
* Modified from the hexo version, * Modified from the hexo version,
* https://github.com/hexojs/hexo/blob/master/lib/plugins/helper/open_graph.js * https://github.com/hexojs/hexo/blob/master/lib/plugins/helper/open_graph.js
* for compatibility with cloudinary.js * to include https://github.com/hexojs/hexo/pull/3674
* the <meta name="og:image"> now use * and use WHATWG URL API
* <a> where href value links to an image * https://nodejs.org/api/url.html#url_the_whatwg_url_api
*/ */
'use strict' 'use strict'
@ -67,16 +67,12 @@ function openGraphHelper (options = {}) {
} }
if (!images.length && content && content.includes('<img')) { if (!images.length && content && content.includes('<img')) {
images = images.slice() images = images.slice();
// https://github.com/hexojs/hexo/pull/3680
let img let img
const imgPattern = /<a [^>]*href=['"]([^'"]+)([^>]*>)/gi const imgPattern = /<img [^>]*src=['"]([^'"]+)([^>]*>)/gi
while ((img = imgPattern.exec(content)) !== null) { while ((img = imgPattern.exec(content)) !== null) {
const imgExt = ['*.jpg', '*.png', '*.webp'] images.push(img[1])
if (micromatch.isMatch(img[1], imgExt, { basename: true })) {
images.push(img[1])
}
} }
} }
@ -107,10 +103,11 @@ function openGraphHelper (options = {}) {
} }
images = images.map(path => { images = images.map(path => {
if (!new URL(path).host) { // parse relative url to full url
// resolve `path`'s absolute path relative to current page's url try {
// `path` can be both absolute (starts with `/`) or relative. new URL(path)
return new URL(path, url || config.url) } catch {
return new URL(path, config.url).href
} }
return path return path