Skip to content

Commit

Permalink
Merge pull request #124 from SukkaW/perf-is-external
Browse files Browse the repository at this point in the history
refactor(isExternal): avoid undefined & improve performance
  • Loading branch information
curbengh authored Nov 17, 2019
2 parents a1ad13b + 6ea2fb7 commit e2e3739
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
37 changes: 17 additions & 20 deletions lib/is_external_link.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
'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
* @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 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;
if (!sitehost) return false;

if (!sitehost || typeof data === 'string') return false;
// handle relative url
const data = new URL(input, `http://${sitehost}`);

// handle mailto: javascript: vbscript: and so on
if (data.origin === 'null') return false;

if (exclude && exclude.length) {
for (const i of exclude) {
if (host === i) return false;
const host = data.hostname;

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;
}
}
}

Expand Down
15 changes: 10 additions & 5 deletions test/is_external_link.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
describe('isExternalLink', () => {
const ctx = {
config: {
url: 'https://example.com',
external_link: {}
url: 'https://example.com'
}
};

Expand All @@ -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);
Expand Down

0 comments on commit e2e3739

Please sign in to comment.