0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/core/server/helpers/content.js
Eol 9a21bea2c7 🐛 Fixed null displayed on empty post content (#10617)
refs #10612

- Added `null` value handling to {{content}} helper, which is same to how {{exerpt}} helper handles `null` values at the moment
2019-03-18 19:46:59 +08:00

32 lines
1 KiB
JavaScript

// # Content Helper
// Usage: `{{content}}`, `{{content words="20"}}`, `{{content characters="256"}}`
//
// Turns content html into a safestring so that the user doesn't have to
// escape it or tell handlebars to leave it alone with a triple-brace.
//
// Enables tag-safe truncation of content by characters or words.
var proxy = require('./proxy'),
_ = require('lodash'),
downsize = require('downsize'),
SafeString = proxy.SafeString;
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);
});
if (this.html === null) {
this.html = '';
}
if (truncateOptions.hasOwnProperty('words') || truncateOptions.hasOwnProperty('characters')) {
return new SafeString(
downsize(this.html, truncateOptions)
);
}
return new SafeString(this.html);
};