Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
const { URL } = require('url'); | ||
|
||
const urlObj = (str) => { | ||
try { | ||
return new URL(str); | ||
} catch (err) { | ||
return str; | ||
} | ||
}; | ||
|
||
/** | ||
* Check whether the link is external | ||
* @param {String} url 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) { | ||
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; | ||
|
||
if (!sitehost || typeof data === 'string') return false; | ||
|
||
// 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; | ||
} | ||
} | ||
|
||
if (host !== sitehost) return true; | ||
|
||
return false; | ||
} | ||
|
||
module.exports = isExternalLink; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
|
||
describe('isExternalLink', () => { | ||
const ctx = { | ||
config: { | ||
url: 'https://example.com', | ||
external_link: {} | ||
} | ||
}; | ||
|
||
const isExternalLink = require('../lib/is_external_link').bind(ctx); | ||
|
||
it('external link', () => { | ||
isExternalLink('https://hexo.io/').should.eql(true); | ||
}); | ||
|
||
it('internal link', () => { | ||
isExternalLink('https://example.com').should.eql(false); | ||
isExternalLink('//example.com').should.eql(false); | ||
isExternalLink('//example.com/archives/foo.html').should.eql(false); | ||
isExternalLink('/archives/foo.html').should.eql(false); | ||
}); | ||
|
||
it('hash, mailto, javascript', () => { | ||
isExternalLink('#top').should.eql(false); | ||
isExternalLink('mailto:hi@hexo.io').should.eql(false); | ||
isExternalLink('javascript:alert(\'Hexo is Awesome\')').should.eql(false); | ||
}); | ||
|
||
it('exclude - empty string', () => { | ||
ctx.config.external_link.exclude = ''; | ||
isExternalLink('https://hexo.io/').should.eql(true); | ||
}); | ||
|
||
it('exclude - string', () => { | ||
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']; | ||
isExternalLink('https://foo.com/').should.eql(false); | ||
isExternalLink('https://bar.com/').should.eql(false); | ||
isExternalLink('https://baz.com/').should.eql(true); | ||
}); | ||
}); |