-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcreateLinks.js
32 lines (30 loc) · 980 Bytes
/
createLinks.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
const URL_REGEX =
/(\s|^)(?:http(s)?:\/\/|www\.)[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=.]+/gi;
const makeLinks = (txt) => {
let text = txt;
let urls = text.match(URL_REGEX);
if (chayns.utils.isArray(urls)) {
const indexArray = [];
urls = urls.map((url) => url.trim());
urls.forEach((url) => {
indexArray.push(text.indexOf(url));
text = text.replace(url, '');
});
urls.reverse();
indexArray.reverse();
urls.forEach((url, index) => {
let link = url;
if (!link.startsWith('http')) {
link = `https://${url}`;
}
const position = indexArray[index];
text = [
text.slice(0, position),
`<a onclick="chayns.openUrlInBrowser('${link}')">${url}</a>`,
text.slice(position),
].join('');
});
}
return text;
};
export default makeLinks;