From 03077e790f4195bdd5d73de66ea898b441b70c76 Mon Sep 17 00:00:00 2001 From: Tommy Chen Date: Mon, 18 May 2015 22:47:59 +0800 Subject: [PATCH] relative_url helper: replace trailing slash with `index.html`. --- lib/plugins/helper/relative_url.js | 7 +++++++ test/scripts/helpers/relative_url.js | 8 ++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/plugins/helper/relative_url.js b/lib/plugins/helper/relative_url.js index 805f90e352..1c71da79b1 100644 --- a/lib/plugins/helper/relative_url.js +++ b/lib/plugins/helper/relative_url.js @@ -17,6 +17,13 @@ function relativeUrlHelper(from, to){ out.unshift('..'); } + var outLength = out.length; + + // If the last 2 elements of `out` is empty strings, replace them with `index.html`. + if (outLength > 1 && !out[outLength - 1] && !out[outLength] - 2){ + out = out.slice(0, outLength - 2).concat('index.html'); + } + return out.join('/').replace(/\/{2,}/g, '/'); } diff --git a/test/scripts/helpers/relative_url.js b/test/scripts/helpers/relative_url.js index 43ee1e36d0..0324d8151d 100644 --- a/test/scripts/helpers/relative_url.js +++ b/test/scripts/helpers/relative_url.js @@ -25,8 +25,8 @@ describe('relative_url', function(){ }); it('to root', function(){ - relativeURL('index.html', '/').should.eql('/'); - relativeURL('foo/', '/').should.eql('../'); - relativeURL('foo/index.html', '/').should.eql('../'); + relativeURL('index.html', '/').should.eql('index.html'); + relativeURL('foo/', '/').should.eql('../index.html'); + relativeURL('foo/index.html', '/').should.eql('../index.html'); }); -}); +}); \ No newline at end of file