Ghost/core/server/helpers/content.js
Vikas Potluri bd77c1e2e0 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
2019-04-02 08:36:13 +02:00

36 lines
969 B
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.
const {SafeString} = require('./proxy');
const downsize = require('downsize');
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 (runTruncate) {
return new SafeString(
downsize(this.html, truncateOptions)
);
}
return new SafeString(this.html);
};