Fix invalid image helper URLs when using a subdir.

No Issue
- Strip sub-directory from image paths before passing to
  config.createUrl. Since images are stored with the sub-directory
  and createUrl builds a URL with the sub-directory the result
  would be a URL that contains the sub-directory twice.
This commit is contained in:
Jason Williams 2014-11-17 03:38:10 +00:00
parent f76088169a
commit e499e5dda9
2 changed files with 44 additions and 0 deletions

View File

@ -139,6 +139,9 @@ function urlFor(context, data, absolute) {
imagePathRe = new RegExp('^' + ghostConfig.paths.subdir + '/' + ghostConfig.paths.imagesRelPath);
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), '');
}
// other objects are recognised but not yet supported
} else if (_.isString(context) && _.indexOf(_.keys(knownPaths), context) !== -1) {

View File

@ -67,3 +67,44 @@ describe('{{image}} helper', function () {
}).catch(done);
});
});
describe('{{image}} helper when Ghost is running on a sub-directory', function () {
var sandbox;
before(function () {
sandbox = sinon.sandbox.create();
utils.overrideConfig({url: 'http://testurl.com/blog'});
utils.loadHelpers();
});
afterEach(function () {
sandbox.restore();
});
after(function () {
utils.restoreConfig();
});
it('should output relative url of image', function (done) {
helpers.image.call({
image: '/blog/content/images/image-relative-url.png',
author: {
image: '/blog/content/images/author-image-relatve-url.png'
}
}).then(function (rendered) {
should.exist(rendered);
rendered.should.equal('/blog/content/images/image-relative-url.png');
done();
}).catch(done);
});
it('should output absolute url of image if the option is present ', function (done) {
helpers.image.call({image: '/blog/content/images/image-relative-url.png',
author: {image: '/blog/content/images/author-image-relatve-url.png'}},
{hash: {absolute: 'true'}}).then(function (rendered) {
should.exist(rendered);
rendered.should.equal('http://testurl.com/blog/content/images/image-relative-url.png');
done();
}).catch(done);
});
});