From 623ba269a2f0f3d34d1d6bb215d1f2ca40cb02ed Mon Sep 17 00:00:00 2001 From: SukkaW Date: Fri, 8 Nov 2019 21:05:23 +0800 Subject: [PATCH 1/6] refactor(isExternal): avoid undefined & improve performance --- lib/is_external_link.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/is_external_link.js b/lib/is_external_link.js index 5b517eed..c5a08d4d 100644 --- a/lib/is_external_link.js +++ b/lib/is_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,17 +10,19 @@ const urlObj = (str) => { function isExternalLink(url) { const { config } = this; - 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 = parse(config.url).hostname || config.url; + // Pass a base to new URL + const data = new URL(url, `http://${sitehost}`); if (!sitehost || typeof data === 'string') return false; // handle mailto: javascript: vbscript: and so on if (data.origin === 'null') return false; + const host = data.hostname; + const exclude = Array.isArray(config.external_link.exclude) ? config.external_link.exclude + : [config.external_link.exclude]; + if (exclude && exclude.length) { for (const i of exclude) { if (host === i) return false; From b7e4d9fab322e63dc33a0e168d38a81e5d90bcf2 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 9 Nov 2019 12:10:36 +0800 Subject: [PATCH 2/6] refactor(isExternal): update Apply suggestions from code review by @curbengh --- lib/is_external_link.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/is_external_link.js b/lib/is_external_link.js index c5a08d4d..d63e6528 100644 --- a/lib/is_external_link.js +++ b/lib/is_external_link.js @@ -11,10 +11,10 @@ const { parse, URL } = require('url'); function isExternalLink(url) { const { config } = this; const sitehost = parse(config.url).hostname || config.url; - // Pass a base to new URL - const data = new URL(url, `http://${sitehost}`); + if (!sitehost) return false; - if (!sitehost || typeof data === 'string') return false; + // handle relative url + const data = new URL(url, `http://${sitehost}`); // handle mailto: javascript: vbscript: and so on if (data.origin === 'null') return false; From 32bef13350b28b7166ad53b02d8e3c34a2e689ba Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 9 Nov 2019 12:20:22 +0800 Subject: [PATCH 3/6] refactor(isExternal): rename url to input --- README.md | 2 +- lib/is_external_link.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d4895e0b..60f72624 100644 --- a/README.md +++ b/README.md @@ -254,7 +254,7 @@ htmlTag('script', {src: '/foo.js', async: true}, '') // ``` -### isExternalLink(url) +### isExternalLink(input) Returns if a given url is external link relative to `config.url` and `config.exclude`. diff --git a/lib/is_external_link.js b/lib/is_external_link.js index d63e6528..1e37fbad 100644 --- a/lib/is_external_link.js +++ b/lib/is_external_link.js @@ -4,17 +4,17 @@ const { parse, URL } = require('url'); /** * Check whether the link is external - * @param {String} url The url to check + * @param {String} input The url to check * @returns {Boolean} True if the link doesn't have protocol or link has same host with config.url */ -function isExternalLink(url) { +function isExternalLink(input) { const { config } = this; const sitehost = parse(config.url).hostname || config.url; if (!sitehost) return false; // handle relative url - const data = new URL(url, `http://${sitehost}`); + const data = new URL(input, `http://${sitehost}`); // handle mailto: javascript: vbscript: and so on if (data.origin === 'null') return false; From d2c01a76cbc2631f8dc07ef8fdd27dbb8f9745ba Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Sat, 9 Nov 2019 15:13:55 +1030 Subject: [PATCH 4/6] docs(isExternalLink): "url" as parameter name --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 60f72624..d4895e0b 100644 --- a/README.md +++ b/README.md @@ -254,7 +254,7 @@ htmlTag('script', {src: '/foo.js', async: true}, '') // ``` -### isExternalLink(input) +### isExternalLink(url) Returns if a given url is external link relative to `config.url` and `config.exclude`. From 34088245fc4f576321546a7dab4cea10eb8ffd74 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 9 Nov 2019 15:10:00 +0800 Subject: [PATCH 5/6] fix(isExternalLink): handle when there is no external_link or exclude --- lib/is_external_link.js | 14 +++++++++----- test/is_external_link.spec.js | 15 ++++++++++----- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/is_external_link.js b/lib/is_external_link.js index 1e37fbad..c5dcbde7 100644 --- a/lib/is_external_link.js +++ b/lib/is_external_link.js @@ -20,15 +20,19 @@ function isExternalLink(input) { if (data.origin === 'null') return false; const host = data.hostname; - const exclude = Array.isArray(config.external_link.exclude) ? config.external_link.exclude - : [config.external_link.exclude]; - if (exclude && exclude.length) { - for (const i of exclude) { - if (host === i) return false; + if (config.external_link && config.external_link.exclude) { + const exclude = Array.isArray(config.external_link.exclude) ? config.external_link.exclude + : [config.external_link.exclude]; + + if (exclude && exclude.length) { + for (const i of exclude) { + if (host === i) return false; + } } } + if (host !== sitehost) return true; return false; diff --git a/test/is_external_link.spec.js b/test/is_external_link.spec.js index e2a11113..78c84b99 100644 --- a/test/is_external_link.spec.js +++ b/test/is_external_link.spec.js @@ -3,8 +3,7 @@ describe('isExternalLink', () => { const ctx = { config: { - url: 'https://example.com', - external_link: {} + url: 'https://example.com' } }; @@ -28,19 +27,25 @@ describe('isExternalLink', () => { }); it('exclude - empty string', () => { - ctx.config.external_link.exclude = ''; + ctx.config.external_link = { + exclude: '' + }; isExternalLink('https://hexo.io/').should.eql(true); }); it('exclude - string', () => { - ctx.config.external_link.exclude = 'foo.com'; + ctx.config.external_link = { + exclude: 'foo.com' + }; isExternalLink('https://foo.com/').should.eql(false); isExternalLink('https://bar.com/').should.eql(true); isExternalLink('https://baz.com/').should.eql(true); }); it('exclude - array', () => { - ctx.config.external_link.exclude = ['foo.com', 'bar.com']; + ctx.config.external_link = { + exclude: ['foo.com', 'bar.com'] + }; isExternalLink('https://foo.com/').should.eql(false); isExternalLink('https://bar.com/').should.eql(false); isExternalLink('https://baz.com/').should.eql(true); From 6ea2fb73f6c9cefff68dfd2713c14e42e02e9531 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Sun, 17 Nov 2019 13:42:16 +1030 Subject: [PATCH 6/6] style: remove extra new line --- lib/is_external_link.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/is_external_link.js b/lib/is_external_link.js index c5dcbde7..e3c09095 100644 --- a/lib/is_external_link.js +++ b/lib/is_external_link.js @@ -32,7 +32,6 @@ function isExternalLink(input) { } } - if (host !== sitehost) return true; return false;