2020-04-29 18:44:27 +03:00
|
|
|
const downsize = require('downsize');
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2021-07-02 11:35:07 +03:00
|
|
|
function generateExcerpt(html, truncateOptions) {
|
2016-02-18 14:47:08 +03:00
|
|
|
truncateOptions = truncateOptions || {};
|
2016-01-17 13:07:52 +03:00
|
|
|
// Strip inline and bottom footnotes
|
2020-04-29 18:44:27 +03:00
|
|
|
let excerpt = html.replace(/<a href="#fn.*?rel="footnote">.*?<\/a>/gi, '');
|
2016-01-17 13:07:52 +03:00
|
|
|
excerpt = excerpt.replace(/<div class="footnotes"><ol>.*?<\/ol><\/div>/, '');
|
2019-08-26 18:01:20 +03:00
|
|
|
|
|
|
|
// Make sure to have space between paragraphs and new lines
|
|
|
|
excerpt = excerpt.replace(/(<\/p>|<br>)/gi, ' ');
|
|
|
|
|
2016-01-17 13:07:52 +03:00
|
|
|
// Strip other html
|
|
|
|
excerpt = excerpt.replace(/<\/?[^>]+>/gi, '');
|
|
|
|
excerpt = excerpt.replace(/(\r\n|\n|\r)+/gm, ' ');
|
|
|
|
|
|
|
|
if (!truncateOptions.words && !truncateOptions.characters) {
|
|
|
|
truncateOptions.words = 50;
|
|
|
|
}
|
|
|
|
|
|
|
|
return downsize(excerpt, truncateOptions);
|
|
|
|
}
|
|
|
|
|
2021-07-02 11:35:07 +03:00
|
|
|
module.exports = generateExcerpt;
|