forked from TobitSoftware/chayns-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createLinks.js
27 lines (25 loc) · 1.01 KB
/
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
const URL_REGEX = /(\s|^)(?:http(s)?:\/\/|www\.)[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=.]+/gi;
const makeLinks = (txt) => {
let text = txt;
const urls = text.match(URL_REGEX);
if (chayns.utils.isArray(urls)) {
const indexArray = [];
urls.forEach((url) => {
const space = (url.startsWith('w') || url.startsWith('h') ? 0 : 1);
indexArray.push(text.indexOf(url) + space);
text = text.replace(url, space ? ' ' : '');
});
urls.reverse();
indexArray.reverse();
urls.forEach((url, index) => {
let link = url.substring(url.startsWith('w') || url.startsWith('h') ? 0 : 1);
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;