2014-10-10 18:54:07 +04:00
|
|
|
// # Excerpt Helper
|
|
|
|
// Usage: `{{excerpt}}`, `{{excerpt words="50"}}`, `{{excerpt characters="256"}}`
|
|
|
|
//
|
|
|
|
// Attempts to remove all HTML from the string, and then shortens the result according to the provided option.
|
|
|
|
//
|
|
|
|
// Defaults to words="50"
|
|
|
|
|
2016-01-17 13:07:52 +03:00
|
|
|
var hbs = require('express-hbs'),
|
2016-02-21 21:48:44 +03:00
|
|
|
_ = require('lodash'),
|
2016-01-17 13:07:52 +03:00
|
|
|
getMetaDataExcerpt = require('../data/meta/excerpt');
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2016-01-17 13:07:52 +03:00
|
|
|
function excerpt(options) {
|
|
|
|
var truncateOptions = (options || {}).hash || {};
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
|
|
truncateOptions = _.pick(truncateOptions, ['words', 'characters']);
|
|
|
|
_.keys(truncateOptions).map(function (key) {
|
|
|
|
truncateOptions[key] = parseInt(truncateOptions[key], 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
return new hbs.handlebars.SafeString(
|
2016-01-17 13:07:52 +03:00
|
|
|
getMetaDataExcerpt(String(this.html), truncateOptions)
|
2014-10-10 18:54:07 +04:00
|
|
|
);
|
2016-01-17 13:07:52 +03:00
|
|
|
}
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
|
|
module.exports = excerpt;
|