diff --git a/core/server/config/url.js b/core/server/config/url.js index 88ee9e0481..928aeeb237 100644 --- a/core/server/config/url.js +++ b/core/server/config/url.js @@ -32,7 +32,6 @@ function setConfig(config) { function createUrl(urlPath, absolute, secure) { urlPath = urlPath || '/'; absolute = absolute || false; - var output = '', baseUrl; // create base of url, always ends without a slash @@ -103,7 +102,7 @@ function urlPathForPost(post, permalinks) { function urlFor(context, data, absolute) { var urlPath = '/', secure, imagePathRe, - knownObjects = ['post', 'tag', 'author', 'image'], + knownObjects = ['post', 'tag', 'author', 'image'], baseUrl, // this will become really big knownPaths = { @@ -140,8 +139,15 @@ function urlFor(context, data, absolute) { absolute = imagePathRe.test(data.image) ? absolute : false; secure = data.image.secure; - // Remove the sub-directory from the URL because createUrl() will add it back. - urlPath = urlPath.replace(new RegExp('^' + ghostConfig.paths.subdir), ''); + if (absolute) { + // Remove the sub-directory from the URL because ghostConfig will add it back. + urlPath = urlPath.replace(new RegExp('^' + ghostConfig.paths.subdir), ''); + baseUrl = (secure && ghostConfig.urlSSL) ? ghostConfig.urlSSL : ghostConfig.url; + baseUrl = baseUrl.replace(/\/$/, ''); + urlPath = baseUrl + urlPath; + } + + return urlPath; } // other objects are recognised but not yet supported } else if (_.isString(context) && _.indexOf(_.keys(knownPaths), context) !== -1) { diff --git a/core/test/unit/server_helpers/image_spec.js b/core/test/unit/server_helpers/image_spec.js index 8b1d8aabbb..f1cdbf7086 100644 --- a/core/test/unit/server_helpers/image_spec.js +++ b/core/test/unit/server_helpers/image_spec.js @@ -34,7 +34,7 @@ describe('{{image}} helper', function () { helpers.image.call({ image: '/content/images/image-relative-url.png', author: { - image: '/content/images/author-image-relatve-url.png' + image: '/content/images/author-image-relative-url.png' } }).then(function (rendered) { should.exist(rendered); @@ -45,7 +45,7 @@ describe('{{image}} helper', function () { it('should output absolute url of image if the option is present ', function (done) { helpers.image.call({image: '/content/images/image-relative-url.png', - author: {image: '/content/images/author-image-relatve-url.png'}}, + author: {image: '/content/images/author-image-relative-url.png'}}, {hash: {absolute: 'true'}}).then(function (rendered) { should.exist(rendered); rendered.should.equal('http://testurl.com/content/images/image-relative-url.png'); @@ -89,7 +89,7 @@ describe('{{image}} helper when Ghost is running on a sub-directory', function ( helpers.image.call({ image: '/blog/content/images/image-relative-url.png', author: { - image: '/blog/content/images/author-image-relatve-url.png' + image: '/blog/content/images/author-image-relative-url.png' } }).then(function (rendered) { should.exist(rendered); @@ -107,4 +107,17 @@ describe('{{image}} helper when Ghost is running on a sub-directory', function ( done(); }).catch(done); }); + + it('should not change output for an external url', function (done) { + helpers.image.call({ + image: 'http://example.com/picture.jpg', + author: { + image: '/blog/content/images/author-image-relative-url.png' + } + }).then(function (rendered) { + should.exist(rendered); + rendered.should.equal('http://example.com/picture.jpg'); + done(); + }).catch(done); + }); });