From 60acb95c4c624fd904531103520cd2a02eec494f Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 6 Nov 2019 12:47:31 +0800 Subject: [PATCH 1/4] refactor: update isExternalLink --- lib/plugins/filter/after_post_render/external_link.js | 4 ++-- lib/plugins/filter/after_render/external_link.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/plugins/filter/after_post_render/external_link.js b/lib/plugins/filter/after_post_render/external_link.js index 10a0c0a3b2..f2f515efa8 100644 --- a/lib/plugins/filter/after_post_render/external_link.js +++ b/lib/plugins/filter/after_post_render/external_link.js @@ -14,12 +14,12 @@ 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 sitehost = urlObj(config.url).hostname || config.url; 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; diff --git a/lib/plugins/filter/after_render/external_link.js b/lib/plugins/filter/after_render/external_link.js index 92ed1f57fa..671d3bfc22 100644 --- a/lib/plugins/filter/after_render/external_link.js +++ b/lib/plugins/filter/after_render/external_link.js @@ -20,12 +20,12 @@ 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 sitehost = urlObj(config.url).hostname || config.url; 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; From 6d9ed7a3bef8ab03c00c3cf776494213b5ca5650 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 6 Nov 2019 14:01:43 +0800 Subject: [PATCH 2/4] refactor(external_link): run exclude array once a page --- lib/plugins/filter/after_post_render/external_link.js | 6 ++++-- lib/plugins/filter/after_render/external_link.js | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/plugins/filter/after_post_render/external_link.js b/lib/plugins/filter/after_post_render/external_link.js index f2f515efa8..8a940c5d66 100644 --- a/lib/plugins/filter/after_post_render/external_link.js +++ b/lib/plugins/filter/after_post_render/external_link.js @@ -11,8 +11,7 @@ const urlObj = (str) => { }; const isExternal = (url, config) => { - const exclude = Array.isArray(config.external_link.exclude) ? config.external_link.exclude - : [config.external_link.exclude]; + const { exclude } = config.external_link; const data = urlObj(url); const sitehost = urlObj(config.url).hostname || config.url; @@ -45,6 +44,9 @@ 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; diff --git a/lib/plugins/filter/after_render/external_link.js b/lib/plugins/filter/after_render/external_link.js index 671d3bfc22..5ddbe333df 100644 --- a/lib/plugins/filter/after_render/external_link.js +++ b/lib/plugins/filter/after_render/external_link.js @@ -17,8 +17,7 @@ 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 { exclude } = config.external_link; const data = urlObj(url); const sitehost = urlObj(config.url).hostname || config.url; @@ -51,6 +50,9 @@ 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; From 216f65355f96c60db055f2fc70c62202d22a9743 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 6 Nov 2019 14:41:36 +0800 Subject: [PATCH 3/4] refactor(external_link): relpace test() with includes() --- lib/plugins/filter/after_post_render/external_link.js | 4 ++-- lib/plugins/filter/after_render/external_link.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/plugins/filter/after_post_render/external_link.js b/lib/plugins/filter/after_post_render/external_link.js index 8a940c5d66..089efb9a75 100644 --- a/lib/plugins/filter/after_post_render/external_link.js +++ b/lib/plugins/filter/after_post_render/external_link.js @@ -48,9 +48,9 @@ function externalLinkFilter(data) { : [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 5ddbe333df..ecac70f51c 100644 --- a/lib/plugins/filter/after_render/external_link.js +++ b/lib/plugins/filter/after_render/external_link.js @@ -54,9 +54,9 @@ function externalLinkFilter(data) { : [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; From 20d4ae4ab270a6010a8a130fa988f27eccc2d850 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 6 Nov 2019 21:57:51 +0800 Subject: [PATCH 4/4] refactor(external_link): avoid urlObj --- .../filter/after_post_render/external_link.js | 15 ++++----------- lib/plugins/filter/after_render/external_link.js | 15 ++++----------- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/lib/plugins/filter/after_post_render/external_link.js b/lib/plugins/filter/after_post_render/external_link.js index 089efb9a75..c7e2469051 100644 --- a/lib/plugins/filter/after_post_render/external_link.js +++ b/lib/plugins/filter/after_post_render/external_link.js @@ -1,19 +1,12 @@ '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 } = config.external_link; - const data = urlObj(url); - const sitehost = urlObj(config.url).hostname || config.url; + 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; diff --git a/lib/plugins/filter/after_render/external_link.js b/lib/plugins/filter/after_render/external_link.js index ecac70f51c..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 @@ -18,8 +10,9 @@ const urlObj = (str) => { */ const isExternal = (url, config) => { const { exclude } = config.external_link; - const data = urlObj(url); - const sitehost = urlObj(config.url).hostname || config.url; + 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;