-
-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(isExternal): avoid undefined & improve performance #124
Changes from 1 commit
623ba26
b7e4d9f
32bef13
d2c01a7
3408824
6ea2fb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say the purpose is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated to |
||
const data = new URL(url, `http://${sitehost}`); | ||
|
||
if (!sitehost || typeof data === 'string') return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with L15, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed. |
||
|
||
// 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; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggest to rename to
path
orinput
to not confuse withnew URL()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
input
might be a good idea.