mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 09:52:06 +03:00
2e28bc2a5f
refs https://github.com/TryGhost/Ghost/issues/10062 - When content gating is in place a lot of times both `html` and `custom_excerpt` fields on posts/pages are empty and the output of `{{excerpt}}` helper is also empty. We do return an `excerpt` property as a part of post resource which can serve as a safe fallback for when the above fields are not filled. It massively improves the experience of using the helper with gated content - Refactored nested ternaries to be more readable - Added fallback to excerpt property when HTML is hidden from members - Removed note about the review of excerpt helper - Added test case for 'excerpt' property
43 lines
1.3 KiB
JavaScript
43 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"
|
|
|
|
var proxy = require('./proxy'),
|
|
_ = require('lodash'),
|
|
SafeString = proxy.SafeString,
|
|
getMetaDataExcerpt = proxy.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)
|
|
);
|
|
};
|