From 07ab8c08d71769da5ec22050ab8957ceb195c099 Mon Sep 17 00:00:00 2001 From: curben <2809763-curben@users.noreply.gitlab.com> Date: Wed, 4 Sep 2019 02:31:06 +0100 Subject: [PATCH] fix(openGraph): URL api error handling --- scripts/openGraph.js | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/scripts/openGraph.js b/scripts/openGraph.js index 8ad2926..4ace0e5 100644 --- a/scripts/openGraph.js +++ b/scripts/openGraph.js @@ -4,9 +4,9 @@ /* * Modified from the hexo version, * https://github.com/hexojs/hexo/blob/master/lib/plugins/helper/open_graph.js -* for compatibility with cloudinary.js -* the now use -* where href value links to an image +* to include https://github.com/hexojs/hexo/pull/3674 +* and use WHATWG URL API +* https://nodejs.org/api/url.html#url_the_whatwg_url_api */ 'use strict' @@ -67,16 +67,12 @@ function openGraphHelper (options = {}) { } if (!images.length && content && content.includes(']*href=['"]([^'"]+)([^>]*>)/gi + const imgPattern = /]*src=['"]([^'"]+)([^>]*>)/gi while ((img = imgPattern.exec(content)) !== null) { - const imgExt = ['*.jpg', '*.png', '*.webp'] - if (micromatch.isMatch(img[1], imgExt, { basename: true })) { - images.push(img[1]) - } + images.push(img[1]) } } @@ -107,10 +103,11 @@ function openGraphHelper (options = {}) { } images = images.map(path => { - if (!new URL(path).host) { - // resolve `path`'s absolute path relative to current page's url - // `path` can be both absolute (starts with `/`) or relative. - return new URL(path, url || config.url) + // parse relative url to full url + try { + new URL(path) + } catch { + return new URL(path, config.url).href } return path