2020-04-29 18:44:27 +03:00
const should = require ( 'should' ) ;
2014-10-10 18:54:07 +04:00
2020-04-29 18:44:27 +03:00
// Stuff we are testing
2021-10-06 12:52:46 +03:00
const excerpt = require ( '../../../../core/frontend/helpers/excerpt' ) ;
2014-10-10 18:54:07 +04:00
describe ( '{{excerpt}} Helper' , function ( ) {
2019-12-03 07:32:46 +03:00
it ( 'renders empty string when html, excerpt, and custom_excerpt are null' , function ( ) {
const html = null ;
2021-10-04 18:30:54 +03:00
const rendered = excerpt . call ( {
2019-12-03 07:32:46 +03:00
html : html ,
custom _excerpt : null ,
excerpt : null
} ) ;
2019-03-18 14:52:49 +03:00
should . exist ( rendered ) ;
rendered . string . should . equal ( '' ) ;
} ) ;
2019-12-03 07:32:46 +03:00
it ( 'can render custom_excerpt' , function ( ) {
2020-04-29 18:44:27 +03:00
const html = 'Hello World' ;
2021-10-04 18:30:54 +03:00
const rendered = excerpt . call ( {
2020-04-29 18:44:27 +03:00
html : html ,
custom _excerpt : ''
} ) ;
2014-10-10 18:54:07 +04:00
should . exist ( rendered ) ;
rendered . string . should . equal ( html ) ;
} ) ;
2019-12-03 07:32:46 +03:00
it ( 'can render excerpt when other fields are empty' , function ( ) {
2020-04-29 18:44:27 +03:00
const html = '' ;
2021-10-04 18:30:54 +03:00
const rendered = excerpt . call ( {
2020-04-29 18:44:27 +03:00
html : html ,
custom _excerpt : '' ,
excerpt : 'Regular excerpt'
} ) ;
2019-12-03 07:32:46 +03:00
should . exist ( rendered ) ;
rendered . string . should . equal ( 'Regular excerpt' ) ;
} ) ;
2014-10-10 18:54:07 +04:00
it ( 'does not output HTML' , function ( ) {
2019-12-03 07:32:46 +03:00
const html = '<p>There are <br />10<br> types<br/> of people in <img src="a">the world:' +
2014-10-10 18:54:07 +04: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.' ;
2021-10-04 18:30:54 +03:00
const rendered = excerpt . call ( {
2020-04-29 18:44:27 +03:00
html : html ,
custom _excerpt : ''
} ) ;
2014-10-10 18:54:07 +04:00
should . exist ( rendered ) ;
rendered . string . should . equal ( expected ) ;
} ) ;
2014-12-04 16:33:30 +03:00
it ( '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>, 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> right after.' ;
const expected = 'Testing, my footnotes. And stuff. Footnotewith a link right after.' ;
2021-10-04 18:30:54 +03:00
const rendered = excerpt . call ( {
2020-04-29 18:44:27 +03:00
html : html ,
custom _excerpt : ''
} ) ;
2014-12-04 16:33:30 +03:00
should . exist ( rendered ) ;
rendered . string . should . equal ( expected ) ;
} ) ;
it ( 'strips inline and bottom footnotes' , function ( ) {
2019-12-03 07:32:46 +03:00
const html = '<p>Testing<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup> a very short post with a single footnote.</p>\n' +
2020-04-29 18:44:27 +03:00
'<div class="footnotes"><ol><li class="footnote" id="fn:1"><p><a href="https://ghost.org">https://ghost.org</a> <a href="#fnref:1" title="return to article">↩</a></p></li></ol></div>' ;
const expected = 'Testing a very short post with a single footnote.' ;
2021-10-04 18:30:54 +03:00
const rendered = excerpt . call ( {
2020-04-29 18:44:27 +03:00
html : html ,
custom _excerpt : ''
} ) ;
2014-12-04 16:33:30 +03:00
should . exist ( rendered ) ;
rendered . string . should . equal ( expected ) ;
} ) ;
2014-10-10 18:54:07 +04:00
it ( 'can truncate html 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!' ;
const rendered = (
2021-10-04 18:30:54 +03:00
excerpt . call (
2020-04-29 18:44:27 +03:00
{
html : html ,
custom _excerpt : ''
} ,
{ hash : { words : '2' } }
)
) ;
2014-10-10 18:54:07 +04:00
should . exist ( rendered ) ;
rendered . string . should . equal ( expected ) ;
} ) ;
it ( 'can truncate html with non-ascii characters by word' , function ( ) {
2020-04-29 18:44:27 +03:00
const html = '<p>Едквюэ опортэат <strong>праэчынт ючю но, квуй эю</strong></p>' ;
const expected = 'Едквюэ опортэат' ;
const rendered = (
2021-10-04 18:30:54 +03:00
excerpt . call (
2020-04-29 18:44:27 +03:00
{
html : html ,
custom _excerpt : ''
} ,
{ hash : { words : '2' } }
)
) ;
2014-10-10 18:54:07 +04:00
should . exist ( rendered ) ;
rendered . string . should . equal ( expected ) ;
} ) ;
it ( 'can truncate html by character' , function ( ) {
2020-04-29 18:44:27 +03:00
const html = '<p>Hello <strong>World! It\'s me!</strong></p>' ;
const expected = 'Hello Wo' ;
const rendered = (
2021-10-04 18:30:54 +03:00
excerpt . call (
2020-04-29 18:44:27 +03:00
{
html : html ,
custom _excerpt : ''
} ,
{ hash : { characters : '8' } }
)
) ;
2014-10-10 18:54:07 +04:00
should . exist ( rendered ) ;
rendered . string . should . equal ( expected ) ;
} ) ;
2017-08-01 11:39:34 +03:00
2019-12-03 07:32:46 +03:00
it ( 'uses custom_excerpt if provided instead of truncating html' , function ( ) {
2020-04-29 18:44:27 +03:00
const html = '<p>Hello <strong>World! It\'s me!</strong></p>' ;
const customExcerpt = 'My Custom Excerpt wins!' ;
const expected = 'My Custom Excerpt wins!' ;
const rendered = (
2021-10-04 18:30:54 +03:00
excerpt . call (
2020-04-29 18:44:27 +03:00
{
html : html ,
custom _excerpt : customExcerpt
}
)
) ;
2017-08-02 14:09:12 +03:00
should . exist ( rendered ) ;
rendered . string . should . equal ( expected ) ;
} ) ;
2019-12-03 07:32:46 +03:00
it ( 'does not truncate custom_excerpt if characters options is provided' , function ( ) {
2020-04-29 18:44:27 +03:00
const html = '<p>Hello <strong>World! It\'s me!</strong></p>' ;
const customExcerpt = '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 ' +
'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 rendered = (
2021-10-04 18:30:54 +03:00
excerpt . call (
2020-04-29 18:44:27 +03:00
{
html : html ,
custom _excerpt : customExcerpt
} ,
{ hash : { characters : '8' } }
)
) ;
2017-08-01 11:39:34 +03:00
should . exist ( rendered ) ;
rendered . string . should . equal ( expected ) ;
} ) ;
2017-08-02 14:09:12 +03:00
2019-12-03 07:32:46 +03:00
it ( 'does not truncate custom_excerpt if words options is provided' , function ( ) {
2020-04-29 18:44:27 +03:00
const html = '<p>Hello <strong>World! It\'s me!</strong></p>' ;
const customExcerpt = '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 ' +
'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 rendered = (
2021-10-04 18:30:54 +03:00
excerpt . call (
2020-04-29 18:44:27 +03:00
{
html : html ,
custom _excerpt : customExcerpt
} ,
{ hash : { words : '10' } }
)
) ;
2017-08-02 14:09:12 +03:00
should . exist ( rendered ) ;
rendered . string . should . equal ( expected ) ;
} ) ;
2019-08-26 18:01:20 +03:00
it ( 'puts additional space after closing paragraph' , function ( ) {
2020-04-29 18:44:27 +03:00
const html = '<p>Testing.</p><p>Space before this text.</p><p>And this as well!</p>' ;
const expected = 'Testing. Space before this text. And this as well!' ;
const rendered = (
2021-10-04 18:30:54 +03:00
excerpt . call (
2020-04-29 18:44:27 +03:00
{
html : html ,
custom _excerpt : ''
}
)
) ;
2019-08-26 18:01:20 +03:00
should . exist ( rendered ) ;
rendered . string . should . equal ( expected ) ;
} ) ;
it ( 'puts additional space instead of <br> tag' , function ( ) {
2020-04-29 18:44:27 +03:00
const html = '<p>Testing.<br>Space before this text.<br>And this as well!</p>' ;
const expected = 'Testing. Space before this text. And this as well!' ;
const rendered = (
2021-10-04 18:30:54 +03:00
excerpt . call (
2020-04-29 18:44:27 +03:00
{
html : html ,
custom _excerpt : ''
}
)
) ;
2019-08-26 18:01:20 +03:00
should . exist ( rendered ) ;
rendered . string . should . equal ( expected ) ;
} ) ;
it ( 'puts additional space between paragraph in markup generated by Ghost' , function ( ) {
2019-12-03 07:32:46 +03:00
const html = '<p>put space in excerpt.</p><p></p><p>before this paragraph.</p>' +
2019-09-20 16:31:42 +03:00
'<figure class="kg-card kg-image-card"><img src="/content/images/2019/08/photo.jpg" class="kg-image"></figure>' +
2020-04-29 18:44:27 +03:00
'<p>and skip the image.</p><p></p>' ;
const expected = 'put space in excerpt. before this paragraph. and skip the image.' ;
const rendered = (
2021-10-04 18:30:54 +03:00
excerpt . call (
2020-04-29 18:44:27 +03:00
{
html : html ,
custom _excerpt : ''
}
)
) ;
2019-08-26 18:01:20 +03:00
should . exist ( rendered ) ;
rendered . string . should . equal ( expected ) ;
} ) ;
2014-10-10 18:54:07 +04:00
} ) ;