Created
October 14, 2012 01:17
-
-
Save ryndel/3886867 to your computer and use it in GitHub Desktop.
Handlebars.js: linkify helper #cc #handlebars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Handlebars.registerHelper('linkify', function (text) { | |
text = text.replace(/(https?:\/\/\S+)/gi, function (s) { | |
return '<a href="' + s + '">' + s + '</a>'; | |
}); | |
text = text.replace(/(^|)@(\w+)/gi, function (s) { | |
return '<a href="http://twitter.com/' + s + '">' + s + '</a>'; | |
}); | |
text = text.replace(/(^|)#(\w+)/gi, function (s) { | |
return '<a href="http://search.twitter.com/search?q=' + s.replace(/#/,'%23') + '">' + s + '</a>'; | |
}); | |
return new Handlebars.SafeString(text); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was having trouble with trailing punctuation (http://google.com,) so I changed to this regex
/(http|ftp|https)://([\w-]+(.[\w-]+)+)([\w.,@?^=%&:/
+#-]*[\w@?^=%&/+#-])?/gi