mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-12 06:25:51 +03:00
16c3785b52
closes #10531 - Adds space when encountering closing </p> and <br> tags - The mobiledoc-to-html conversion produces these tags in this exact syntax, so there is no need to account for more cases like additional spaces or alternative syntax like <br /> - Added test cases that cover spacing use-casei - Changed some existing tests to contain more close-to-real-world markup - The downside of this approach is generating multiple spaces in case there are empty paragraphs in the markup. The same concern is true for current "new line" substitution: > excerpt.replace(/(\r\n|\n|\r)+/gm, ' ') but it never has been a concern as in real world when multiple spaces are used inside of the `<p>` tag they are rendered as single space.
24 lines
768 B
JavaScript
24 lines
768 B
JavaScript
var downsize = require('downsize');
|
|
|
|
function getExcerpt(html, truncateOptions) {
|
|
truncateOptions = truncateOptions || {};
|
|
// Strip inline and bottom footnotes
|
|
var excerpt = html.replace(/<a href="#fn.*?rel="footnote">.*?<\/a>/gi, '');
|
|
excerpt = excerpt.replace(/<div class="footnotes"><ol>.*?<\/ol><\/div>/, '');
|
|
|
|
// Make sure to have space between paragraphs and new lines
|
|
excerpt = excerpt.replace(/(<\/p>|<br>)/gi, ' ');
|
|
|
|
// 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);
|
|
}
|
|
|
|
module.exports = getExcerpt;
|