From 4e1cf7acd7be692d8d56bc6941b676ad51a71bfa Mon Sep 17 00:00:00 2001 From: Mike Taylor Date: Fri, 26 Jun 2015 18:25:56 +0100 Subject: [PATCH] Part of ACREP-32. Provide new Handlebars helper mkws-best-url, which returns the first item in a list that matches a regular-expression suggesting that it's a URL (basically anything that starts with http://, https:// or //). Also provide mkws-other-urls (not yet tested), which returns all the other URLs in the list except the first. --- src/mkws-handlebars.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/mkws-handlebars.js b/src/mkws-handlebars.js index 2aaba2c..3e8ee6d 100644 --- a/src/mkws-handlebars.js +++ b/src/mkws-handlebars.js @@ -81,6 +81,31 @@ Handlebars.registerHelper('mkws-first', function(items, options) { }); +var _urlRegexp = /^(https?:)?\/\//; +Handlebars.registerHelper('mkws-best-url', function(items) { + for (var i in items) { + var item = items[i] + if (item.match(_urlRegexp)) { + mkws.warn("'" + item + "' *is* a URL"); + return item; + } + mkws.warn("'" + item + "' is not a URL"); + } + return ""; +}); +Handlebars.registerHelper('mkws-other-urls', function(items) { + var res = []; + for (var i in items) { + var item = items[i] + if (item.match(_urlRegexp)) { + res.push(item); + } + } + return res; +}); + + + Handlebars.registerHelper('mkws-commaList', function(items, options) { var out = ""; -- 1.7.10.4