Ghost/core/frontend/helpers/excerpt.js
Hannah Wolfe 35e3e0708c Moved helper proxy into a service
- The proxy is not a helper, we want the helpers folder to only include helpers
- The proxy is also meant to be the interface to Ghost for the helpers, and we want to enforce that
- This is a small step on the way
2020-04-08 17:22:44 +01:00

42 lines
1.3 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"
const {SafeString, metaData} = require('../services/proxy');
const _ = require('lodash');
const getMetaDataExcerpt = metaData.getMetaDataExcerpt;
module.exports = function excerpt(options) {
let truncateOptions = (options || {}).hash || {};
let excerptText;
if (this.custom_excerpt) {
excerptText = String(this.custom_excerpt);
} else if (this.html) {
excerptText = String(this.html);
} else if (this.excerpt) {
excerptText = String(this.excerpt);
} else {
excerptText = '';
}
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)
);
};