Last active
August 29, 2015 13:56
-
-
Save thedjpetersen/9265479 to your computer and use it in GitHub Desktop.
Plugin to make links clickable
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
/* plugin css */ |
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
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; | |
} |
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
{ | |
"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