Skip to content

Commit 2dc3cb7

Browse files
authored
fix: add backward compatibility patch for regex lookbehind in autolink literals (#4483)
1 parent 431390f commit 2dc3cb7

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

patches/[email protected]

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,79 @@
11
diff --git a/lib/index.js b/lib/index.js
2-
index c5ca771c24dd914e342f791716a822431ee32b3a..9e9dc090d4be5cf882316ed2eced4c471c40407b 100644
2+
index c5ca771c24dd914e342f791716a822431ee32b3a..457d9f8c4625f7d9c7ea1e9ffc13616db1fc6fef 100644
33
--- a/lib/index.js
44
+++ b/lib/index.js
5-
@@ -132,7 +132,7 @@ function transformGfmAutolinkLiterals(tree) {
5+
@@ -126,8 +126,37 @@ function exitLiteralAutolink(token) {
6+
this.exit(token)
7+
}
8+
9+
-/** @type {FromMarkdownTransform} */
10+
+// Regex support detector, for backward compatibility
11+
+// Ref: https://github.com/syntax-tree/mdast-util-gfm-autolink-literal/pull/14
12+
+const regexSupport = {
13+
+ lookbehind: (() => {
14+
+ try {
15+
+ // Using regex literal instead of RegExp constructor
16+
+ ;/(?<=x)/.test('x')
17+
+ return true
18+
+ } catch {
19+
+ return false
20+
+ }
21+
+ })()
22+
+}
23+
+
24+
+/**
25+
+ * Main transform function that uses the appropriate version based on regex support
26+
+ * @type {FromMarkdownTransform}
27+
+ */
28+
function transformGfmAutolinkLiterals(tree) {
29+
+ if (regexSupport.lookbehind) {
30+
+ modernAutolinkTransform(tree)
31+
+ } else {
32+
+ legacyAutolinkTransform(tree)
33+
+ }
34+
+}
35+
+
36+
+/**
37+
+ * Modern version of autolink transform using lookbehind
38+
+ * @type {FromMarkdownTransform}
39+
+ */
40+
+function modernAutolinkTransform(tree) {
41+
findAndReplace(
642
tree,
743
[
8-
[/(https?:\/\/|www(?=\.))([-.\w]+)([^ \t\r\n]*)/gi, findUrl],
9-
- [/(?<=^|\s|\p{P}|\p{S})([-.\w+]+)@([-\w]+(?:\.[-\w]+)+)/gu, findEmail]
10-
+ [/([-.\w+]+)@([-\w]+(?:\.[-\w]+)+)/g, findEmail]
11-
],
12-
{ignore: ['link', 'linkReference']}
44+
@@ -138,6 +167,35 @@ function transformGfmAutolinkLiterals(tree) {
1345
)
46+
}
47+
48+
+
49+
+/**
50+
+ * Legacy version of autolink transform for older Node.js versions
51+
+ * @type {FromMarkdownTransform}
52+
+ */
53+
+function legacyAutolinkTransform(tree) {
54+
+ findAndReplace(
55+
+ tree,
56+
+ [
57+
+ [/(https?:\/\/|www(?=\.))([-.\w]+)([^ \t\r\n]*)/gi, findUrl],
58+
+ // [/([-.\w+]+)@([-\w]+(?:\.[-\w]+)+)/g, findEmail] # NOTE: original regex in 2.0.0
59+
+ [/(^|\s|\p{P}|\p{S})([-.\w+]+)@([-\w]+(?:\.[-\w]+)+)/gu, findEmailLegacy]
60+
+ ],
61+
+ {ignore: ['link', 'linkReference']}
62+
+ )
63+
+}
64+
+
65+
+/**
66+
+ * Helper function for legacy email matching
67+
+ * @param {string} _ - Unused parameter
68+
+ * @param {string} prefix - Email prefix
69+
+ * @param {string} name - Email name
70+
+ * @param {string} domain - Email domain
71+
+ * @returns {*} The processed email
72+
+ */
73+
+function findEmailLegacy(_, prefix, name, domain) {
74+
+ return findEmail(name + '@' + domain)
75+
+}
76+
+
77+
/**
78+
* @type {ReplaceFunction}
79+
* @param {string} _

pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)