Ghost/core/frontend/helpers/date.js
Naz Gargol a9050f68ea
🔥 Removed V1 code/references in frontend helpers/meta layers (#11080)
no issue

- Removed deprecated 'blog' reference from frontend data. The alias (site->blog) stays till next version (v4) as it's not leaving much of technical debt but would ease the migration process for anybody still using it. 
- The follow up to this is substitute of all references to `options.data.blog` with `options.data.site` in "frontend"
- Fixed test utils helper to use `site` instead of `blog`
- Removed 0.1 flag checks in {{get}} helper
- Removed user aliasing from {{get}} helper
- Removed unused translation for {{get}} helper
- Added a note to excerpt changes in metadata for future reference
- Removed page alias used in description helper. The mix of page context with post object in the metadata was only possible in v0.1
- Changed mock in ghost_head helper to use v2
- Removed unneeded test for body class helper
2019-09-10 11:37:04 +02:00

48 lines
1.6 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, i18n} = require('./proxy');
const moment = require('moment-timezone');
module.exports = function (date, options) {
let timezone;
if (!options && Object.prototype.hasOwnProperty.call(date, 'hash')) {
options = date;
date = undefined;
timezone = options.data.site.timezone;
// set to published_at by default, if it's available
// otherwise, this will print the current date
if (this.published_at) {
date = moment(this.published_at).tz(timezone).format();
}
}
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;
timezone = options.data.site.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
const dateMoment = moment(date);
dateMoment.locale(i18n.locale());
if (timeago) {
date = timezone ? dateMoment.tz(timezone).from(timeNow) : dateMoment.fromNow();
} else {
date = timezone ? dateMoment.tz(timezone).format(format) : dateMoment.format(format);
}
return new SafeString(date);
};