2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const getExcerpt = require('../../../../core/frontend/meta/excerpt');
|
2016-01-17 13:07:52 +03:00
|
|
|
|
|
|
|
describe('getExcerpt', function () {
|
|
|
|
it('should return html excerpt with no html', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const html = '<p>There are <br />10<br> types<br/> of people in <img src="a">the world:' +
|
2016-01-17 13:07:52 +03:00
|
|
|
'<img src=b alt="c"> those who <img src="@" onclick="javascript:alert(\'hello\');">' +
|
2019-08-26 18:01:20 +03:00
|
|
|
'understand trinary,</p> those who don\'t <div style="" class=~/\'-,._?!|#>and' +
|
2020-04-29 18:44:27 +03:00
|
|
|
'< test > those<<< test >>> who mistake it <for> binary.';
|
|
|
|
|
|
|
|
const expected = 'There are 10 types of people in the world: those who understand trinary, those who ' +
|
|
|
|
'don\'t and those>> who mistake it <for> binary.';
|
2016-01-17 13:07:52 +03:00
|
|
|
|
|
|
|
getExcerpt(html, {}).should.equal(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return html excerpt strips multiple inline footnotes', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const html = '<p>Testing<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>, ' +
|
2016-01-17 13:07:52 +03:00
|
|
|
'my footnotes. And stuff. Footnote<sup id="fnref:2"><a href="#fn:2" ' +
|
|
|
|
'rel="footnote">2</a></sup><a href="http://google.com">with a link</a> ' +
|
2020-04-29 18:44:27 +03:00
|
|
|
'right after.';
|
|
|
|
|
|
|
|
const expected = 'Testing, my footnotes. And stuff. Footnotewith a link right after.';
|
2016-01-17 13:07:52 +03:00
|
|
|
|
|
|
|
getExcerpt(html, {}).should.equal(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return html excerpt striping inline and bottom footnotes', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const html = '<p>Testing<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a>' +
|
2017-03-21 11:24:11 +03:00
|
|
|
'</sup> a very short post with a single footnote.</p>\n' +
|
|
|
|
'<div class="footnotes"><ol><li class="footnote" id="fn:1"><p>' +
|
|
|
|
'<a href="https://ghost.org">https://ghost.org</a> <a href="#fnref:1" ' +
|
2020-04-29 18:44:27 +03:00
|
|
|
'title="return to article">↩</a></p></li></ol></div>';
|
|
|
|
|
|
|
|
const expected = 'Testing a very short post with a single footnote.';
|
2016-01-17 13:07:52 +03:00
|
|
|
|
|
|
|
getExcerpt(html, {}).should.equal(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return html excerpt truncated by word', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const html = '<p>Hello <strong>World! It\'s me!</strong></p>';
|
|
|
|
const expected = 'Hello World!';
|
2016-01-17 13:07:52 +03:00
|
|
|
|
|
|
|
getExcerpt(html, {words: '2'}).should.equal(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return html excerpt truncated by words with non-ascii characters',
|
2017-03-21 11:24:11 +03:00
|
|
|
function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const html = '<p>Едквюэ опортэат <strong>праэчынт ючю но, квуй эю</strong></p>';
|
|
|
|
const expected = 'Едквюэ опортэат';
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2017-03-21 11:24:11 +03:00
|
|
|
getExcerpt(html, {words: '2'}).should.equal(expected);
|
|
|
|
});
|
2016-01-17 13:07:52 +03:00
|
|
|
|
|
|
|
it('should return html excerpt truncated by character',
|
2017-03-21 11:24:11 +03:00
|
|
|
function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const html = '<p>Hello <strong>World! It\'s me!</strong></p>';
|
|
|
|
const expected = 'Hello Wo';
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2017-03-21 11:24:11 +03:00
|
|
|
getExcerpt(html, {characters: '8'}).should.equal(expected);
|
|
|
|
});
|
2017-08-02 14:09:12 +03:00
|
|
|
|
|
|
|
it('should fall back to 50 words if not specified',
|
|
|
|
function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const html = '<p>There are <br />10<br> types<br/> of people in <img src="a">the world:' +
|
2017-08-02 14:09:12 +03:00
|
|
|
'<img src=b alt="c"> those who <img src="@" onclick="javascript:alert(\'hello\');">' +
|
2019-08-26 18:01:20 +03:00
|
|
|
'understand trinary,</p> those who don\'t <div style="" class=~/\'-,._?!|#>and' +
|
2020-04-29 18:44:27 +03:00
|
|
|
'< test > those<<< test >>> who mistake it <for> binary.';
|
|
|
|
|
|
|
|
const expected = 'There are 10 types of people in the world: those who understand trinary, those who ' +
|
|
|
|
'don\'t and those>> who mistake it <for> binary.';
|
2017-08-02 14:09:12 +03:00
|
|
|
|
|
|
|
getExcerpt(html).should.equal(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should truncate plain text for custom excerpts',
|
|
|
|
function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const html = 'This is a custom excerpt. It should always be rendered in full length and not being cut ' +
|
|
|
|
'off. The maximum length of a custom excerpt is 300 characters. Enough to tell a bit about ' +
|
|
|
|
'your story and make a nice summary for your readers. It\s only allowed to truncate anything ' +
|
|
|
|
'after 300 characters. This give';
|
|
|
|
|
|
|
|
const expected = 'This is a custom excerpt. It should always be rendered in full length and not being cut ' +
|
2017-08-02 14:09:12 +03:00
|
|
|
'off. The maximum length of a custom excerpt is 300 characters. Enough to tell a bit about ' +
|
|
|
|
'your story and make a nice summary for your readers. It\s only allowed to truncate anything ' +
|
2020-04-29 18:44:27 +03:00
|
|
|
'after 300 characters. This give';
|
2017-08-02 14:09:12 +03:00
|
|
|
|
|
|
|
getExcerpt(html, {characters: '300'}).should.equal(expected);
|
|
|
|
});
|
2016-01-17 13:07:52 +03:00
|
|
|
});
|