-
-
Save onsails/7125110 to your computer and use it in GitHub Desktop.
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
define(['angular'], function(angular){ | |
/** | |
* Converts any links inside the passed in text parameter into HTML links. | |
*/ | |
angular.module('Filters') | |
.filter('Autolink', function(){ | |
var urlPattern = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/gi; | |
var normalize = function(arr){ | |
var result = {}; | |
for (var i = 0, l = arr.length; i < l; i++) { | |
if (!result.hasOwnProperty(arr[i])) { | |
result[arr[i]] = arr[i]; | |
} | |
} | |
return result; | |
}; | |
return function(text, target){ | |
angular.forEach(normalize(text.match(urlPattern)), function(url) { | |
text = text.replace(new RegExp(url, 'g'), '<a target="' + target + '" href="'+ url + '">' + url +'</a>'); | |
}); | |
return text; | |
}; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment