0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Themes: Added truncation to hbs content helper

Fixes #256

- Developed and linked new module, downsize, for tag-safe truncation
- Altered existing content handler to accept options for truncation
- Added tests for handler

Using truncation:

{{content words=10}}
{{content characters=256}}
This commit is contained in:
Christopher Giffard 2013-08-07 19:45:37 +10:00
parent 2f11f053ab
commit e484d9224e
3 changed files with 53 additions and 2 deletions

View file

@ -1,5 +1,6 @@
var _ = require('underscore'),
moment = require('moment'),
downsize = require('downsize'),
when = require('when'),
hbs = require('express-hbs'),
errors = require('../errorHandling'),
@ -29,8 +30,29 @@ coreHelpers = function (ghost) {
});
// ### Content Helper
// Turns content html into a safestring so that the user doesn't have to escape it
//
// *Usage example:*
// `{{content}}`
// `{{content words=20}}`
// `{{content characters=256}}`
//
// Turns content html into a safestring so that the user doesn't have to
// escape it or tell handlebars to leave it alone with a triple-brace.
//
// Enables tag-safe truncation of content by characters or words.
//
// **returns** SafeString content html, complete or truncated.
//
ghost.registerThemeHelper('content', function (options) {
var truncateOptions = (options || {}).hash || {};
truncateOptions = _.pick(truncateOptions, ["words", "characters"]);
if (truncateOptions.words || truncateOptions.characters) {
return new hbs.handlebars.SafeString(
downsize(this.content, truncateOptions)
);
}
return new hbs.handlebars.SafeString(this.content);
});

View file

@ -31,6 +31,34 @@ describe('Core Helpers', function () {
should.exist(rendered);
rendered.string.should.equal(content);
});
it('can truncate content by word', function () {
var content = "<p>Hello <strong>World! It's me!</strong></p>",
rendered = (
handlebars.helpers.content
.call(
{content: content},
{"hash":{"words": 2}}
)
);
should.exist(rendered);
rendered.string.should.equal("<p>Hello <strong>World</strong></p>");
});
it('can truncate content by character', function () {
var content = "<p>Hello <strong>World! It's me!</strong></p>",
rendered = (
handlebars.helpers.content
.call(
{content: content},
{"hash":{"characters": 8}}
)
);
should.exist(rendered);
rendered.string.should.equal("<p>Hello <strong>Wo</strong></p>");
});
});
describe('Navigation Helper', function () {

View file

@ -26,7 +26,8 @@
"node-uuid": "1.4.0",
"colors": "0.6.1",
"semver": "2.1.0",
"fs-extra": "0.6.3"
"fs-extra": "0.6.3",
"downsize": "0.0.2"
},
"devDependencies": {
"grunt": "~0.4.1",