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"
|
|
|
|
|
2021-09-28 17:06:33 +03:00
|
|
|
const {SafeString} = require('../services/rendering');
|
|
|
|
const {metaData} = require('../services/proxy');
|
2020-03-30 23:23:02 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const getMetaDataExcerpt = metaData.getMetaDataExcerpt;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
module.exports = function excerpt(options) {
|
2019-12-03 07:32:46 +03:00
|
|
|
let truncateOptions = (options || {}).hash || {};
|
2022-02-21 15:18:02 +03:00
|
|
|
|
2019-12-03 07:32:46 +03:00
|
|
|
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 = '';
|
|
|
|
}
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2022-02-21 15:18:02 +03:00
|
|
|
truncateOptions = _.reduce(truncateOptions, (_truncateOptions, value, key) => {
|
|
|
|
if (['words', 'characters'].includes(key)) {
|
|
|
|
_truncateOptions[key] = parseInt(value, 10);
|
|
|
|
}
|
|
|
|
return _truncateOptions;
|
|
|
|
}, {});
|
2014-10-10 18:54:07 +04:00
|
|
|
|
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
|
|
|
};
|