From 6cd14945a668052bf58dcba1b856bbd95eac69e5 Mon Sep 17 00:00:00 2001 From: MDLeom <43627182+curbengh@users.noreply.github.com> Date: Wed, 1 Jul 2020 09:31:25 +0100 Subject: [PATCH] fix(full_url_for): handle config.url with a trailing slash --- lib/full_url_for.js | 9 +++++---- test/full_url_for.spec.js | 14 ++++++++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/full_url_for.js b/lib/full_url_for.js index 27233843..114612d7 100755 --- a/lib/full_url_for.js +++ b/lib/full_url_for.js @@ -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; diff --git a/test/full_url_for.spec.js b/test/full_url_for.spec.js index b8d8100a..25a70441 100755 --- a/test/full_url_for.spec.js +++ b/test/full_url_for.spec.js @@ -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', () => { @@ -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', () => { @@ -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'); });