forked from scottcorgan/angular-linkify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathangular-linkify.min.js
6 lines (6 loc) · 1.52 KB
/
angular-linkify.min.js
1
2
3
4
5
6
/*
angular-linkify - v1.2.0 - 10/25/2016
Angular filter to linkify urls, "@" usernames, and hashtags.
Copyright (c) 2016 ; Licensed
*/
angular.module("linkify",[]),angular.module("linkify").filter("linkify",function(){"use strict";function linkify(_str,type){if(_str){var _text=_str.replace(/(?:https?\:\/\/|www\.)+(?![^\s]*?")([\w.,@?!^=%&:\/~+#-]*[\w@?!^=%&\/~+#-])?/gi,function(url){var wrap=document.createElement("div"),anch=document.createElement("a");return anch.href=url.replace(/&/g,"&"),anch.target="_blank",anch.innerHTML=url,wrap.appendChild(anch),wrap.innerHTML});return _text?("twitter"===type&&(_text=_text.replace(/(|\s)*@([\u00C0-\u1FFF\w]+)/g,'$1<a href="https://twitter.com/$2" target="_blank">@$2</a>'),_text=_text.replace(/(^|\s)*#([\u00C0-\u1FFF\w]+)/g,'$1<a href="https://twitter.com/search?q=%23$2" target="_blank">#$2</a>')),"github"===type&&(_text=_text.replace(/(|\s)*@(\w+)/g,'$1<a href="https://github.com/$2" target="_blank">@$2</a>')),_text):""}}return function(text,type){return linkify(text,type)}}).factory("linkify",["$filter",function($filter){"use strict";function _linkifyAsType(type){return function(str){return $filter("linkify")(str,type)}}return{twitter:_linkifyAsType("twitter"),github:_linkifyAsType("github"),normal:_linkifyAsType()}}]).directive("linkify",["$filter","$timeout","linkify",function($filter,$timeout,linkify){"use strict";return{restrict:"A",link:function(scope,element,attrs){var type=attrs.linkify||"normal";$timeout(function(){element.html(linkify[type](element.html()))})}}}]);