An open source design system that's fully customizable and agent ready
TypeScript
fix(Markdown): GFM autolink parses long text in linear time (#9990)
Cindy Zhangbf743545e95d
diff --git a/.changeset/markdown-autolink-linear.md b/.changeset/markdown-autolink-linear.mdnew file mode 100644index 0000000000..cf3858dae8--- /dev/null+++ b/.changeset/markdown-autolink-linear.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Markdown's GFM autolink option (`autolink: 'gfm'`) no longer slows to seconds on long text. The bare-email scan started over at every character of a long run, so its time grew with the square of the run: a 100,000-character word took over 10 seconds. It now starts from each `@`, and trailing `)` are peeled off a link in one pass, so a long run costs about as much as a plain parse. Linked emails and URLs are unchanged.++@cixzhangdiff --git a/docs/specs/components/Markdown/Markdown.spec.md b/docs/specs/components/Markdown/Markdown.spec.mdindex c0e2a11327..2d15a40607 100644--- a/docs/specs/components/Markdown/Markdown.spec.md+++ b/docs/specs/components/Markdown/Markdown.spec.md@@ -341,6 +341,9 @@ text. Outside a table cell, inline code retains its authored backslashes.### Performance and resources- Math scanning is disabled unless requested.+- GFM autolink scanning is linear in the input length: the email and URL+ literal scans are anchored, so a long plain run costs about as much as a+ plain parse.- Inline matching is a bounded forward scan of one line. Display matching scansonly from a candidate `$$` opener to its closer.- The incremental parser keeps completed blocks cached, treats an open display@@ -471,6 +474,7 @@ and this change preserves the existing spelling exactly.| Public syntax/types | `Markdown.public.test.ts`, core typecheck, and `Markdown.doc.mjs` | Legacy exhaustive switches, math opt-ins, inferred extension-node unions | A released union widens, an enabled union loses nodes, or docs drift from declarations. || Navigation contract | `parser.test.ts`, `Markdown.test.tsx`, and `Markdown.renderBoundary.test.tsx` | Parsed and rendered links, including transformed built-in links; accepted ordinary schemes; rejected destinations; links versus images | A blocked destination reaches navigation or a custom link renderer, or resource policy narrows accepted navigation. || Security/accessibility | `parser.test.ts`, `Markdown.test.tsx`, and renderer guidance | Inert expression strings and renderer-owned semantics | Astryx executes math as HTML or silently claims renderer-owned accessibility. |+| Autolink scan cost | `parser.perf.test.ts` "GFM autolink scanning is linear in the input length" (100,000-character runs parse with autolink within a fixed budget) and `parser.test.ts` "finds the same bare emails as a scan from every character" | A long plain run, a run with an `@` and no dotted domain, a run followed by an email and a URL, and a URL closed by a long run of `)` | A scan whose cost grows with the square of a run exceeds the budget; an anchored scan that finds different emails fails the comparison |Focused tests continue to pin all nine current target names and default blockplacement. Math intentionally adds no target and no default anatomy.diff --git a/packages/core/src/Markdown/parser.perf.test.ts b/packages/core/src/Markdown/parser.perf.test.tsindex f385cee45c..c4f01fd67e 100644--- a/packages/core/src/Markdown/parser.perf.test.ts+++ b/packages/core/src/Markdown/parser.perf.test.ts@@ -2,6 +2,7 @@import {describe, it, expect} from 'vitest';import {+ parseInline,parseMarkdown,parseMarkdownIncremental,createIncrementalState,@@ -396,3 +397,28 @@ describe('parseMarkdownIncremental cache', () => {expect(withRanges).toBe(without);});});++describe('GFM autolink scanning is linear in the input length (Markdown Performance)', () => {+ // A scan that restarts at every character of a long run grows with the+ // square of the run: 100,000 characters took seconds. A linear scan takes+ // milliseconds, so this budget holds on a loaded machine.+ const BUDGET_MS = 1000;++ it.each([+ ['a long plain word', 'a'.repeat(100_000)],+ [+ 'a long word with an @ and no dotted domain',+ `${'a'.repeat(50_000)}@${'b'.repeat(50_000)}`,+ ],+ [+ 'a long word, then an email and a URL',+ `${'a'.repeat(100_000)} [email protected] https://example.com`,+ ],+ ['a URL closed by a long run of )', `https://a${')'.repeat(100_000)}`],+ ])('parses %s with autolink within the budget', (_label, text) => {+ const startedAt = performance.now();+ const nodes = parseInline(text, {autolink: 'gfm'});+ expect(performance.now() - startedAt).toBeLessThan(BUDGET_MS);+ expect(nodes.length).toBeGreaterThan(0);+ });+});diff --git a/packages/core/src/Markdown/parser.test.ts b/packages/core/src/Markdown/parser.test.tsindex aca90bfea3..c97565a06a 100644--- a/packages/core/src/Markdown/parser.test.ts+++ b/packages/core/src/Markdown/parser.test.ts@@ -1309,6 +1309,58 @@ describe('citation parsing', () => {}});+ it('finds the same bare emails as a scan from every character', () => {+ // The reference is the unanchored pattern the scan from each `@`+ // replaced, with the same boundary checks.+ const referenceEmails = (text: string): string[] => {+ const found: string[] = [];+ const re = /[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+/g;+ let m: RegExpExecArray | null;+ while ((m = re.exec(text)) !== null) {+ const prev = text[m.index - 1];+ if (prev != null && /[A-Za-z0-9._%+\-@]/.test(prev)) {+ continue;+ }+ if (text.slice(Math.max(0, m.index - 7), m.index) === 'mailto:') {+ continue;+ }+ found.push(m[0]);+ }+ return found;+ };+ const linkedEmails = (text: string): string[] =>+ parseInline(text, {autolink: 'gfm'}).flatMap(node =>+ node.type === 'link' && node.href.startsWith('mailto:')+ ? [node.href.slice('mailto:'.length)]+ : [],+ );++ const cases = [+ '[email protected]',+ '[email protected]@host.com',+ 'a@[email protected]',+ 'mailto:[email protected] and [email protected]',+ 'ab@cd [email protected].',+ '[email protected]@e.f',+ '@a.b x@ [email protected] [email protected]',+ ];+ // A seeded walk over the characters that matter to the scan.+ let seed = 7;+ const alphabet = 'ab.@+-_ %:';+ for (let n = 0; n < 300; n++) {+ let text = '';+ for (let i = 0; i < 24; i++) {+ seed = (seed * 1103515245 + 12345) % 2147483648;+ text += alphabet[seed % alphabet.length];+ }+ cases.push(text);+ }+ for (const text of cases) {+ expect(linkedEmails(text), text).toEqual(referenceEmails(text));+ }+ });+it('parses <scheme:url> angle-bracket autolinks', () => {const result = parseInline('see <https://example.com> ok', {autolink: 'gfm',diff --git a/packages/core/src/Markdown/parser.ts b/packages/core/src/Markdown/parser.tsindex 87dab10c58..f4d8242d3f 100644--- a/packages/core/src/Markdown/parser.ts+++ b/packages/core/src/Markdown/parser.ts@@ -2748,11 +2748,12 @@ const URL_LITERAL_RE = new RegExp(/** Bare www. URL. Resulting href gets `http://` prepended. */const WWW_LITERAL_RE = new RegExp(`www\\.${ANY_NON_WHITESPACE_OR_ANGLE}`, 'g');-/** Bare email: local-part@domain (at least one dot in domain). */-const EMAIL_LITERAL_RE = new RegExp(- `${EMAIL_LOCAL_PART}@${DOMAIN_WITH_DOT}`,- 'g',-);+/**+ * The domain of a bare email, with at least one dot. The scan matches it after+ * each `@`. A pattern that starts at every character rereads a long run each+ * time, so its cost grows with the square of the run.+ */+const EMAIL_DOMAIN_RE = new RegExp(DOMAIN_WITH_DOT, 'y');/** Angle-bracket autolink: `<scheme:url>` (CommonMark §6.5). */const ANGLE_URL_RE = new RegExp(`<(${ANGLE_SCHEME}:${ANGLE_URL_BODY})>`, 'g');@@ -2771,6 +2772,11 @@ const EMAIL_LOCAL_CHARS = new Set('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._%+-@',);+/** The characters `EMAIL_LOCAL_PART` allows. */+const EMAIL_LOCAL_PART_CHARS = new Set(+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._%+-',+);+interface AutolinkMatch {start: number;end: number;@@ -2786,35 +2792,30 @@ interface AutolinkMatch {* `https://example.com/Foo_(bar)` keeps the inner pair).*/function peelTrailingPunctAndParens(url: string): string {- let s = url;- while (s.length > 0) {- // Peel trailing punctuation characters in one pass.- if (TRAILING_PUNCT_CHARS.has(s[s.length - 1])) {- let end = s.length - 1;- while (end > 0 && TRAILING_PUNCT_CHARS.has(s[end - 1])) {- end--;- }- s = s.slice(0, end);- continue;- }- if (s.endsWith(')')) {- let open = 0;- let close = 0;- for (let idx = 0; idx < s.length; idx++) {- if (s[idx] === '(') {- open++;- } else if (s[idx] === ')') {- close++;- }- }- if (close > open) {- s = s.slice(0, -1);- continue;- }+ // Each peeled `)` lowers the close count instead of a recount, so a long+ // run of `)` costs one pass.+ let open = 0;+ let close = 0;+ for (let idx = 0; idx < url.length; idx++) {+ if (url[idx] === '(') {+ open++;+ } else if (url[idx] === ')') {+ close++;+ }+ }+ let end = url.length;+ while (end > 0) {+ const ch = url[end - 1];+ if (TRAILING_PUNCT_CHARS.has(ch)) {+ end--;+ } else if (ch === ')' && close > open) {+ close--;+ end--;+ } else {+ break;}- break;}- return s;+ return url.slice(0, end);}/** Characters that, when preceding a URL, indicate it's part of a larger token. */@@ -2922,26 +2923,48 @@ function scanAutolinksInText(text: string): AutolinkMatch[] {}}- // bare email+ // bare email: local-part@domain, found from each `@`{- const re = new RegExp(EMAIL_LITERAL_RE.source, 'g');- let m: RegExpExecArray | null;- while ((m = re.exec(text)) !== null) {- const prev = text[m.index - 1];+ // A match, accepted or not, ends where the next one may start.+ let consumedTo = 0;+ for (+ let at = text.indexOf('@');+ at !== -1;+ at = text.indexOf('@', at + 1)+ ) {+ let start = at;+ while (+ start > consumedTo &&+ EMAIL_LOCAL_PART_CHARS.has(text[start - 1])+ ) {+ start--;+ }+ if (start === at) {+ continue;+ }+ EMAIL_DOMAIN_RE.lastIndex = at + 1;+ const domain = EMAIL_DOMAIN_RE.exec(text);+ if (domain === null) {+ continue;+ }+ const end = at + 1 + domain[0].length;+ consumedTo = end;+ const prev = text[start - 1];// Reject if previous char could be part of the local part or sits in// a position that would make this match continuation of something// bigger (`[email protected]@host`, `foo+bar@x`, `mailto:user@host`).if (prev != null && EMAIL_LOCAL_CHARS.has(prev)) {continue;}- if (text.slice(Math.max(0, m.index - 7), m.index) === 'mailto:') {+ if (text.slice(Math.max(0, start - 7), start) === 'mailto:') {continue;}+ const email = text.slice(start, end);matches.push({- start: m.index,- end: m.index + m[0].length,- href: `mailto:${m[0]}`,- display: m[0],+ start,+ end,+ href: `mailto:${email}`,+ display: email,});}}