2014-10-10 18:54:07 +04:00
|
|
|
// # Body Class Helper
|
|
|
|
// Usage: `{{body_class}}`
|
|
|
|
//
|
|
|
|
// Output classes for the body element
|
2021-09-28 17:06:33 +03:00
|
|
|
const {SafeString} = require('../services/rendering');
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2019-04-02 09:36:13 +03:00
|
|
|
// We use the name body_class to match the helper for consistency
|
2017-11-01 16:44:54 +03:00
|
|
|
module.exports = function body_class(options) { // eslint-disable-line camelcase
|
2019-03-12 03:18:07 +03:00
|
|
|
let classes = [];
|
2019-04-04 16:41:56 +03:00
|
|
|
const context = options.data.root.context || [];
|
2019-03-12 03:18:07 +03:00
|
|
|
const obj = this.post || this.page;
|
|
|
|
const tags = obj && obj.tags ? obj.tags : [];
|
|
|
|
const isPage = !!(obj && obj.page);
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2019-04-02 09:36:13 +03:00
|
|
|
if (context.includes('home')) {
|
2015-05-14 13:31:43 +03:00
|
|
|
classes.push('home-template');
|
2019-04-02 09:36:13 +03:00
|
|
|
} else if (context.includes('post') && obj) {
|
2016-10-10 12:38:12 +03:00
|
|
|
classes.push('post-template');
|
2019-04-02 09:36:13 +03:00
|
|
|
} else if (context.includes('page') && obj && isPage) {
|
2014-10-10 18:54:07 +04:00
|
|
|
classes.push('page-template');
|
2019-04-02 09:36:13 +03:00
|
|
|
classes.push(`page-${obj.slug}`);
|
|
|
|
} else if (context.includes('tag') && this.tag) {
|
2015-05-14 13:31:43 +03:00
|
|
|
classes.push('tag-template');
|
2019-04-02 09:36:13 +03:00
|
|
|
classes.push(`tag-${this.tag.slug}`);
|
|
|
|
} else if (context.includes('author') && this.author) {
|
2015-05-14 13:31:43 +03:00
|
|
|
classes.push('author-template');
|
2019-04-02 09:36:13 +03:00
|
|
|
classes.push(`author-${this.author.slug}`);
|
|
|
|
} else if (context.includes('private')) {
|
2015-05-14 13:31:43 +03:00
|
|
|
classes.push('private-template');
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tags) {
|
2019-04-02 09:36:13 +03:00
|
|
|
classes = classes.concat(
|
|
|
|
tags.map(({slug}) => `tag-${slug}`)
|
|
|
|
);
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:36:13 +03:00
|
|
|
if (context.includes('paged')) {
|
2015-05-14 13:31:43 +03:00
|
|
|
classes.push('paged');
|
2015-11-24 18:12:50 +03:00
|
|
|
}
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2019-04-02 09:36:13 +03:00
|
|
|
classes = classes.join(' ').trim();
|
2019-03-12 03:18:07 +03:00
|
|
|
|
2019-04-02 09:36:13 +03:00
|
|
|
return new SafeString(classes);
|
2014-10-10 18:54:07 +04:00
|
|
|
};
|