mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Misc helper updates & fixes
refs #5205 - Ensure that pages are treated the same as posts in meta_title, add test - Added a private-template body class for the private context, add test - Added a missing test to is_spec.js
This commit is contained in:
parent
1f0fb3c5f3
commit
d11a91e75c
5 changed files with 122 additions and 46 deletions
|
@ -14,44 +14,45 @@ var hbs = require('express-hbs'),
|
||||||
template = require('./template'),
|
template = require('./template'),
|
||||||
body_class;
|
body_class;
|
||||||
|
|
||||||
body_class = function () {
|
body_class = function (options) {
|
||||||
var classes = [],
|
var classes = [],
|
||||||
|
context = options.data.root.context,
|
||||||
post = this.post,
|
post = this.post,
|
||||||
tags = this.post && this.post.tags ? this.post.tags : this.tags || [],
|
tags = this.post && this.post.tags ? this.post.tags : this.tags || [],
|
||||||
page = this.post && this.post.page ? this.post.page : this.page || false;
|
page = this.post && this.post.page ? this.post.page : this.page || false;
|
||||||
|
|
||||||
if (this.tag !== undefined) {
|
if (post) {
|
||||||
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');
|
|
||||||
// To be removed from pages by #2597 when we're ready to deprecate this
|
// To be removed from pages by #2597 when we're ready to deprecate this
|
||||||
classes.push('archive-template');
|
// i.e. this should be if (_.contains(context, 'post') && post) { ... }
|
||||||
} 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) { ... }
|
|
||||||
classes.push('post-template');
|
classes.push('post-template');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (page) {
|
if (_.contains(context, 'home')) {
|
||||||
|
classes.push('home-template');
|
||||||
|
} else if (_.contains(context, 'page') && page) {
|
||||||
classes.push('page-template');
|
classes.push('page-template');
|
||||||
// To be removed by #2597 when we're ready to deprecate this
|
// To be removed by #2597 when we're ready to deprecate this
|
||||||
classes.push('page');
|
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) {
|
if (tags) {
|
||||||
classes = classes.concat(tags.map(function (tag) { return 'tag-' + tag.slug; }));
|
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) {
|
return api.settings.read({context: {internal: true}, key: 'activeTheme'}).then(function (response) {
|
||||||
var activeTheme = response.settings[0],
|
var activeTheme = response.settings[0],
|
||||||
paths = config.paths.availableThemes[activeTheme.value],
|
paths = config.paths.availableThemes[activeTheme.value],
|
||||||
|
|
|
@ -31,7 +31,7 @@ meta_title = function (options) {
|
||||||
title = this.author.name + pageString + ' - ' + blog.title;
|
title = this.author.name + pageString + ' - ' + blog.title;
|
||||||
} else if (_.contains(context, 'tag') && this.tag) {
|
} else if (_.contains(context, 'tag') && this.tag) {
|
||||||
title = this.tag.meta_title || this.tag.name + pageString + ' - ' + blog.title;
|
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;
|
title = this.post.meta_title || this.post.title;
|
||||||
} else {
|
} else {
|
||||||
title = blog.title + pageString;
|
title = blog.title + pageString;
|
||||||
|
|
|
@ -46,7 +46,7 @@ describe('{{body_class}} helper', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can render class string', function (done) {
|
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);
|
should.exist(rendered);
|
||||||
|
|
||||||
rendered.string.should.equal('home-template');
|
rendered.string.should.equal('home-template');
|
||||||
|
@ -57,15 +57,53 @@ describe('{{body_class}} helper', function () {
|
||||||
|
|
||||||
it('can render class string for context', function (done) {
|
it('can render class string for context', function (done) {
|
||||||
Promise.all([
|
Promise.all([
|
||||||
helpers.body_class.call({relativeUrl: '/'}),
|
// Standard home page
|
||||||
helpers.body_class.call({relativeUrl: '/a-post-title', post: {}}),
|
helpers.body_class.call(
|
||||||
helpers.body_class.call({relativeUrl: '/page/4'}),
|
{relativeUrl: '/'},
|
||||||
helpers.body_class.call({relativeUrl: '/tag/foo', tag: {slug: 'foo'}}),
|
{data: {root: {context: ['home', 'index']}}}
|
||||||
helpers.body_class.call({relativeUrl: '/tag/foo/page/2', tag: {slug: 'foo'}}),
|
),
|
||||||
helpers.body_class.call({relativeUrl: '/author/bar', author: {slug: 'bar'}}),
|
// A post
|
||||||
helpers.body_class.call({relativeUrl: '/author/bar/page/2', author: {slug: 'bar'}})
|
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) {
|
]).then(function (rendered) {
|
||||||
rendered.length.should.equal(7);
|
rendered.length.should.equal(9);
|
||||||
|
|
||||||
should.exist(rendered[0]);
|
should.exist(rendered[0]);
|
||||||
should.exist(rendered[1]);
|
should.exist(rendered[1]);
|
||||||
|
@ -74,6 +112,8 @@ describe('{{body_class}} helper', function () {
|
||||||
should.exist(rendered[4]);
|
should.exist(rendered[4]);
|
||||||
should.exist(rendered[5]);
|
should.exist(rendered[5]);
|
||||||
should.exist(rendered[6]);
|
should.exist(rendered[6]);
|
||||||
|
should.exist(rendered[7]);
|
||||||
|
should.exist(rendered[8]);
|
||||||
|
|
||||||
rendered[0].string.should.equal('home-template');
|
rendered[0].string.should.equal('home-template');
|
||||||
rendered[1].string.should.equal('post-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[4].string.should.equal('tag-template tag-foo paged archive-template');
|
||||||
rendered[5].string.should.equal('author-template author-bar');
|
rendered[5].string.should.equal('author-template author-bar');
|
||||||
rendered[6].string.should.equal('author-template author-bar paged archive-template');
|
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();
|
done();
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can render class for static page', function (done) {
|
it('can render class for static page', function (done) {
|
||||||
helpers.body_class.call({
|
helpers.body_class.call(
|
||||||
relativeUrl: '/',
|
{
|
||||||
post: {
|
relativeUrl: '/about',
|
||||||
page: true
|
post: {
|
||||||
}
|
page: true
|
||||||
}).then(function (rendered) {
|
}
|
||||||
|
},
|
||||||
|
{data: {root: {context: ['page']}}}
|
||||||
|
).then(function (rendered) {
|
||||||
should.exist(rendered);
|
should.exist(rendered);
|
||||||
rendered.string.should.equal('home-template page-template page');
|
rendered.string.should.equal('post-template page-template page');
|
||||||
|
|
||||||
done();
|
done();
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can render class for static page with custom template', function (done) {
|
it('can render class for static page with custom template', function (done) {
|
||||||
helpers.body_class.call({
|
helpers.body_class.call(
|
||||||
relativeUrl: '/about',
|
{
|
||||||
post: {
|
relativeUrl: '/about',
|
||||||
page: true,
|
post: {
|
||||||
slug: 'about'
|
page: true,
|
||||||
|
slug: 'about'
|
||||||
}
|
}
|
||||||
}).then(function (rendered) {
|
},
|
||||||
|
{data: {root: {context: ['page']}}}).then(function (rendered) {
|
||||||
should.exist(rendered);
|
should.exist(rendered);
|
||||||
rendered.string.should.equal('post-template page-template page page-about page-template-about');
|
rendered.string.should.equal('post-template page-template page page-about page-template-about');
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,8 @@ var should = require('should'),
|
||||||
|
|
||||||
// Stuff we are testing
|
// Stuff we are testing
|
||||||
handlebars = hbs.handlebars,
|
handlebars = hbs.handlebars,
|
||||||
helpers = require('../../../server/helpers');
|
helpers = require('../../../server/helpers'),
|
||||||
|
errors = require('../../../server/errors');
|
||||||
|
|
||||||
describe('{{#is}} helper', function () {
|
describe('{{#is}} helper', function () {
|
||||||
before(function () {
|
before(function () {
|
||||||
|
@ -60,4 +61,20 @@ describe('{{#is}} helper', function () {
|
||||||
fn.called.should.be.false;
|
fn.called.should.be.false;
|
||||||
inverse.called.should.be.true;
|
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;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -74,6 +74,18 @@ describe('{{meta_title}} helper', function () {
|
||||||
}).catch(done);
|
}).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) {
|
it('returns correct title for a tag page', function (done) {
|
||||||
var tag = {relativeUrl: '/tag/rasper-red', tag: {name: 'Rasper Red'}};
|
var tag = {relativeUrl: '/tag/rasper-red', tag: {name: 'Rasper Red'}};
|
||||||
helpers.meta_title.call(
|
helpers.meta_title.call(
|
||||||
|
|
Loading…
Add table
Reference in a new issue