nodebb-plugin-onekite-discord 1.0.9 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/library.js +28 -13
- package/package.json +1 -1
- package/plugin.json +1 -1
package/library.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
1
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
2
|
+
* Normalize links for Discord embeds.
|
|
3
|
+
* Discord does NOT support masked links in embed descriptions.
|
|
4
|
+
* NodeBB may already render links as: texte(url)
|
|
5
|
+
* We always output a plain URL so Discord auto-linkifies it.
|
|
8
6
|
*/
|
|
9
7
|
function formatDiscordLinks(str) {
|
|
10
8
|
if (!str || typeof str !== 'string') return str;
|
|
@@ -12,20 +10,37 @@ function formatDiscordLinks(str) {
|
|
|
12
10
|
const normalizeUrl = (u) => {
|
|
13
11
|
if (!u) return u;
|
|
14
12
|
let url = String(u).trim();
|
|
15
|
-
|
|
16
|
-
// Strip wrapping <> if present
|
|
17
13
|
url = url.replace(/^<(.+)>$/, '$1');
|
|
18
|
-
|
|
19
|
-
// Fix common "https:/" typo
|
|
20
14
|
url = url.replace(/^https:\//i, 'https://');
|
|
21
15
|
url = url.replace(/^http:\//i, 'http://');
|
|
22
|
-
|
|
23
|
-
// If it's a bare domain starting with www., add scheme
|
|
24
16
|
if (/^www\./i.test(url)) url = 'https://' + url;
|
|
25
|
-
|
|
26
17
|
return url;
|
|
27
18
|
};
|
|
28
19
|
|
|
20
|
+
// HTML <a href>
|
|
21
|
+
str = str.replace(/<a\s+[^>]*href=["']([^"']+)["'][^>]*>.*?<\/a>/gi, (_m, href) => {
|
|
22
|
+
return normalizeUrl(href);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// Markdown [text](url)
|
|
26
|
+
str = str.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_m, _text, url) => {
|
|
27
|
+
return normalizeUrl(url);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// Already-rendered form: texte(url)
|
|
31
|
+
str = str.replace(/([^\s(]+)\((https?:\/\/[^)]+|www\.[^)]+)\)/g, (_m, _text, url) => {
|
|
32
|
+
return normalizeUrl(url);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return str;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
'use strict';
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
;
|
|
43
|
+
|
|
29
44
|
// HTML links -> URL
|
|
30
45
|
str = str.replace(/<a\s+[^>]*href=["']([^"']+)["'][^>]*>(.*?)<\/a>/gi, (_m, href, _text) => {
|
|
31
46
|
return normalizeUrl(href);
|
package/package.json
CHANGED
package/plugin.json
CHANGED