0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Merge pull request #4726 from jaswilli/issue-3961

Change {{url}} and {{image}} into synchronous helpers
This commit is contained in:
Hannah Wolfe 2014-12-30 19:20:51 +00:00
commit 4f278e3cf7
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 // 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. // `absolute` flag outputs absolute URL, else URL is relative.
var Promise = require('bluebird'), var config = require('../config'),
config = require('../config'),
image; image;
image = function (options) { image = function (options) {
var absolute = options && options.hash.absolute; var absolute = options && options.hash.absolute;
if (this.image) { if (this.image) {
return Promise.resolve(config.urlFor('image', {image: this.image}, absolute)); return config.urlFor('image', {image: this.image}, absolute);
} else {
return Promise.resolve();
} }
}; };

View file

@ -94,6 +94,8 @@ registerHelpers = function (adminHbs) {
registerThemeHelper('pagination', coreHelpers.pagination); registerThemeHelper('pagination', coreHelpers.pagination);
registerThemeHelper('tags', coreHelpers.tags); registerThemeHelper('tags', coreHelpers.tags);
registerThemeHelper('plural', coreHelpers.plural); registerThemeHelper('plural', coreHelpers.plural);
registerThemeHelper('url', coreHelpers.url);
registerThemeHelper('image', coreHelpers.image);
// Async theme helpers // Async theme helpers
registerAsyncThemeHelper('body_class', coreHelpers.body_class); registerAsyncThemeHelper('body_class', coreHelpers.body_class);
@ -102,8 +104,6 @@ registerHelpers = function (adminHbs) {
registerAsyncThemeHelper('meta_description', coreHelpers.meta_description); registerAsyncThemeHelper('meta_description', coreHelpers.meta_description);
registerAsyncThemeHelper('meta_title', coreHelpers.meta_title); registerAsyncThemeHelper('meta_title', coreHelpers.meta_title);
registerAsyncThemeHelper('post_class', coreHelpers.post_class); registerAsyncThemeHelper('post_class', coreHelpers.post_class);
registerAsyncThemeHelper('url', coreHelpers.url);
registerAsyncThemeHelper('image', coreHelpers.image);
// Register admin helpers // Register admin helpers
registerAdminHelper('ghost_script_tags', coreHelpers.ghost_script_tags); 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 // 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 // `absolute` flag outputs absolute URL, else URL is relative
var Promise = require('bluebird'), var config = require('../config'),
config = require('../config'),
schema = require('../data/schema').checks, schema = require('../data/schema').checks,
url; url;
@ -13,18 +12,18 @@ url = function (options) {
var absolute = options && options.hash.absolute; var absolute = options && options.hash.absolute;
if (schema.isPost(this)) { if (schema.isPost(this)) {
return Promise.resolve(config.urlFor('post', {post: this}, absolute)); return config.urlFor('post', {post: this}, absolute);
} }
if (schema.isTag(this)) { if (schema.isTag(this)) {
return Promise.resolve(config.urlFor('tag', {tag: this}, absolute)); return config.urlFor('tag', {tag: this}, absolute);
} }
if (schema.isUser(this)) { 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; module.exports = url;

View file

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

View file

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