1 |
|
2 |
|
3 |
|
4 |
|
5 | import LinkifyIt from 'linkify-it';
|
6 | var whitelistedURLPatterns = [/^https?:\/\//im, /^ftps?:\/\
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | export var isSafeUrl = function isSafeUrl(url) {
|
12 | var urlTrimmed = url.trim();
|
13 | if (urlTrimmed.length === 0) {
|
14 | return true;
|
15 | }
|
16 | return whitelistedURLPatterns.some(function (p) {
|
17 | return p.test(urlTrimmed);
|
18 | });
|
19 | };
|
20 | export var linkify = LinkifyIt();
|
21 | linkify.add('sourcetree:', 'http:');
|
22 | linkify.add('jamfselfservice:', 'http:');
|
23 | var tlds = 'biz|com|edu|gov|net|org|pro|web|xxx|aero|asia|coop|info|museum|name|shop|рф'.split('|');
|
24 | var tlds2Char = 'a[cdefgilmnoqrtuwxz]|b[abdefghijmnorstvwyz]|c[acdfghiklmnoruvwxyz]|d[ejkmoz]|e[cegrstu]|f[ijkmor]|g[abdefghilmnpqrstuwy]|h[kmnrtu]|i[delmnoqrst]|j[emop]|k[eghimnprwyz]|l[abcikrstuvy]|m[acdeghklmnopqrtuvwxyz]|n[acefgilopruz]|om|p[aefghkmnrtw]|qa|r[eosuw]|s[abcdegijklmnrtuvxyz]|t[cdfghjklmnortvwz]|u[agksyz]|v[aceginu]|w[fs]|y[et]|z[amw]';
|
25 | tlds.push(tlds2Char);
|
26 | linkify.tlds(tlds, false);
|
27 | export var LINK_REGEXP = /(https?|ftp|jamfselfservice|gopher|dynamicsnav|integrity|file|smb):\/\/[^\s]+/;
|
28 | export var linkifyMatch = function linkifyMatch(text) {
|
29 | var matches = [];
|
30 | if (!LINK_REGEXP.test(text)) {
|
31 | return matches;
|
32 | }
|
33 | var startpos = 0;
|
34 | var substr;
|
35 | while (substr = text.substr(startpos)) {
|
36 | var link = (substr.match(LINK_REGEXP) || [''])[0];
|
37 | if (link) {
|
38 | var index = substr.search(LINK_REGEXP);
|
39 | var start = index >= 0 ? index + startpos : index;
|
40 | var end = start + link.length;
|
41 | matches.push({
|
42 | index: start,
|
43 | lastIndex: end,
|
44 | raw: link,
|
45 | url: link,
|
46 | text: link,
|
47 | schema: ''
|
48 | });
|
49 | startpos += end;
|
50 | } else {
|
51 | break;
|
52 | }
|
53 | }
|
54 | return matches;
|
55 | };
|
56 | export function getLinkMatch(str) {
|
57 | if (!str) {
|
58 | return null;
|
59 | }
|
60 | var match = linkifyMatch(str);
|
61 | if (!match.length) {
|
62 | match = linkify.match(str);
|
63 | }
|
64 | return match && match[0];
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | export function normalizeUrl(url) {
|
71 | var match = getLinkMatch(url);
|
72 | return match && match.url || '';
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | export function isRootRelative(url) {
|
79 | return url.startsWith('/');
|
80 | } |
\ | No newline at end of file |