From: Mike Taylor Date: Thu, 29 May 2014 15:12:26 +0000 (+0100) Subject: the "mkws-paragraphs" Handlebars helper now accepts an optional second X-Git-Tag: 1.0.0~566 X-Git-Url: http://sru.miketaylor.org.uk/?a=commitdiff_plain;h=94a3083ce79f3cdf185add39bb933d695815b623;p=mkws-moved-to-github.git the "mkws-paragraphs" Handlebars helper now accepts an optional second argument specifying how many paragraphs to emit. --- diff --git a/src/mkws-handlebars.js b/src/mkws-handlebars.js index 66c9f1c..537779e 100644 --- a/src/mkws-handlebars.js +++ b/src/mkws-handlebars.js @@ -8,9 +8,15 @@ Handlebars.registerHelper('mkws-json', function(obj) { // This is intended to handle paragraphs from Wikipedia, hence the // rather hacky code to remove numbered references. // -Handlebars.registerHelper('mkws-paragraphs', function(obj) { +Handlebars.registerHelper('mkws-paragraphs', function(obj, count) { var acc = []; - for (var i = 0; i < obj.length; i++) { + + // For some reason, Handlebars provides the value + // {"hash":{},"data":{}} for undefined parameters + if (count.hasOwnProperty('hash')) count = undefined; + if (!count || count > obj.length) count = obj.length; + + for (var i = 0; i < count; i++) { acc.push('

', obj[i].replace(/\[[0-9,]+\]/g, ''), '

'); } return acc.join('');