mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-24 06:35:49 +03:00
Added excerpt helper
closes #471 - strips HTML aggressively - with unit tests
This commit is contained in:
parent
590ba48988
commit
2afa81bc1e
@ -57,6 +57,38 @@ coreHelpers = function (ghost) {
|
||||
});
|
||||
|
||||
|
||||
// ### Excerpt Helper
|
||||
//
|
||||
// *Usage example:*
|
||||
// `{{excerpt}}`
|
||||
// `{{excerpt words=50}}`
|
||||
// `{{excerpt characters=256}}`
|
||||
//
|
||||
// Attempts to remove all HTML from the string, and then shortens the result according to the provided option.
|
||||
//
|
||||
// Defaults to words=50
|
||||
//
|
||||
// **returns** SafeString truncated, HTML-free content.
|
||||
//
|
||||
ghost.registerThemeHelper('excerpt', function (options) {
|
||||
var truncateOptions = (options || {}).hash || {},
|
||||
excerpt;
|
||||
|
||||
truncateOptions = _.pick(truncateOptions, ["words", "characters"]);
|
||||
|
||||
/*jslint regexp:true */
|
||||
excerpt = String(this.content).replace(/<\/?[^>]+>/gi, "");
|
||||
/*jslint regexp:false */
|
||||
|
||||
if (!truncateOptions.words && !truncateOptions.characters) {
|
||||
truncateOptions.words = 50;
|
||||
}
|
||||
|
||||
return new hbs.handlebars.SafeString(
|
||||
downsize(excerpt, truncateOptions)
|
||||
);
|
||||
});
|
||||
|
||||
/**
|
||||
* [ description]
|
||||
*
|
||||
|
@ -61,6 +61,65 @@ describe('Core Helpers', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Excerpt Helper', function () {
|
||||
|
||||
it('has loaded excerpt helper', function () {
|
||||
should.exist(handlebars.helpers.excerpt);
|
||||
});
|
||||
|
||||
it('can render excerpt', function () {
|
||||
var content = "Hello World",
|
||||
rendered = handlebars.helpers.excerpt.call({content: content});
|
||||
|
||||
should.exist(rendered);
|
||||
rendered.string.should.equal(content);
|
||||
});
|
||||
|
||||
it('does not output HTML', function () {
|
||||
var content = '<p>There are <br />10<br> types<br/> of people in <img src="a">the world:'
|
||||
+ '<img src=b alt=\"c\"> those who <img src="@" onclick="javascript:alert(\'hello\');">'
|
||||
+ "understand trinary</p>, those who don't <div style='' class=~/'-,._?!|#>and"
|
||||
+ "< test > those<<< test >>> who mistake it <for> binary.",
|
||||
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.",
|
||||
rendered = handlebars.helpers.excerpt.call({content: content});
|
||||
|
||||
should.exist(rendered);
|
||||
rendered.string.should.equal(expected);
|
||||
|
||||
});
|
||||
|
||||
it('can truncate content by word', function () {
|
||||
var content = "<p>Hello <strong>World! It's me!</strong></p>",
|
||||
expected = "Hello World",
|
||||
rendered = (
|
||||
handlebars.helpers.excerpt.call(
|
||||
{content: content},
|
||||
{"hash": {"words": 2}}
|
||||
)
|
||||
);
|
||||
|
||||
should.exist(rendered);
|
||||
rendered.string.should.equal(expected);
|
||||
});
|
||||
|
||||
it('can truncate content by character', function () {
|
||||
var content = "<p>Hello <strong>World! It's me!</strong></p>",
|
||||
expected = "Hello Wo",
|
||||
rendered = (
|
||||
handlebars.helpers.excerpt.call(
|
||||
{content: content},
|
||||
{"hash": {"characters": 8}}
|
||||
)
|
||||
);
|
||||
|
||||
should.exist(rendered);
|
||||
rendered.string.should.equal(expected);
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
describe('Navigation Helper', function () {
|
||||
|
||||
it('has loaded nav helper', function () {
|
||||
|
Loading…
Reference in New Issue
Block a user