Merge pull request #493 from ErisDS/helpers

Added excerpt helper
This commit is contained in:
Hannah Wolfe 2013-08-22 12:56:01 -07:00
commit 2d3e2f36b1
2 changed files with 91 additions and 0 deletions

View File

@ -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]
*

View File

@ -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 &lt;for&gt; binary.",
expected = "There are 10 types of people in the world: those who understand trinary, those who don't "
+ "and those>> who mistake it &lt;for&gt; 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 () {