Skip to content

Instantly share code, notes, and snippets.

@thedjpetersen
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thedjpetersen/9265479 to your computer and use it in GitHub Desktop.
Save thedjpetersen/9265479 to your computer and use it in GitHub Desktop.
Plugin to make links clickable
/* plugin css */
app.plugins.linkify = function(message) {
// see http://daringfireball.net/2010/07/improved_regex_for_matching_urls
var re = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi;
var parsed = message.replace(re, function(url) {
// If the text is in a html tag
// we need to just return it without embedding it
// we fetch the rest of the string from our match
// and then check to see if the we reach a > character
// before a < character
var rest = _.last(arguments).substring(arguments[arguments.length-2]);
if (rest.indexOf(">") !== -1 && rest.indexOf(">") < rest.indexOf("<")) {
return url;
}
// turn into a link
var href = url;
if (url.indexOf('http') !== 0) {
href = 'http://' + url;
}
return '<a href="' + href + '" target="_blank">' + url + '</a>';
});
return parsed;
}
{
"name": "Linkify",
"author": "David Petersen",
"description": "Create embedded links",
"pluginId": "linkify",
"settings": {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment