diff --git a/lib/plugins/filter/after_post_render/external_link.js b/lib/plugins/filter/after_post_render/external_link.js index 10a0c0a3b2..c7e2469051 100644 --- a/lib/plugins/filter/after_post_render/external_link.js +++ b/lib/plugins/filter/after_post_render/external_link.js @@ -1,25 +1,17 @@ 'use strict'; -const { URL } = require('url'); - -const urlObj = (str) => { - try { - return new URL(str); - } catch (err) { - return str; - } -}; +const { parse, URL } = require('url'); const isExternal = (url, config) => { - const exclude = Array.isArray(config.external_link.exclude) ? config.external_link.exclude - : [config.external_link.exclude]; - const data = urlObj(url); - const host = data.hostname; - const sitehost = typeof urlObj(config.url) === 'object' ? urlObj(config.url).hostname : config.url; + const { exclude } = config.external_link; + const sitehost = parse(config.url).hostname || config.url; + // some little dirty hacks + const data = new URL(url, `http://${sitehost}`); if (!sitehost || typeof data === 'string') return false; if (data.origin === 'null') return false; + const host = data.hostname; if (exclude && exclude.length) { for (const i of exclude) { if (host === i) return false; @@ -45,10 +37,13 @@ function externalLinkFilter(data) { if (config.external_link === false || config.external_link.enable === false || config.external_link.field !== 'post') return; + config.external_link.exclude = Array.isArray(config.external_link.exclude) ? config.external_link.exclude + : [config.external_link.exclude]; + data.content = data.content.replace(//gi, (str, hrefStr, href) => { - if (/target=/gi.test(str) || !isExternal(href, config)) return str; + if (str.includes('target=') || !isExternal(href, config)) return str; - if (/rel=/gi.test(str)) { + if (str.includes('rel=')) { str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => { if (!rel.includes('noopenner')) relStr = relStr.replace(rel, `${rel} noopener`); return relStr; diff --git a/lib/plugins/filter/after_render/external_link.js b/lib/plugins/filter/after_render/external_link.js index 92ed1f57fa..dfdec89f85 100644 --- a/lib/plugins/filter/after_render/external_link.js +++ b/lib/plugins/filter/after_render/external_link.js @@ -1,14 +1,6 @@ 'use strict'; -const { URL } = require('url'); - -const urlObj = (str) => { - try { - return new URL(str); - } catch (err) { - return str; - } -}; +const { parse, URL } = require('url'); /** * Check whether the link is external @@ -17,15 +9,15 @@ const urlObj = (str) => { * @returns {Boolean} True if the link doesn't have protocol or link has same host with config.url */ const isExternal = (url, config) => { - const exclude = Array.isArray(config.external_link.exclude) ? config.external_link.exclude - : [config.external_link.exclude]; - const data = urlObj(url); - const host = data.hostname; - const sitehost = typeof urlObj(config.url) === 'object' ? urlObj(config.url).hostname : config.url; + const { exclude } = config.external_link; + const sitehost = parse(config.url).hostname || config.url; + // some little dirty hacks + const data = new URL(url, `http://${sitehost}`); if (!sitehost || typeof data === 'string') return false; if (data.origin === 'null') return false; + const host = data.hostname; if (exclude && exclude.length) { for (const i of exclude) { if (host === i) return false; @@ -51,10 +43,13 @@ function externalLinkFilter(data) { if (config.external_link === false || config.external_link.enable === false || config.external_link.field !== 'site') return; + config.external_link.exclude = Array.isArray(config.external_link.exclude) ? config.external_link.exclude + : [config.external_link.exclude]; + data = data.replace(//gi, (str, hrefStr, href) => { - if (/target=/gi.test(str) || !isExternal(href, config)) return str; + if (str.includes('target=') || !isExternal(href, config)) return str; - if (/rel=/gi.test(str)) { + if (str.includes('rel=')) { str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => { if (!rel.includes('noopenner')) relStr = relStr.replace(rel, `${rel} noopener`); return relStr;