mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
Migrated body_class, content and date helpers to ES6 (#10644)
refs #9589 * updated body_class helper to use newer code standards * updated content helper to use newer code standards * updated date helper to use newer code standards
This commit is contained in:
parent
cea6b6c837
commit
bd77c1e2e0
3 changed files with 41 additions and 42 deletions
|
@ -2,11 +2,9 @@
|
|||
// Usage: `{{body_class}}`
|
||||
//
|
||||
// Output classes for the body element
|
||||
var proxy = require('./proxy'),
|
||||
_ = require('lodash'),
|
||||
SafeString = proxy.SafeString;
|
||||
const {SafeString} = require('./proxy');
|
||||
|
||||
// We use the name body_class to match the helper for consistency:
|
||||
// We use the name body_class to match the helper for consistency
|
||||
module.exports = function body_class(options) { // eslint-disable-line camelcase
|
||||
let classes = [];
|
||||
const context = options.data.root.context;
|
||||
|
@ -14,37 +12,35 @@ module.exports = function body_class(options) { // eslint-disable-line camelcase
|
|||
const tags = obj && obj.tags ? obj.tags : [];
|
||||
const isPage = !!(obj && obj.page);
|
||||
|
||||
if (_.includes(context, 'home')) {
|
||||
if (context.includes('home')) {
|
||||
classes.push('home-template');
|
||||
} else if (_.includes(context, 'post') && obj) {
|
||||
} else if (context.includes('post') && obj) {
|
||||
classes.push('post-template');
|
||||
} else if (_.includes(context, 'page') && obj && isPage) {
|
||||
} else if (context.includes('page') && obj && isPage) {
|
||||
classes.push('page-template');
|
||||
classes.push('page-' + obj.slug);
|
||||
} else if (_.includes(context, 'tag') && this.tag) {
|
||||
classes.push(`page-${obj.slug}`);
|
||||
} else if (context.includes('tag') && this.tag) {
|
||||
classes.push('tag-template');
|
||||
classes.push('tag-' + this.tag.slug);
|
||||
} else if (_.includes(context, 'author') && this.author) {
|
||||
classes.push(`tag-${this.tag.slug}`);
|
||||
} else if (context.includes('author') && this.author) {
|
||||
classes.push('author-template');
|
||||
classes.push('author-' + this.author.slug);
|
||||
} else if (_.includes(context, 'private')) {
|
||||
classes.push(`author-${this.author.slug}`);
|
||||
} else if (context.includes('private')) {
|
||||
classes.push('private-template');
|
||||
}
|
||||
|
||||
if (tags) {
|
||||
classes = classes.concat(tags.map(function (tag) {
|
||||
return 'tag-' + tag.slug;
|
||||
}));
|
||||
classes = classes.concat(
|
||||
tags.map(({slug}) => `tag-${slug}`)
|
||||
);
|
||||
}
|
||||
|
||||
if (_.includes(context, 'paged')) {
|
||||
if (context.includes('paged')) {
|
||||
classes.push('paged');
|
||||
}
|
||||
|
||||
classes = _.reduce(classes, function (memo, item) {
|
||||
return memo + ' ' + item;
|
||||
}, '');
|
||||
classes = classes.join(' ').trim();
|
||||
|
||||
return new SafeString(classes.trim());
|
||||
return new SafeString(classes);
|
||||
};
|
||||
|
||||
|
|
|
@ -6,23 +6,26 @@
|
|||
//
|
||||
// Enables tag-safe truncation of content by characters or words.
|
||||
|
||||
var proxy = require('./proxy'),
|
||||
_ = require('lodash'),
|
||||
downsize = require('downsize'),
|
||||
SafeString = proxy.SafeString;
|
||||
const {SafeString} = require('./proxy');
|
||||
const downsize = require('downsize');
|
||||
|
||||
module.exports = function content(options) {
|
||||
var truncateOptions = (options || {}).hash || {};
|
||||
truncateOptions = _.pick(truncateOptions, ['words', 'characters']);
|
||||
_.keys(truncateOptions).map(function (key) {
|
||||
truncateOptions[key] = parseInt(truncateOptions[key], 10);
|
||||
});
|
||||
module.exports = function content(options = {}) {
|
||||
const hash = options.hash || {};
|
||||
const truncateOptions = {};
|
||||
let runTruncate = false;
|
||||
|
||||
for (const key of ['words', 'characters']) {
|
||||
if (hash.hasOwnProperty(key)) {
|
||||
runTruncate = true;
|
||||
truncateOptions[key] = parseInt(hash[key], 10);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.html === null) {
|
||||
this.html = '';
|
||||
}
|
||||
|
||||
if (truncateOptions.hasOwnProperty('words') || truncateOptions.hasOwnProperty('characters')) {
|
||||
if (runTruncate) {
|
||||
return new SafeString(
|
||||
downsize(this.html, truncateOptions)
|
||||
);
|
||||
|
|
|
@ -3,13 +3,11 @@
|
|||
//
|
||||
// Formats a date using moment-timezone.js. Formats published_at by default but will also take a date as a parameter
|
||||
|
||||
var proxy = require('./proxy'),
|
||||
moment = require('moment-timezone'),
|
||||
SafeString = proxy.SafeString,
|
||||
i18n = proxy.i18n;
|
||||
const {SafeString, i18n} = require('./proxy');
|
||||
const moment = require('moment-timezone');
|
||||
|
||||
module.exports = function (date, options) {
|
||||
var timezone, format, timeago, timeNow, dateMoment;
|
||||
let timezone;
|
||||
|
||||
if (!options && date.hasOwnProperty('hash')) {
|
||||
options = date;
|
||||
|
@ -23,18 +21,20 @@ module.exports = function (date, options) {
|
|||
}
|
||||
}
|
||||
|
||||
const {
|
||||
format = 'MMM DD, YYYY',
|
||||
timeago
|
||||
} = options.hash;
|
||||
|
||||
// ensure that context is undefined, not null, as that can cause errors
|
||||
date = date === null ? undefined : date;
|
||||
|
||||
format = options.hash.format || 'MMM DD, YYYY';
|
||||
timeago = options.hash.timeago;
|
||||
timezone = options.data.blog.timezone;
|
||||
timeNow = moment().tz(timezone);
|
||||
const timeNow = moment().tz(timezone);
|
||||
|
||||
// i18n: Making dates, including month names, translatable to any language.
|
||||
// Documentation: http://momentjs.com/docs/#/i18n/
|
||||
// Locales: https://github.com/moment/moment/tree/develop/locale
|
||||
dateMoment = moment(date);
|
||||
const dateMoment = moment(date);
|
||||
dateMoment.locale(i18n.locale());
|
||||
|
||||
if (timeago) {
|
||||
|
|
Loading…
Reference in a new issue