Skip to content

Commit 04f72b2

Browse files
committed
add: add support for old ios regex
1 parent 182000e commit 04f72b2

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

lib/index.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,24 @@ function exitLiteralAutolink(token) {
126126
this.exit(token)
127127
}
128128

129-
/** @type {FromMarkdownTransform} */
130-
function transformGfmAutolinkLiterals(tree) {
129+
// Regex support detector
130+
const regexSupport = {
131+
lookbehind: (() => {
132+
try {
133+
// Using regex literal instead of RegExp constructor
134+
;/(?<=x)/.test('x')
135+
return true
136+
} catch {
137+
return false
138+
}
139+
})()
140+
}
141+
142+
/**
143+
* Modern version of autolink transform using lookbehind
144+
* @type {FromMarkdownTransform}
145+
*/
146+
function modernAutolinkTransform(tree) {
131147
findAndReplace(
132148
tree,
133149
[
@@ -138,6 +154,45 @@ function transformGfmAutolinkLiterals(tree) {
138154
)
139155
}
140156

157+
/**
158+
* Legacy version of autolink transform for older Node.js versions
159+
* @type {FromMarkdownTransform}
160+
*/
161+
function legacyAutolinkTransform(tree) {
162+
findAndReplace(
163+
tree,
164+
[
165+
[/(https?:\/\/|www(?=\.))([-.\w]+)([^ \t\r\n]*)/gi, findUrl],
166+
[/(^|\s|\p{P}|\p{S})([-.\w+]+)@([-\w]+(?:\.[-\w]+)+)/gu, findEmailLegacy]
167+
],
168+
{ignore: ['link', 'linkReference']}
169+
)
170+
}
171+
172+
/**
173+
* Helper function for legacy email matching
174+
* @param {string} _ - Unused parameter
175+
* @param {string} prefix - Email prefix
176+
* @param {string} name - Email name
177+
* @param {string} domain - Email domain
178+
* @returns {*} The processed email
179+
*/
180+
function findEmailLegacy(_, prefix, name, domain) {
181+
return findEmail(name + '@' + domain)
182+
}
183+
184+
/**
185+
* Main transform function that uses the appropriate version based on regex support
186+
* @type {FromMarkdownTransform}
187+
*/
188+
function transformGfmAutolinkLiterals(tree) {
189+
if (regexSupport.lookbehind) {
190+
modernAutolinkTransform(tree)
191+
} else {
192+
legacyAutolinkTransform(tree)
193+
}
194+
}
195+
141196
/**
142197
* @type {ReplaceFunction}
143198
* @param {string} _

test/index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,15 @@ test('gfmAutolinkLiteralToMarkdown()', async function (t) {
452452
)
453453
})
454454

455+
/**
456+
* Normalizes line endings to Unix style (\n)
457+
* @param {string} text - The text to normalize
458+
* @returns {string} The normalized text
459+
*/
460+
function normalizeLineEndings(text) {
461+
return text.replaceAll('\r\n', '\n')
462+
}
463+
455464
test('fixtures', async function (t) {
456465
const root = new URL('fixture/', import.meta.url)
457466

@@ -469,8 +478,10 @@ test('fixtures', async function (t) {
469478
const inputUrl = new URL(file, root)
470479
const expectedUrl = new URL(stem + '.html', root)
471480

472-
const input = await fs.readFile(inputUrl)
473-
const expected = String(await fs.readFile(expectedUrl))
481+
const input = normalizeLineEndings(String(await fs.readFile(inputUrl)))
482+
const expected = normalizeLineEndings(
483+
String(await fs.readFile(expectedUrl))
484+
)
474485

475486
const mdast = fromMarkdown(input, {
476487
extensions: [gfmAutolinkLiteral()],

0 commit comments

Comments
 (0)