UNPKG

2.94 kBJavaScriptView Raw
1/**
2 * This file has been duplicated in packages/linking-platform/link-picker/src/common/utils/url.ts
3 * Any changes made here should be mirrored there.
4 */
5import LinkifyIt from 'linkify-it';
6var whitelistedURLPatterns = [/^https?:\/\//im, /^ftps?:\/\//im, /^gopher:\/\//im, /^integrity:\/\//im, /^file:\/\//im, /^smb:\/\//im, /^dynamicsnav:\/\//im, /^jamfselfservice:\/\//im, /^\//im, /^mailto:/im, /^skype:/im, /^callto:/im, /^facetime:/im, /^git:/im, /^irc6?:/im, /^news:/im, /^nntp:/im, /^feed:/im, /^cvs:/im, /^svn:/im, /^mvn:/im, /^ssh:/im, /^scp:\/\//im, /^sftp:\/\//im, /^itms:/im, /^notes:/im, /^hipchat:\/\//im, /^sourcetree:/im, /^urn:/im, /^tel:/im, /^xmpp:/im, /^telnet:/im, /^vnc:/im, /^rdp:/im, /^whatsapp:/im, /^slack:/im, /^sips?:/im, /^magnet:/im, /^#/im];
7
8/**
9 * Please notify the Editor Mobile team (Slack: #help-mobilekit) if the logic for this changes.
10 */
11export 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};
20export var linkify = LinkifyIt();
21linkify.add('sourcetree:', 'http:');
22linkify.add('jamfselfservice:', 'http:');
23var tlds = 'biz|com|edu|gov|net|org|pro|web|xxx|aero|asia|coop|info|museum|name|shop|рф'.split('|');
24var 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]';
25tlds.push(tlds2Char);
26linkify.tlds(tlds, false);
27export var LINK_REGEXP = /(https?|ftp|jamfselfservice|gopher|dynamicsnav|integrity|file|smb):\/\/[^\s]+/;
28export 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};
56export 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 * Adds protocol to url if needed.
69 */
70export function normalizeUrl(url) {
71 var match = getLinkMatch(url);
72 return match && match.url || '';
73}
74
75/**
76 * checks if root relative link
77 */
78export function isRootRelative(url) {
79 return url.startsWith('/');
80}
\No newline at end of file