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"
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
var proxy = require('./proxy'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
SafeString = proxy.SafeString,
|
|
|
|
getMetaDataExcerpt = proxy.metaData.getMetaDataExcerpt;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2019-01-04 21:00:45 +03:00
|
|
|
/**
|
|
|
|
* @NOTE:
|
|
|
|
*
|
|
|
|
* Content API v2 returns a calculated `post.excerpt` field.
|
|
|
|
* See https://github.com/TryGhost/Ghost/issues/10062.
|
|
|
|
* We have not touched this helper yet, we will revisit later.
|
|
|
|
*
|
|
|
|
* @TODO: remove or change if we drop v0.1
|
|
|
|
*/
|
2017-04-04 19:07:35 +03:00
|
|
|
module.exports = function excerpt(options) {
|
2017-08-01 11:39:34 +03:00
|
|
|
var truncateOptions = (options || {}).hash || {},
|
2019-03-04 09:39:20 +03:00
|
|
|
excerptText = this.custom_excerpt
|
|
|
|
? String(this.custom_excerpt)
|
|
|
|
: this.html
|
|
|
|
? String(this.html)
|
|
|
|
: '';
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
|
|
truncateOptions = _.pick(truncateOptions, ['words', 'characters']);
|
|
|
|
_.keys(truncateOptions).map(function (key) {
|
|
|
|
truncateOptions[key] = parseInt(truncateOptions[key], 10);
|
|
|
|
});
|
|
|
|
|
2017-08-02 14:09:12 +03:00
|
|
|
if (!_.isEmpty(this.custom_excerpt)) {
|
|
|
|
truncateOptions.characters = this.custom_excerpt.length;
|
|
|
|
if (truncateOptions.words) {
|
|
|
|
delete truncateOptions.words;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
return new SafeString(
|
2017-08-01 11:39:34 +03:00
|
|
|
getMetaDataExcerpt(excerptText, truncateOptions)
|
2014-10-10 18:54:07 +04:00
|
|
|
);
|
2017-04-04 19:07:35 +03:00
|
|
|
};
|