Skip to content

Commit

Permalink
perf(is_ecternal_link): absolute url detection
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Feb 20, 2020
1 parent 12bdb3a commit 2a9a5ba
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
3 changes: 1 addition & 2 deletions lib/full_url_for.js
Expand Up @@ -16,8 +16,7 @@ function fullUrlForHelper(path = '/') {

// cacheId is designed to works across different hexo.config & options
return cache.apply(`${config.url}-${prettyUrlsOptions.trailing_index}-${prettyUrlsOptions.trailing_html}-${path}`, () => {
const pathRegex = /^(\/\/|http(s)?:)/;
if (pathRegex.test(path)) return path;
if (/^(\/\/|http(s)?:)/.test(path)) return path;

const sitehost = parse(config.url).hostname || config.url;
const data = new URL(path, `http://${sitehost}`);
Expand Down
3 changes: 3 additions & 0 deletions lib/is_external_link.js
Expand Up @@ -13,6 +13,9 @@ const cache = new Cache();

function isExternalLink(input, sitehost, exclude) {
return cache.apply(`${input}-${sitehost}-${exclude}`, () => {
// Return false early for internal link
if (!/^(\/\/|http(s)?:)/.test(input)) return false;

sitehost = parse(sitehost).hostname || sitehost;

if (!sitehost) return false;
Expand Down
5 changes: 1 addition & 4 deletions lib/url_for.js
Expand Up @@ -28,10 +28,7 @@ function urlForHelper(path = '/', options) {

// cacheId is designed to works across different hexo.config & options
return cache.apply(`${config.url}-${root}-${prettyUrlsOptions.trailing_index}-${prettyUrlsOptions.trailing_html}-${path}`, () => {
const pathRegex = /^(#|\/\/|http(s)?:)/;
if (pathRegex.test(path)) {
return path;
}
if (/^(#|\/\/|http(s)?:)/.test(path)) return path;

const sitehost = parse(config.url).hostname || config.url;
const data = new URL(path, `http://${sitehost}`);
Expand Down
2 changes: 2 additions & 0 deletions test/is_external_link.spec.js
Expand Up @@ -15,13 +15,15 @@ describe('isExternalLink', () => {

it('external link', () => {
isExternalLink('https://hexo.io/', ctx.config.url).should.eql(true);
isExternalLink('//hexo.io/', ctx.config.url).should.eql(true);
});

it('internal link', () => {
isExternalLink('https://example.com', ctx.config.url).should.eql(false);
isExternalLink('//example.com', ctx.config.url).should.eql(false);
isExternalLink('//example.com/archives/foo.html', ctx.config.url).should.eql(false);
isExternalLink('/archives/foo.html', ctx.config.url).should.eql(false);
isExternalLink('/archives//hexo.io', ctx.config.url).should.eql(false);
});

it('hash, mailto, javascript', () => {
Expand Down

0 comments on commit 2a9a5ba

Please sign in to comment.