From e74a329b8a790518352f6e06925a55a9578d4ae3 Mon Sep 17 00:00:00 2001 From: Ian Lopshire Date: Thu, 19 Mar 2015 22:00:32 -0500 Subject: [PATCH] Navigation menu support for subdomains of blog url Closes #5033 - Added unit tests for the nav context of urlFor - Fixed issue in the nav context of urlFor where subdomains of blog url were truncated - Fixed issue in the nav context of urlFor where there was sometimes an extra preceding / --- core/server/config/url.js | 8 ++++++-- core/test/unit/config_spec.js | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/core/server/config/url.js b/core/server/config/url.js index a0d81f1e05..c7654bf5d5 100644 --- a/core/server/config/url.js +++ b/core/server/config/url.js @@ -156,10 +156,14 @@ function urlFor(context, data, absolute) { urlPath = data.nav.url; baseUrl = (secure && ghostConfig.urlSSL) ? ghostConfig.urlSSL : ghostConfig.url; hostname = baseUrl.split('//')[1] + ghostConfig.paths.subdir; - if (urlPath.indexOf(hostname) > -1) { + if (urlPath.indexOf(hostname) > -1 && urlPath.indexOf('.' + hostname) === -1) { // make link relative to account for possible // mismatch in http/https etc, force absolute - urlPath = '/' + urlPath.split(hostname)[1]; + // do not do so if link is a subdomain of blog url + urlPath = urlPath.split(hostname)[1]; + if (urlPath.substring(0, 1) !== '/') { + urlPath = '/' + urlPath; + } absolute = true; } } diff --git a/core/test/unit/config_spec.js b/core/test/unit/config_spec.js index df1ccdad67..94ee6482cd 100644 --- a/core/test/unit/config_spec.js +++ b/core/test/unit/config_spec.js @@ -225,6 +225,26 @@ describe('Config', function () { config.urlFor(testContext, testData).should.equal('/blog/tag/kitchen-sink/'); config.urlFor(testContext, testData, true).should.equal('http://my-ghost-blog.com/blog/tag/kitchen-sink/'); }); + + it('should return a url for a nav item when asked for it', function () { + var testContext = 'nav', + testData; + + config.set({url: 'http://my-ghost-blog.com', urlSSL: 'https://my-ghost-blog.com'}); + + testData = {nav: {url: 'http://my-ghost-blog.com/short-and-sweet/'}}; + config.urlFor(testContext, testData).should.equal('http://my-ghost-blog.com/short-and-sweet/'); + + testData = {nav: {url: 'http://my-ghost-blog.com/short-and-sweet/'}, secure: true}; + config.urlFor(testContext, testData).should.equal('https://my-ghost-blog.com/short-and-sweet/'); + + testData = {nav: {url: 'http://sub.my-ghost-blog.com/'}}; + config.urlFor(testContext, testData).should.equal('http://sub.my-ghost-blog.com/'); + + config.set({url: 'http://my-ghost-blog.com/blog'}); + testData = {nav: {url: 'http://my-ghost-blog.com/blog/short-and-sweet/'}}; + config.urlFor(testContext, testData).should.equal('http://my-ghost-blog.com/blog/short-and-sweet/'); + }); }); describe('urlPathForPost', function () {