From e484d9224e3c0a08028621acdc06ff2fb72a2a3b Mon Sep 17 00:00:00 2001 From: Christopher Giffard Date: Wed, 7 Aug 2013 19:45:37 +1000 Subject: [PATCH] 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}} --- core/server/helpers/index.js | 24 +++++++++++++++- core/test/unit/frontend_helpers_index_spec.js | 28 +++++++++++++++++++ package.json | 3 +- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/core/server/helpers/index.js b/core/server/helpers/index.js index 6025300397..521193244c 100644 --- a/core/server/helpers/index.js +++ b/core/server/helpers/index.js @@ -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); }); diff --git a/core/test/unit/frontend_helpers_index_spec.js b/core/test/unit/frontend_helpers_index_spec.js index 61ad98a788..7af52773dc 100644 --- a/core/test/unit/frontend_helpers_index_spec.js +++ b/core/test/unit/frontend_helpers_index_spec.js @@ -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 = "

Hello World! It's me!

", + rendered = ( + handlebars.helpers.content + .call( + {content: content}, + {"hash":{"words": 2}} + ) + ); + + should.exist(rendered); + rendered.string.should.equal("

Hello World

"); + }); + + it('can truncate content by character', function () { + var content = "

Hello World! It's me!

", + rendered = ( + handlebars.helpers.content + .call( + {content: content}, + {"hash":{"characters": 8}} + ) + ); + + should.exist(rendered); + rendered.string.should.equal("

Hello Wo

"); + }); }); describe('Navigation Helper', function () { diff --git a/package.json b/package.json index 85a6ac1c32..2aa11a5115 100644 --- a/package.json +++ b/package.json @@ -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",