2014-10-10 09:54:07 -05:00
|
|
|
// # Body Class Helper
|
|
|
|
// Usage: `{{body_class}}`
|
|
|
|
//
|
|
|
|
// Output classes for the body element
|
2017-04-04 11:07:35 -05:00
|
|
|
var proxy = require('./proxy'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
SafeString = proxy.SafeString;
|
2014-10-10 09:54:07 -05:00
|
|
|
|
2017-11-01 08:44:54 -05:00
|
|
|
// We use the name body_class to match the helper for consistency:
|
|
|
|
module.exports = function body_class(options) { // eslint-disable-line camelcase
|
2019-03-11 19:18:07 -05:00
|
|
|
let classes = [];
|
|
|
|
const context = options.data.root.context;
|
|
|
|
const obj = this.post || this.page;
|
|
|
|
const tags = obj && obj.tags ? obj.tags : [];
|
|
|
|
const isPage = !!(obj && obj.page);
|
2014-10-10 09:54:07 -05:00
|
|
|
|
2016-06-11 13:23:27 -05:00
|
|
|
if (_.includes(context, 'home')) {
|
2015-05-14 05:31:43 -05:00
|
|
|
classes.push('home-template');
|
2019-03-11 19:18:07 -05:00
|
|
|
} else if (_.includes(context, 'post') && obj) {
|
2016-10-10 04:38:12 -05:00
|
|
|
classes.push('post-template');
|
2019-03-11 19:18:07 -05:00
|
|
|
} else if (_.includes(context, 'page') && obj && isPage) {
|
2014-10-10 09:54:07 -05:00
|
|
|
classes.push('page-template');
|
2019-03-11 19:18:07 -05:00
|
|
|
classes.push('page-' + obj.slug);
|
2016-06-11 13:23:27 -05:00
|
|
|
} else if (_.includes(context, 'tag') && this.tag) {
|
2015-05-14 05:31:43 -05:00
|
|
|
classes.push('tag-template');
|
|
|
|
classes.push('tag-' + this.tag.slug);
|
2016-06-11 13:23:27 -05:00
|
|
|
} else if (_.includes(context, 'author') && this.author) {
|
2015-05-14 05:31:43 -05:00
|
|
|
classes.push('author-template');
|
|
|
|
classes.push('author-' + this.author.slug);
|
2016-06-11 13:23:27 -05:00
|
|
|
} else if (_.includes(context, 'private')) {
|
2015-05-14 05:31:43 -05:00
|
|
|
classes.push('private-template');
|
2014-10-10 09:54:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tags) {
|
2018-09-17 13:49:30 -05:00
|
|
|
classes = classes.concat(tags.map(function (tag) {
|
2019-03-11 19:18:07 -05:00
|
|
|
return 'tag-' + tag.slug;
|
2018-09-17 13:49:30 -05:00
|
|
|
}));
|
2014-10-10 09:54:07 -05:00
|
|
|
}
|
|
|
|
|
2016-06-11 13:23:27 -05:00
|
|
|
if (_.includes(context, 'paged')) {
|
2015-05-14 05:31:43 -05:00
|
|
|
classes.push('paged');
|
2015-11-24 10:12:50 -05:00
|
|
|
}
|
2014-10-10 09:54:07 -05:00
|
|
|
|
2018-09-17 13:49:30 -05:00
|
|
|
classes = _.reduce(classes, function (memo, item) {
|
2019-03-11 19:18:07 -05:00
|
|
|
return memo + ' ' + item;
|
2018-09-17 13:49:30 -05:00
|
|
|
}, '');
|
2019-03-11 19:18:07 -05:00
|
|
|
|
2017-04-04 11:07:35 -05:00
|
|
|
return new SafeString(classes.trim());
|
2014-10-10 09:54:07 -05:00
|
|
|
};
|
|
|
|
|