Shuo/client/js/libs/handlebars/uri.js

33 lines
663 B
JavaScript
Raw Normal View History

2014-07-07 23:13:12 +00:00
function escape(text) {
var e = {
"<": "&lt;",
2014-09-15 16:46:46 +00:00
">": "&gt;",
2014-09-16 17:33:15 +00:00
"'": "&#39;"
2014-07-07 23:13:12 +00:00
};
2014-09-15 16:46:46 +00:00
return text.replace(/[<>']/g, function (c) {
2014-07-07 23:13:12 +00:00
return e[c];
});
}
Handlebars.registerHelper(
"uri", function(text) {
var urls = [];
text = URI.withinString(text, function(url) {
urls.push(url);
2014-07-24 00:16:00 +00:00
return "$(" + (urls.length - 1) + ")";
2014-07-07 23:13:12 +00:00
});
text = escape(text);
for (var i in urls) {
var url = escape(urls[i]);
2014-09-15 16:46:46 +00:00
var replace = url;
if (url.indexOf("javascript:") !== 0) {
replace = "<a href='" + url.replace(/^www/, "//www") + "' target='_blank'>" + url + "</a>";
}
2014-07-07 23:13:12 +00:00
text = text.replace(
2014-09-15 16:46:46 +00:00
"$(" + i + ")", replace
2014-07-07 23:13:12 +00:00
);
}
return text;
}
);