Skip to content

perf: regex patch for backward compatibility #4483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 73 additions & 7 deletions patches/[email protected]
Original file line number Diff line number Diff line change
@@ -1,13 +1,79 @@
diff --git a/lib/index.js b/lib/index.js
index c5ca771c24dd914e342f791716a822431ee32b3a..9e9dc090d4be5cf882316ed2eced4c471c40407b 100644
index c5ca771c24dd914e342f791716a822431ee32b3a..457d9f8c4625f7d9c7ea1e9ffc13616db1fc6fef 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -132,7 +132,7 @@ function transformGfmAutolinkLiterals(tree) {
@@ -126,8 +126,37 @@ function exitLiteralAutolink(token) {
this.exit(token)
}

-/** @type {FromMarkdownTransform} */
+// Regex support detector, for backward compatibility
+// Ref: https://github.com/syntax-tree/mdast-util-gfm-autolink-literal/pull/14
+const regexSupport = {
+ lookbehind: (() => {
+ try {
+ // Using regex literal instead of RegExp constructor
+ ;/(?<=x)/.test('x')
+ return true
+ } catch {
+ return false
+ }
+ })()
+}
+
+/**
+ * Main transform function that uses the appropriate version based on regex support
+ * @type {FromMarkdownTransform}
+ */
function transformGfmAutolinkLiterals(tree) {
+ if (regexSupport.lookbehind) {
+ modernAutolinkTransform(tree)
+ } else {
+ legacyAutolinkTransform(tree)
+ }
+}
+
+/**
+ * Modern version of autolink transform using lookbehind
+ * @type {FromMarkdownTransform}
+ */
+function modernAutolinkTransform(tree) {
findAndReplace(
tree,
[
[/(https?:\/\/|www(?=\.))([-.\w]+)([^ \t\r\n]*)/gi, findUrl],
- [/(?<=^|\s|\p{P}|\p{S})([-.\w+]+)@([-\w]+(?:\.[-\w]+)+)/gu, findEmail]
+ [/([-.\w+]+)@([-\w]+(?:\.[-\w]+)+)/g, findEmail]
],
{ignore: ['link', 'linkReference']}
@@ -138,6 +167,35 @@ function transformGfmAutolinkLiterals(tree) {
)
}

+
+/**
+ * Legacy version of autolink transform for older Node.js versions
+ * @type {FromMarkdownTransform}
+ */
+function legacyAutolinkTransform(tree) {
+ findAndReplace(
+ tree,
+ [
+ [/(https?:\/\/|www(?=\.))([-.\w]+)([^ \t\r\n]*)/gi, findUrl],
+ // [/([-.\w+]+)@([-\w]+(?:\.[-\w]+)+)/g, findEmail] # NOTE: original regex in 2.0.0
+ [/(^|\s|\p{P}|\p{S})([-.\w+]+)@([-\w]+(?:\.[-\w]+)+)/gu, findEmailLegacy]
+ ],
+ {ignore: ['link', 'linkReference']}
+ )
+}
+
+/**
+ * Helper function for legacy email matching
+ * @param {string} _ - Unused parameter
+ * @param {string} prefix - Email prefix
+ * @param {string} name - Email name
+ * @param {string} domain - Email domain
+ * @returns {*} The processed email
+ */
+function findEmailLegacy(_, prefix, name, domain) {
+ return findEmail(name + '@' + domain)
+}
+
/**
* @type {ReplaceFunction}
* @param {string} _
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading