Ghost/core/server/helpers/excerpt.js
Aileen Nowak a63c26a82b 🐛 Fixed custom post excerpt length (#8826)
closes #8823

- if a custom post excerpt is set, the default max length of 50 was used, which was wrong
- adds more tests
2017-08-02 15:09:12 +04:00

33 lines
1.1 KiB
JavaScript

// # 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"
var proxy = require('./proxy'),
_ = require('lodash'),
SafeString = proxy.SafeString,
getMetaDataExcerpt = proxy.metaData.getMetaDataExcerpt;
module.exports = function excerpt(options) {
var truncateOptions = (options || {}).hash || {},
excerptText = this.custom_excerpt ? String(this.custom_excerpt) : String(this.html);
truncateOptions = _.pick(truncateOptions, ['words', 'characters']);
_.keys(truncateOptions).map(function (key) {
truncateOptions[key] = parseInt(truncateOptions[key], 10);
});
if (!_.isEmpty(this.custom_excerpt)) {
truncateOptions.characters = this.custom_excerpt.length;
if (truncateOptions.words) {
delete truncateOptions.words;
}
}
return new SafeString(
getMetaDataExcerpt(excerptText, truncateOptions)
);
};