diff --git a/core/server/helpers/excerpt.js b/core/server/helpers/excerpt.js index dc7553e8b0..817587e74d 100644 --- a/core/server/helpers/excerpt.js +++ b/core/server/helpers/excerpt.js @@ -19,6 +19,13 @@ module.exports = function excerpt(options) { truncateOptions[key] = parseInt(truncateOptions[key], 10); }); + if (!_.isEmpty(this.custom_excerpt)) { + truncateOptions.characters = this.custom_excerpt.length; + if (truncateOptions.words) { + delete truncateOptions.words; + } + } + return new SafeString( getMetaDataExcerpt(excerptText, truncateOptions) ); diff --git a/core/test/unit/metadata/excerpt_spec.js b/core/test/unit/metadata/excerpt_spec.js index 0dbf9df8fc..a0469f3708 100644 --- a/core/test/unit/metadata/excerpt_spec.js +++ b/core/test/unit/metadata/excerpt_spec.js @@ -56,4 +56,30 @@ describe('getExcerpt', function () { getExcerpt(html, {characters: '8'}).should.equal(expected); }); + + it('should fall back to 50 words if not specified', + function () { + var html = '
There are
10
types
of people in the world:' +
+ ' those who ' +
+ 'understand trinary
Testing1, my footnotes. And stuff. Footnote2with a link right after.', expected = 'Testing, my footnotes. And stuff. Footnotewith a link right after.', - rendered = helpers.excerpt.call({html: html}); + rendered = helpers.excerpt.call({ + html: html, + custom_excerpt: '' + }); should.exist(rendered); rendered.string.should.equal(expected); @@ -38,7 +47,10 @@ describe('{{excerpt}} Helper', function () { var html = '
Testing1 a very short post with a single footnote.
\n' + '', expected = 'Testing a very short post with a single footnote.', - rendered = helpers.excerpt.call({html: html}); + rendered = helpers.excerpt.call({ + html: html, + custom_excerpt: '' + }); should.exist(rendered); rendered.string.should.equal(expected); @@ -49,7 +61,10 @@ describe('{{excerpt}} Helper', function () { expected = 'Hello World!', rendered = ( helpers.excerpt.call( - {html: html}, + { + html: html, + custom_excerpt: '' + }, {hash: {words: '2'}} ) ); @@ -63,7 +78,10 @@ describe('{{excerpt}} Helper', function () { expected = 'Едквюэ опортэат', rendered = ( helpers.excerpt.call( - {html: html}, + { + html: html, + custom_excerpt: '' + }, {hash: {words: '2'}} ) ); @@ -77,7 +95,10 @@ describe('{{excerpt}} Helper', function () { expected = 'Hello Wo', rendered = ( helpers.excerpt.call( - {html: html}, + { + html: html, + custom_excerpt: '' + }, {hash: {characters: '8'}} ) ); @@ -89,7 +110,30 @@ describe('{{excerpt}} Helper', function () { it('uses custom excerpt if provided instead of truncating html', function () { var html = 'Hello World! It\'s me!
', customExcerpt = 'My Custom Excerpt wins!', - expected = 'My Custo', + expected = 'My Custom Excerpt wins!', + rendered = ( + helpers.excerpt.call( + { + html: html, + custom_excerpt: customExcerpt + } + ) + ); + + should.exist(rendered); + rendered.string.should.equal(expected); + }); + + it('does not truncate custom excerpt if characters options is provided', function () { + var html = 'Hello World! It\'s me!
', + 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', + 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', rendered = ( helpers.excerpt.call( { @@ -103,4 +147,28 @@ describe('{{excerpt}} Helper', function () { should.exist(rendered); rendered.string.should.equal(expected); }); + + it('does not truncate custom excerpt if words options is provided', function () { + var html = 'Hello World! It\'s me!
', + 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', + 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', + rendered = ( + helpers.excerpt.call( + { + html: html, + custom_excerpt: customExcerpt + }, + {hash: {words: '10'}} + ) + ); + + should.exist(rendered); + rendered.string.should.equal(expected); + }); });