Created
August 18, 2013 20:14
-
-
Save CMCDragonkai/6263753 to your computer and use it in GitHub Desktop.
JS: AngularJS Autolinking Filter. Adapted from: http://jsfiddle.net/bmleite/FyGen/
This file contains 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