|
|
@@ -1,5 +1,22 @@
|
|
1
|
1
|
import { regexes } from './smileys';
|
|
2
|
2
|
|
|
|
3
|
+/* eslint-disable no-useless-escape, max-len */
|
|
|
4
|
+const replacePatterns = {
|
|
|
5
|
+
|
|
|
6
|
+ // URLs starting with http://, https://, or ftp://
|
|
|
7
|
+ '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>':
|
|
|
8
|
+ /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim,
|
|
|
9
|
+
|
|
|
10
|
+ // URLs starting with "www." (without // before it, or it'd re-link the ones done above).
|
|
|
11
|
+ '$1<a href="https://$2" target="_blank" rel="noopener noreferrer">$2</a>':
|
|
|
12
|
+ /(^|[^\/])(www\.[\S]+(\b|$))/gim,
|
|
|
13
|
+
|
|
|
14
|
+ // Change email addresses to mailto: links.
|
|
|
15
|
+ '<a href="mailto:$1">$1</a>':
|
|
|
16
|
+ /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim
|
|
|
17
|
+};
|
|
|
18
|
+/* eslint-enable no-useless-escape, max-len */
|
|
|
19
|
+
|
|
3
|
20
|
/**
|
|
4
|
21
|
* Processes links and smileys in "body".
|
|
5
|
22
|
*
|
|
|
@@ -8,39 +25,38 @@ import { regexes } from './smileys';
|
|
8
|
25
|
*/
|
|
9
|
26
|
export function processReplacements(body) {
|
|
10
|
27
|
// make links clickable + add smileys
|
|
11
|
|
- return smilify(linkify(body));
|
|
12
|
|
-}
|
|
13
|
|
-
|
|
14
|
|
-
|
|
15
|
|
-/**
|
|
16
|
|
- * Finds and replaces all links in the links in "body" with an href tag.
|
|
17
|
|
- *
|
|
18
|
|
- * @param {string} inputText - The message body.
|
|
19
|
|
- * @returns {string} The text replaced with HTML tags for links.
|
|
20
|
|
- */
|
|
21
|
|
-function linkify(inputText) {
|
|
22
|
|
- let replacedText;
|
|
23
|
28
|
|
|
24
|
|
- /* eslint-disable no-useless-escape, max-len */
|
|
25
|
|
-
|
|
26
|
|
- // URLs starting with http://, https://, or ftp://
|
|
27
|
|
- const replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
|
|
|
29
|
+ // non of the patterns we search contains a space, that's why we tokenize it
|
|
|
30
|
+ // and after processing each token we join it again with the results
|
|
|
31
|
+ // making sure we do only one replacement for a token
|
|
|
32
|
+ const tokens = body.split(' ');
|
|
|
33
|
+ const resultText = [];
|
|
28
|
34
|
|
|
29
|
|
- replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>');
|
|
|
35
|
+ for (const token of tokens) {
|
|
|
36
|
+ let replacedText;
|
|
|
37
|
+ const tokenLength = token.length;
|
|
30
|
38
|
|
|
31
|
|
- // URLs starting with "www." (without // before it, or it'd re-link the ones done above).
|
|
32
|
|
- const replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
|
|
|
39
|
+ for (const newString in replacePatterns) { // eslint-disable-line guard-for-in, max-len
|
|
|
40
|
+ const replacePattern = replacePatterns[newString];
|
|
33
|
41
|
|
|
34
|
|
- replacedText = replacedText.replace(replacePattern2, '$1<a href="https://$2" target="_blank" rel="noopener noreferrer">$2</a>');
|
|
|
42
|
+ replacedText = token.replace(replacePattern, newString);
|
|
35
|
43
|
|
|
36
|
|
- // Change email addresses to mailto: links.
|
|
37
|
|
- const replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
|
|
|
44
|
+ // replacement was done, stop doing any other replacements
|
|
|
45
|
+ if (replacedText.length > tokenLength) {
|
|
|
46
|
+ break;
|
|
|
47
|
+ }
|
|
|
48
|
+ replacedText = null;
|
|
|
49
|
+ }
|
|
38
|
50
|
|
|
39
|
|
- replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
|
|
|
51
|
+ // no replacement was done, then just check for smiley
|
|
|
52
|
+ if (!replacedText) {
|
|
|
53
|
+ replacedText = smilify(token);
|
|
|
54
|
+ }
|
|
40
|
55
|
|
|
41
|
|
- /* eslint-enable no-useless-escape */
|
|
|
56
|
+ resultText.push(replacedText);
|
|
|
57
|
+ }
|
|
42
|
58
|
|
|
43
|
|
- return replacedText;
|
|
|
59
|
+ return resultText.join(' ');
|
|
44
|
60
|
}
|
|
45
|
61
|
|
|
46
|
62
|
/**
|