forked from scottcorgan/angular-linkify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-linkify.js
121 lines (102 loc) · 4 KB
/
angular-linkify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
angular.module('linkify', []);
angular.module('linkify')
/**
* The angular linkify filter.
*/
.filter('linkify', function () {
'use strict';
/**
* Utility function that does all the logic to replace special text with links.
* @param {string} _str
* @param {string} type
* @returns {string}
* @private
*/
function linkify(_str, type) {
if (!_str) {
return;
}
// force type to be lower case
type = (type ? type.toLowerCase() : '');
// replace regular links
var _text = _str.replace(/(?:https?\:\/\/|www\.)+(?![^\s]*?")([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/ig, function (url) {
var wrap = document.createElement('div'),
anch = document.createElement('a'),
prefix = (url.match(/^https?/) ? '' : 'http://');
anch.href = prefix + url;
anch.target = '_blank';
anch.innerHTML = url;
wrap.appendChild(anch);
return wrap.innerHTML;
});
// bugfix
if (!_text) {
return '';
}
// Twitter
switch (type) {
case 'twitter':
_text = _text.replace(/(|\s)*@([\u00C0-\u1FFF\w]+)/g, '$1<a href="https://mobile.twitter.com/$2" target="_blank">@$2</a>');
_text = _text.replace(/(^|\s)*#([\u00C0-\u1FFF\w]+)/g, function ($1, $2, $3) {
return ($2 ? $2 : '') + '<a href="https://mobile.twitter.com/search?q=' + encodeURIComponent('#' + $3) + '" target="_blank">#' + $3 + '</a>';
});
break;
case 'github':
_text = _text.replace(/(|\s)*@(\w+)/g, '$1<a href="https://github.com/$2" target="_blank">@$2</a>');
break;
case 'facebook':
_text = _text.replace(/(|\s)*@([\u00C0-\u1FFF\w]+)/g, '$1<a href="https://www.facebook.com/$2" target="_blank">@$2</a>');
_text = _text.replace(/(^|\s)*#([\u00C0-\u1FFF\w]+)/g, '$1<a href="https://www.facebook.com/hashtag/$2" target="_blank">#$2</a>');
break;
case 'instagram':
_text = _text.replace(/(|\s)*@([\u00C0-\u1FFF\w]+)/g, '$1<a href="https://instagram.com/$2/" target="_blank">@$2</a>');
_text = _text.replace(/(^|\s)*#([\u00C0-\u1FFF\w]+)/g, '$1<a href="https://instagram.com/explore/tags/$2/" target="_blank">#$2</a>');
break;
}
return _text;
}
//
return function (text, type) {
return linkify(text, type);
};
})
/**
* Factory wrapper for the linkify filter.
*/
.factory('linkify', ['$filter', function ($filter) {
'use strict';
/**
* Generate functions to call linkify filter with the correct type.
* @param {string} type
* @returns {Function}
* @private
*/
function _linkifyAsType(type) {
return function (str) {
(type, str);
return $filter('linkify')(str, type);
};
}
return {
twitter: _linkifyAsType('twitter'),
github: _linkifyAsType('github'),
facebook: _linkifyAsType('facebook'),
instagram: _linkifyAsType('instagram'),
normal: _linkifyAsType()
};
}])
/**
* Directive that wraps the filter.
*/
.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()));
});
}
};
}]);