From 0dc0d37888a63ff001a1912bb4346055f15baf34 Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Sun, 28 Dec 2014 19:38:29 +0000 Subject: [PATCH] Change url and image into synchronous helpers Closes #3961 --- core/server/helpers/image.js | 8 +- core/server/helpers/index.js | 4 +- core/server/helpers/url.js | 11 ++- core/test/unit/server_helpers/image_spec.js | 93 ++++++++++----------- core/test/unit/server_helpers/url_spec.js | 73 ++++++++-------- 5 files changed, 91 insertions(+), 98 deletions(-) diff --git a/core/server/helpers/image.js b/core/server/helpers/image.js index 7b14db0e28..6e57843044 100644 --- a/core/server/helpers/image.js +++ b/core/server/helpers/image.js @@ -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); } }; diff --git a/core/server/helpers/index.js b/core/server/helpers/index.js index 3f560d29df..2d4a355ec0 100644 --- a/core/server/helpers/index.js +++ b/core/server/helpers/index.js @@ -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); diff --git a/core/server/helpers/url.js b/core/server/helpers/url.js index ee291e2844..74de3cdd0b 100644 --- a/core/server/helpers/url.js +++ b/core/server/helpers/url.js @@ -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; diff --git a/core/test/unit/server_helpers/image_spec.js b/core/test/unit/server_helpers/image_spec.js index f1cdbf7086..0e64685bb4 100644 --- a/core/test/unit/server_helpers/image_spec.js +++ b/core/test/unit/server_helpers/image_spec.js @@ -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'); }); }); diff --git a/core/test/unit/server_helpers/url_spec.js b/core/test/unit/server_helpers/url_spec.js index 22238cc556..fcd215a987 100644 --- a/core/test/unit/server_helpers/url_spec.js +++ b/core/test/unit/server_helpers/url_spec.js @@ -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('/'); }); });