-
Notifications
You must be signed in to change notification settings - Fork 2
/
Links.ts
98 lines (90 loc) · 2.74 KB
/
Links.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Common
*/
// cSpell:ignore linkify
import LinkifyIt from "linkify-it";
const linkify = new LinkifyIt({ fuzzyLink: false });
linkify
.add("pw:", {
validate: (
text: string,
pos: number,
self: LinkifyIt.LinkifyIt
): number => {
const tail = text.slice(pos);
// istanbul ignore else
if (!self.re.pw) {
self.re.pw = new RegExp(
`(//|\\\\\\\\)${self.re.src_host}:` +
// Regex for path according to RFC 3986 standards plus the possibility to write '{}' brackets for ProjectWise monikers
`([a-zA-Z0-9-._~!$&'()*+,;=@%{}]+/)+[a-zA-Z0-9-._~!$&'()*+,;=@%{}]*`,
"i"
);
}
// istanbul ignore else
if (self.re.pw.test(tail)) {
const matches = tail.match(self.re.pw);
// istanbul ignore else
if (matches !== null) return matches[0].length;
}
// istanbul ignore next
return 0;
},
})
/* Adding a custom schema for links that start with `www.`.
With fuzzyLink option set to true it matches text without `www.` as a prefix, which is not wanted in our case. */
.add("www.", {
validate: (
text: string,
pos: number,
self: LinkifyIt.LinkifyIt
): number => {
const tail = text.slice(pos);
if (!self.re.www) {
self.re.www = new RegExp(
`^${self.re.src_auth}${self.re.src_host_port_strict}${self.re.src_path}`,
"i"
);
}
// istanbul ignore else
if (self.re.www.test(tail)) {
const matches = tail.match(self.re.www);
// istanbul ignore else
if (matches !== null) return matches[0].length;
}
// istanbul ignore next
return 0;
},
normalize: (match: LinkifyIt.Match) => {
match.schema = "http:";
match.url = `http://${match.url}`;
},
});
/** Returns a list of links from a text
* @public
*/
export const matchLinks = (
text: string
): Array<{ index: number; lastIndex: number; schema: string; url: string }> => {
const head = text.slice(0, 5);
if (head === "pw://" || head === "pw:\\\\")
return Array({
index: 0,
lastIndex: text.length,
schema: "pw:",
url: text,
});
const matches = linkify.match(text);
return matches
? (matches as Array<{
index: number;
lastIndex: number;
schema: string;
url: string;
}>)
: [];
};