-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
42 lines (38 loc) · 1.47 KB
/
utils.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
exports.linkify = function (inputText) {
// for // http://, https://, ftp:// with optional [] to label the <a>
var labelPattern = /\b((?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|])(?:\[([a-z0-9-+&@#\/%?=~_|!:,.; ]+)\])?/gim;
// www. without http:// or https:// with optional [] to label the <a>
var labelPseudoUrlPattern = /(^|[^\/])(www\.[^ \t\n\[]+(\b|$))(?:\[([a-z0-9-+&@#\/%?=~_|!:,.; ]+)\])?/gim; // match all characters until whitespace, tab, newline and [ which is optional anchor label
// Email addresses
var emailAddressPattern = /[\w.]+@[a-zA-Z_-]+?(?:\.[a-zA-Z]{2,6})+/gim;
return inputText
.replace(labelPattern, function (all, g1, g2) {
return (
'<a href="' +
g1 +
'" target="_blank" class="link">' +
(g2 || g1) +
"</a>"
);
})
.replace(labelPseudoUrlPattern, function (all, g1, g2, g3, g4) {
return (
g1 +
'<a href="http://' +
g2 +
'" target="_blank" class="link">' +
(g4 || g2) +
"</a>"
);
})
.replace(emailAddressPattern, '<a href="mailto:$&" class="link">$&</a>');
};
exports.stripUser = function (user) {
user = user || {};
return {
id: user.id,
avatarurl: user.avatarurl,
username: user.username,
roles: user.roles || []
}
}