Skip to content

Commit

Permalink
refactor(external_link): avoid urlObj
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Nov 6, 2019
1 parent 216f653 commit 20d4ae4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
15 changes: 4 additions & 11 deletions lib/plugins/filter/after_post_render/external_link.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
15 changes: 4 additions & 11 deletions lib/plugins/filter/after_render/external_link.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down

0 comments on commit 20d4ae4

Please sign in to comment.