2014-10-10 18:54:07 +04:00
|
|
|
// # 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.
|
|
|
|
|
2019-04-02 09:36:13 +03:00
|
|
|
const {SafeString} = require('./proxy');
|
|
|
|
const downsize = require('downsize');
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2019-04-02 09:36:13 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2019-03-18 14:46:59 +03:00
|
|
|
if (this.html === null) {
|
|
|
|
this.html = '';
|
|
|
|
}
|
|
|
|
|
2019-04-02 09:36:13 +03:00
|
|
|
if (runTruncate) {
|
2017-04-04 19:07:35 +03:00
|
|
|
return new SafeString(
|
2014-10-10 18:54:07 +04:00
|
|
|
downsize(this.html, truncateOptions)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
return new SafeString(this.html);
|
2014-10-10 18:54:07 +04:00
|
|
|
};
|