Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/';
Copy link
Member

@SukkaW SukkaW Jul 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/hexojs/hexo/blob/17ee23fa181bcc2c3b47eba5b345762aef1eac36/test/scripts/hexo/load_config.js#L93-L106

Just as the test case of load_config shows, the url: https://hexo.io/ will result in hexo.config.url.should.eql('https://hexo.io');, while root: foo will result in hexo.config.root.should.eql('foo/');. Thus, the test case just here just won't exist.

Copy link
Contributor Author

@curbengh curbengh Jul 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/hexojs/hexo/blob/17ee23fa181bcc2c3b47eba5b345762aef1eac36/lib/hexo/load_config.js#L34-L35 should be removed if this PR is accepted. Those two lines are considered a workaround and this PR already handles it.

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