Change url and image into synchronous helpers

Closes #3961
This commit is contained in:
Jason Williams 2014-12-28 19:38:29 +00:00
parent edc98602d3
commit 0dc0d37888
5 changed files with 91 additions and 98 deletions

View File

@ -4,16 +4,14 @@
// Returns the URL for the current object scope i.e. If inside a post scope will return image permalink
// `absolute` flag outputs absolute URL, else URL is relative.
var Promise = require('bluebird'),
config = require('../config'),
var config = require('../config'),
image;
image = function (options) {
var absolute = options && options.hash.absolute;
if (this.image) {
return Promise.resolve(config.urlFor('image', {image: this.image}, absolute));
} else {
return Promise.resolve();
return config.urlFor('image', {image: this.image}, absolute);
}
};

View File

@ -94,6 +94,8 @@ registerHelpers = function (adminHbs) {
registerThemeHelper('pagination', coreHelpers.pagination);
registerThemeHelper('tags', coreHelpers.tags);
registerThemeHelper('plural', coreHelpers.plural);
registerThemeHelper('url', coreHelpers.url);
registerThemeHelper('image', coreHelpers.image);
// Async theme helpers
registerAsyncThemeHelper('body_class', coreHelpers.body_class);
@ -102,8 +104,6 @@ registerHelpers = function (adminHbs) {
registerAsyncThemeHelper('meta_description', coreHelpers.meta_description);
registerAsyncThemeHelper('meta_title', coreHelpers.meta_title);
registerAsyncThemeHelper('post_class', coreHelpers.post_class);
registerAsyncThemeHelper('url', coreHelpers.url);
registerAsyncThemeHelper('image', coreHelpers.image);
// Register admin helpers
registerAdminHelper('ghost_script_tags', coreHelpers.ghost_script_tags);

View File

@ -4,8 +4,7 @@
// Returns the URL for the current object scope i.e. If inside a post scope will return post permalink
// `absolute` flag outputs absolute URL, else URL is relative
var Promise = require('bluebird'),
config = require('../config'),
var config = require('../config'),
schema = require('../data/schema').checks,
url;
@ -13,18 +12,18 @@ url = function (options) {
var absolute = options && options.hash.absolute;
if (schema.isPost(this)) {
return Promise.resolve(config.urlFor('post', {post: this}, absolute));
return config.urlFor('post', {post: this}, absolute);
}
if (schema.isTag(this)) {
return Promise.resolve(config.urlFor('tag', {tag: this}, absolute));
return config.urlFor('tag', {tag: this}, absolute);
}
if (schema.isUser(this)) {
return Promise.resolve(config.urlFor('author', {author: this}, absolute));
return config.urlFor('author', {author: this}, absolute);
}
return Promise.resolve(config.urlFor(this, absolute));
return config.urlFor(this, absolute);
};
module.exports = url;

View File

@ -30,41 +30,41 @@ describe('{{image}} helper', function () {
should.exist(handlebars.helpers.image);
});
it('should output relative url of image', function (done) {
helpers.image.call({
it('should output relative url of image', function () {
var rendered = helpers.image.call({
image: '/content/images/image-relative-url.png',
author: {
image: '/content/images/author-image-relative-url.png'
}
}).then(function (rendered) {
should.exist(rendered);
rendered.should.equal('/content/images/image-relative-url.png');
done();
}).catch(done);
});
should.exist(rendered);
rendered.should.equal('/content/images/image-relative-url.png');
});
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-relative-url.png'}},
{hash: {absolute: 'true'}}).then(function (rendered) {
should.exist(rendered);
rendered.should.equal('http://testurl.com/content/images/image-relative-url.png');
done();
}).catch(done);
it('should output absolute url of image if the option is present ', function () {
var rendered = helpers.image.call({
image: '/content/images/image-relative-url.png',
author: {image: '/content/images/author-image-relative-url.png'}
},
{
hash: {absolute: 'true'}
});
should.exist(rendered);
rendered.should.equal('http://testurl.com/content/images/image-relative-url.png');
});
it('should have no output if there is no image ', function (done) {
helpers.image.call({image: null}, {hash: {absolute: 'true'}}).then(function (rendered) {
should.not.exist(rendered);
done();
}).catch(done);
it('should have no output if there is no image ', function () {
var rendered = helpers.image.call({image: null}, {hash: {absolute: 'true'}});
should.not.exist(rendered);
});
it('should have no output if there is no image property ', function (done) {
helpers.image.call({}, {hash: {absolute: 'true'}}).then(function (rendered) {
should.not.exist(rendered);
done();
}).catch(done);
it('should have no output if there is no image property ', function () {
var rendered = helpers.image.call({}, {hash: {absolute: 'true'}});
should.not.exist(rendered);
});
});
@ -85,39 +85,38 @@ describe('{{image}} helper when Ghost is running on a sub-directory', function (
utils.restoreConfig();
});
it('should output relative url of image', function (done) {
helpers.image.call({
it('should output relative url of image', function () {
var rendered = helpers.image.call({
image: '/blog/content/images/image-relative-url.png',
author: {
image: '/blog/content/images/author-image-relative-url.png'
}
}).then(function (rendered) {
should.exist(rendered);
rendered.should.equal('/blog/content/images/image-relative-url.png');
done();
}).catch(done);
});
should.exist(rendered);
rendered.should.equal('/blog/content/images/image-relative-url.png');
});
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);
it('should output absolute url of image if the option is present ', function () {
var rendered = helpers.image.call({
image: '/blog/content/images/image-relative-url.png',
author: {image: '/blog/content/images/author-image-relatve-url.png'}},
{hash: {absolute: 'true'}
});
should.exist(rendered);
rendered.should.equal('http://testurl.com/blog/content/images/image-relative-url.png');
});
it('should not change output for an external url', function (done) {
helpers.image.call({
it('should not change output for an external url', function () {
var rendered = 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);
});
should.exist(rendered);
rendered.should.equal('http://example.com/picture.jpg');
});
});

View File

@ -38,62 +38,59 @@ describe('{{url}} helper', function () {
should.exist(handlebars.helpers.url);
});
it('should return the slug with a prefix slash if the context is a post', function (done) {
helpers.url.call({
it('should return the slug with a prefix slash if the context is a post', function () {
var rendered = helpers.url.call({
html: 'content',
markdown: 'ff',
title: 'title',
slug: 'slug',
created_at: new Date(0),
url: '/slug/'
}).then(function (rendered) {
should.exist(rendered);
rendered.should.equal('/slug/');
done();
}).catch(done);
});
should.exist(rendered);
rendered.should.equal('/slug/');
});
it('should output an absolute URL if the option is present', function (done) {
helpers.url.call(
it('should output an absolute URL if the option is present', function () {
var rendered = helpers.url.call(
{html: 'content', markdown: 'ff', title: 'title', slug: 'slug', url: '/slug/', created_at: new Date(0)},
{hash: {absolute: 'true'}}
).then(function (rendered) {
should.exist(rendered);
rendered.should.equal('http://testurl.com/slug/');
done();
}).catch(done);
);
should.exist(rendered);
rendered.should.equal('http://testurl.com/slug/');
});
it('should return the slug with a prefixed /tag/ if the context is a tag', function (done) {
helpers.url.call({
it('should return the slug with a prefixed /tag/ if the context is a tag', function () {
var rendered = helpers.url.call({
name: 'the tag',
slug: 'the-tag',
description: null,
parent: null
}).then(function (rendered) {
should.exist(rendered);
rendered.should.equal('/tag/the-tag/');
done();
}).catch(done);
});
should.exist(rendered);
rendered.should.equal('/tag/the-tag/');
});
it('should return / if not a post or tag', function (done) {
helpers.url.call({markdown: 'ff', title: 'title', slug: 'slug'}).then(function (rendered) {
rendered.should.equal('/');
}).then(function () {
return helpers.url.call({html: 'content', title: 'title', slug: 'slug'}).then(function (rendered) {
rendered.should.equal('/');
});
}).then(function () {
return helpers.url.call({html: 'content', markdown: 'ff', slug: 'slug'}).then(function (rendered) {
rendered.should.equal('/');
});
}).then(function () {
helpers.url.call({html: 'content', markdown: 'ff', title: 'title'}).then(function (rendered) {
rendered.should.equal('/');
it('should return / if not a post or tag', function () {
var rendered;
done();
});
}).catch(done);
rendered = helpers.url.call({markdown: 'ff', title: 'title', slug: 'slug'});
should.exist(rendered);
rendered.should.equal('/');
rendered = helpers.url.call({html: 'content', title: 'title', slug: 'slug'});
should.exist(rendered);
rendered.should.equal('/');
rendered = helpers.url.call({html: 'content', markdown: 'ff', slug: 'slug'});
should.exist(rendered);
rendered.should.equal('/');
rendered = helpers.url.call({html: 'content', markdown: 'ff', title: 'title'});
should.exist(rendered);
rendered.should.equal('/');
});
});