Parse URLs with Handlebars

This commit is contained in:
Mattias Erming 2014-04-21 12:27:43 +02:00
parent 6366fd38a2
commit d49ee1326b
2 changed files with 31 additions and 14 deletions

View file

@ -114,7 +114,7 @@
<div class="time">{{time}}</div>
<div class="user">{{from}}</div>
<div class="type">{{type}}</div>
<div class="text">{{text}}</div>
<div class="text">{{{uri text}}}</div>
</div>
{{/each}}
</script>

View file

@ -35,13 +35,6 @@ $(function() {
return tpl[id](json);
}
Handlebars.registerHelper(
"partial",
function(id) {
return new Handlebars.SafeString(render(id, this));
}
);
function handleEvent(event, json) {
var data = json.data;
switch (event) {
@ -244,12 +237,6 @@ $(function() {
chat.on("focus", "input[type=text]", function() {
$(this).closest(".window").find(".messages").scrollToBottom();
});
function uri(text) {
return URI.withinString(text, function(url) {
return "<a href='" + url + "' target='_blank'>" + url + "</a>";
});
}
var highest = 1;
$.fn.bringToTop = function() {
@ -262,4 +249,34 @@ $(function() {
.removeClass("active")
.end();
};
function uri(text) {
return URI.withinString(text, function(url) {
return "<a href='" + url + "' target='_blank'>" + url + "</a>";
});
}
function escape(string) {
var e = {
"<": "&lt;",
">": "&gt;",
};
return string.replace(/[<>]/g, function (s) {
return e[s];
});
}
Handlebars.registerHelper(
"uri",
function(text) {
return uri(escape(text));
}
);
Handlebars.registerHelper(
"partial",
function(id) {
return new Handlebars.SafeString(render(id, this));
}
);
});