the "mkws-paragraphs" Handlebars helper now accepts an optional second
[mkws-moved-to-github.git] / src / mkws-handlebars.js
index 6e93578..537779e 100644 (file)
@@ -5,10 +5,19 @@ Handlebars.registerHelper('mkws-json', function(obj) {
 });
 
 
-Handlebars.registerHelper('mkws-paragraphs', 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, count) {
   var acc = [];
-  for (var i = 0; i < obj.length; i++) {
-    acc.push('<p>', obj[i], '</p>');
+
+  // 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('<p>', obj[i].replace(/\[[0-9,]+\]/g, ''), '</p>');
   }
   return acc.join('');
 });