Skip to content

Commit

Permalink
fix(full_url_for): handle config.url with a trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Jul 12, 2020
1 parent 14fa817 commit 6cd1494
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
9 changes: 5 additions & 4 deletions lib/full_url_for.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ 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}`, () => {
if (/^(\/\/|http(s)?:)/.test(path)) return path;
if (path === '/') return config.url;

const sitehost = parse(config.url).hostname || config.url;
const data = new URL(path, `http://${sitehost}`);
const { host, path: sitePath, protocol } = parse(config.url);
const data = new URL(path, `http://${host}`);

// Exit if input is an external link or a data url
if (data.hostname !== sitehost || data.origin === 'null') return path;
if (data.host !== host || data.origin === 'null') return path;

path = encodeURL(config.url + `/${path}`.replace(/\/{2,}/g, '/'));
path = encodeURL(protocol + '//' + host + (sitePath + `/${path}`).replace(/\/{2,}/g, '/'));
path = prettyUrls(path, prettyUrlsOptions);

return path;
Expand Down
14 changes: 10 additions & 4 deletions test/full_url_for.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ describe('full_url_for', () => {
it('internal url - root directory', () => {
ctx.config.url = 'https://example.com';
fullUrlFor('index.html').should.eql(ctx.config.url + '/index.html');
fullUrlFor('/').should.eql(ctx.config.url + '/');
fullUrlFor('/').should.eql(ctx.config.url);
});

it('internal url - subdirectory', () => {
ctx.config.url = 'https://example.com/blog';
fullUrlFor('index.html').should.eql(ctx.config.url + '/index.html');
fullUrlFor('/').should.eql(ctx.config.url + '/');
fullUrlFor('/').should.eql(ctx.config.url);
});

it('internal url - subdirectory with trailing slash', () => {
ctx.config.url = 'https://example.com/blog/';
fullUrlFor('index.html').should.eql(ctx.config.url + 'index.html');
fullUrlFor('/').should.eql(ctx.config.url);
});

it('internal url - no duplicate slash', () => {
Expand All @@ -34,7 +40,7 @@ describe('full_url_for', () => {
};

fullUrlFor('index.html').should.eql(ctx.config.url + '/');
fullUrlFor('/').should.eql(ctx.config.url + '/');
fullUrlFor('/').should.eql(ctx.config.url);
});

it('internal url - pretty_urls.trailing_html disabled', () => {
Expand All @@ -56,7 +62,7 @@ describe('full_url_for', () => {
};

fullUrlFor('index.html').should.eql(ctx.config.url + '/');
fullUrlFor('/').should.eql(ctx.config.url + '/');
fullUrlFor('/').should.eql(ctx.config.url);
fullUrlFor('/foo/bar.html').should.eql(ctx.config.url + '/foo/bar');
});

Expand Down

0 comments on commit 6cd1494

Please sign in to comment.