diff --git a/core/server/helpers/body_class.js b/core/server/helpers/body_class.js index 1764e5f50e..546b45f3e9 100644 --- a/core/server/helpers/body_class.js +++ b/core/server/helpers/body_class.js @@ -14,44 +14,45 @@ var hbs = require('express-hbs'), template = require('./template'), body_class; -body_class = function () { +body_class = function (options) { var classes = [], + context = options.data.root.context, post = this.post, tags = this.post && this.post.tags ? this.post.tags : this.tags || [], page = this.post && this.post.page ? this.post.page : this.page || false; - if (this.tag !== undefined) { - classes.push('tag-template'); - classes.push('tag-' + this.tag.slug); - } - - if (this.author !== undefined) { - classes.push('author-template'); - classes.push('author-' + this.author.slug); - } - - if (_.isString(this.relativeUrl) && this.relativeUrl.match(/\/(page\/\d)/)) { - classes.push('paged'); + if (post) { // To be removed from pages by #2597 when we're ready to deprecate this - classes.push('archive-template'); - } else if (!this.relativeUrl || this.relativeUrl === '/' || this.relativeUrl === '') { - classes.push('home-template'); - } else if (post) { - // To be removed from pages by #2597 when we're ready to deprecate this - // i.e. this should be if (post && !page) { ... } + // i.e. this should be if (_.contains(context, 'post') && post) { ... } classes.push('post-template'); } - if (page) { + if (_.contains(context, 'home')) { + classes.push('home-template'); + } else if (_.contains(context, 'page') && page) { classes.push('page-template'); // To be removed by #2597 when we're ready to deprecate this classes.push('page'); + } else if (_.contains(context, 'tag') && this.tag) { + classes.push('tag-template'); + classes.push('tag-' + this.tag.slug); + } else if (_.contains(context, 'author') && this.author) { + classes.push('author-template'); + classes.push('author-' + this.author.slug); + } else if (_.contains(context, 'private')) { + classes.push('private-template'); } if (tags) { classes = classes.concat(tags.map(function (tag) { return 'tag-' + tag.slug; })); } + if (_.contains(context, 'paged')) { + classes.push('paged'); + // To be removed from pages by #2597 when we're ready to deprecate this + classes.push('archive-template'); + } + return api.settings.read({context: {internal: true}, key: 'activeTheme'}).then(function (response) { var activeTheme = response.settings[0], paths = config.paths.availableThemes[activeTheme.value], diff --git a/core/server/helpers/meta_title.js b/core/server/helpers/meta_title.js index eca9b0595f..9b8dfb06f8 100644 --- a/core/server/helpers/meta_title.js +++ b/core/server/helpers/meta_title.js @@ -31,7 +31,7 @@ meta_title = function (options) { title = this.author.name + pageString + ' - ' + blog.title; } else if (_.contains(context, 'tag') && this.tag) { title = this.tag.meta_title || this.tag.name + pageString + ' - ' + blog.title; - } else if (_.contains(context, 'post') && this.post) { + } else if ((_.contains(context, 'post') || _.contains(context, 'page')) && this.post) { title = this.post.meta_title || this.post.title; } else { title = blog.title + pageString; diff --git a/core/test/unit/server_helpers/body_class_spec.js b/core/test/unit/server_helpers/body_class_spec.js index bb689de1bb..0156eb7193 100644 --- a/core/test/unit/server_helpers/body_class_spec.js +++ b/core/test/unit/server_helpers/body_class_spec.js @@ -46,7 +46,7 @@ describe('{{body_class}} helper', function () { }); it('can render class string', function (done) { - helpers.body_class.call({}).then(function (rendered) { + helpers.body_class.call({}, {data: {root: {context: ['home']}}}).then(function (rendered) { should.exist(rendered); rendered.string.should.equal('home-template'); @@ -57,15 +57,53 @@ describe('{{body_class}} helper', function () { it('can render class string for context', function (done) { Promise.all([ - helpers.body_class.call({relativeUrl: '/'}), - helpers.body_class.call({relativeUrl: '/a-post-title', post: {}}), - helpers.body_class.call({relativeUrl: '/page/4'}), - helpers.body_class.call({relativeUrl: '/tag/foo', tag: {slug: 'foo'}}), - helpers.body_class.call({relativeUrl: '/tag/foo/page/2', tag: {slug: 'foo'}}), - helpers.body_class.call({relativeUrl: '/author/bar', author: {slug: 'bar'}}), - helpers.body_class.call({relativeUrl: '/author/bar/page/2', author: {slug: 'bar'}}) + // Standard home page + helpers.body_class.call( + {relativeUrl: '/'}, + {data: {root: {context: ['home', 'index']}}} + ), + // A post + helpers.body_class.call( + {relativeUrl: '/a-post-title', post: {}}, + {data: {root: {context: ['post']}}} + ), + // Paginated index + helpers.body_class.call( + {relativeUrl: '/page/4'}, + {data: {root: {context: ['index', 'paged']}}} + ), + // Tag page + helpers.body_class.call( + {relativeUrl: '/tag/foo', tag: {slug: 'foo'}}, + {data: {root: {context: ['tag']}}} + ), + // Paginated tag page + helpers.body_class.call( + {relativeUrl: '/tag/foo/page/2', tag: {slug: 'foo'}}, + {data: {root: {context: ['tag', 'paged']}}} + ), + // Author page + helpers.body_class.call( + {relativeUrl: '/author/bar', author: {slug: 'bar'}}, + {data: {root: {context: ['author']}}} + ), + // Paginated author page + helpers.body_class.call( + {relativeUrl: '/author/bar/page/2', author: {slug: 'bar'}}, + {data: {root: {context: ['author', 'paged']}}} + ), + // Private route for password protection + helpers.body_class.call( + {relativeUrl: '/private/'}, + {data: {root: {context: ['private']}}} + ), + // Post with tags + helpers.body_class.call( + {relativeUrl: '/my-awesome-post/', post: {tags: [{slug: 'foo'}, {slug: 'bar'}]}}, + {data: {root: {context: ['post']}}} + ) ]).then(function (rendered) { - rendered.length.should.equal(7); + rendered.length.should.equal(9); should.exist(rendered[0]); should.exist(rendered[1]); @@ -74,6 +112,8 @@ describe('{{body_class}} helper', function () { should.exist(rendered[4]); should.exist(rendered[5]); should.exist(rendered[6]); + should.exist(rendered[7]); + should.exist(rendered[8]); rendered[0].string.should.equal('home-template'); rendered[1].string.should.equal('post-template'); @@ -82,34 +122,40 @@ describe('{{body_class}} helper', function () { rendered[4].string.should.equal('tag-template tag-foo paged archive-template'); rendered[5].string.should.equal('author-template author-bar'); rendered[6].string.should.equal('author-template author-bar paged archive-template'); + rendered[7].string.should.equal('private-template'); + rendered[8].string.should.equal('post-template tag-foo tag-bar'); done(); }).catch(done); }); it('can render class for static page', function (done) { - helpers.body_class.call({ - relativeUrl: '/', - post: { - page: true - } - }).then(function (rendered) { + helpers.body_class.call( + { + relativeUrl: '/about', + post: { + page: true + } + }, + {data: {root: {context: ['page']}}} + ).then(function (rendered) { should.exist(rendered); - rendered.string.should.equal('home-template page-template page'); + rendered.string.should.equal('post-template page-template page'); done(); }).catch(done); }); it('can render class for static page with custom template', function (done) { - helpers.body_class.call({ - relativeUrl: '/about', - post: { - page: true, - slug: 'about' - - } - }).then(function (rendered) { + helpers.body_class.call( + { + relativeUrl: '/about', + post: { + page: true, + slug: 'about' + } + }, + {data: {root: {context: ['page']}}}).then(function (rendered) { should.exist(rendered); rendered.string.should.equal('post-template page-template page page-about page-template-about'); diff --git a/core/test/unit/server_helpers/is_spec.js b/core/test/unit/server_helpers/is_spec.js index 98f19e487e..255ecafd62 100644 --- a/core/test/unit/server_helpers/is_spec.js +++ b/core/test/unit/server_helpers/is_spec.js @@ -7,7 +7,8 @@ var should = require('should'), // Stuff we are testing handlebars = hbs.handlebars, - helpers = require('../../../server/helpers'); + helpers = require('../../../server/helpers'), + errors = require('../../../server/errors'); describe('{{#is}} helper', function () { before(function () { @@ -60,4 +61,20 @@ describe('{{#is}} helper', function () { fn.called.should.be.false; inverse.called.should.be.true; }); + + it('should log warning with no args', function () { + var fn = sinon.spy(), + inverse = sinon.spy(), + logWarn = sinon.stub(errors, 'logWarn'); + + helpers.is.call( + {}, + undefined, + {fn: fn, inverse: inverse, data: {root: {context: ['index', 'home']}}} + ); + + logWarn.called.should.be.true; + fn.called.should.be.false; + inverse.called.should.be.false; + }); }); diff --git a/core/test/unit/server_helpers/meta_title_spec.js b/core/test/unit/server_helpers/meta_title_spec.js index 02787eb1c3..8e40224b99 100644 --- a/core/test/unit/server_helpers/meta_title_spec.js +++ b/core/test/unit/server_helpers/meta_title_spec.js @@ -74,6 +74,18 @@ describe('{{meta_title}} helper', function () { }).catch(done); }); + it('returns correct title for a page with meta_title set', function (done) { + helpers.meta_title.call( + {post: {title: 'About Page', meta_title: 'All about my awesomeness', page: true}}, + {data: {root: {context: ['page']}}} + ).then(function (rendered) { + should.exist(rendered); + String(rendered).should.equal('All about my awesomeness'); + + done(); + }).catch(done); + }); + it('returns correct title for a tag page', function (done) { var tag = {relativeUrl: '/tag/rasper-red', tag: {name: 'Rasper Red'}}; helpers.meta_title.call(