mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 18:31:57 +03:00
b4140d4310
refs https://github.com/TryGhost/Casper/pull/741 closes https://github.com/TryGhost/Team/issues/524 - Use a local-based format as the default format as suggested in https://github.com/TryGhost/Casper/pull/741 - reworked the helper to be easier to read and follow the different use cases - introduced setting and resetting locale in tests via settingsCache and themei18n - updated tests to cover more cases e.g. passing a date, this.published_at and no date - added validation for user inputted dates because they could literally be anything Co-authored-by: Hannah Wolfe <erisds@gmail.com>
57 lines
1.8 KiB
JavaScript
57 lines
1.8 KiB
JavaScript
// # Date Helper
|
|
// Usage: `{{date format="DD MM, YYYY"}}`, `{{date updated_at format="DD MM, YYYY"}}`
|
|
//
|
|
// Formats a date using moment-timezone.js. Formats published_at by default but will also take a date as a parameter
|
|
|
|
const {SafeString, themeI18n} = require('../services/proxy');
|
|
const moment = require('moment-timezone');
|
|
const _ = require('lodash');
|
|
|
|
module.exports = function (...attrs) {
|
|
// Options is the last argument
|
|
const options = attrs.pop();
|
|
let date;
|
|
|
|
// If there is any more arguments, date is the first one
|
|
if (!_.isEmpty(attrs)) {
|
|
date = attrs.shift();
|
|
|
|
// If there is no date argument & the current context contains published_at use that by default,
|
|
// else date being undefined means moment will use the current date
|
|
} else if (this.published_at) {
|
|
date = this.published_at;
|
|
}
|
|
|
|
// ensure that date is undefined, not null, as that can cause errors
|
|
date = date === null ? undefined : date;
|
|
|
|
const timezone = options.data.site.timezone;
|
|
const {
|
|
format = 'll',
|
|
timeago
|
|
} = options.hash;
|
|
|
|
const timeNow = moment().tz(timezone);
|
|
// Our date might be user input
|
|
let testDateInput = Date.parse(date);
|
|
let dateMoment;
|
|
if (isNaN(testDateInput) === false) {
|
|
dateMoment = moment.parseZone(date);
|
|
} else {
|
|
dateMoment = timeNow;
|
|
}
|
|
|
|
// 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.locale(themeI18n.locale());
|
|
|
|
if (timeago) {
|
|
date = dateMoment.tz(timezone).from(timeNow);
|
|
} else {
|
|
date = dateMoment.tz(timezone).format(format);
|
|
}
|
|
|
|
return new SafeString(date);
|
|
};
|