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.
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
var proxy = require('./proxy'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
downsize = require('downsize'),
|
|
|
|
SafeString = proxy.SafeString;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
module.exports = function content(options) {
|
2014-10-10 18:54:07 +04:00
|
|
|
var truncateOptions = (options || {}).hash || {};
|
|
|
|
truncateOptions = _.pick(truncateOptions, ['words', 'characters']);
|
|
|
|
_.keys(truncateOptions).map(function (key) {
|
|
|
|
truncateOptions[key] = parseInt(truncateOptions[key], 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (truncateOptions.hasOwnProperty('words') || truncateOptions.hasOwnProperty('characters')) {
|
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
|
|
|
};
|