An open source design system that's fully customizable and agent ready
TypeScript
fix(Markdown): check a line's trailing spaces in linear time (#7084)
Cindy Zhang0cc31e993bcf
diff --git a/.changeset/markdown-trailing-spaces-linear.md b/.changeset/markdown-trailing-spaces-linear.mdnew file mode 100644index 0000000000..a4b348e941--- /dev/null+++ b/.changeset/markdown-trailing-spaces-linear.md@@ -0,0 +1,8 @@+---+'@astryxdesign/core': patch+---++[fix] Markdown: check a line's trailing spaces in linear time+@cixzhang++`Markdown` no longer slows to seconds on a line holding a long run of spaces before its last word, as a crafted message can: deciding whether trailing spaces make a hard line break now counts them from the end of the line instead of matching a pattern that retried from every space. Line breaks read as before.diff --git a/packages/core/src/Markdown/parser.ts b/packages/core/src/Markdown/parser.tsindex e7ffe94a26..f2e4ffc656 100644--- a/packages/core/src/Markdown/parser.ts+++ b/packages/core/src/Markdown/parser.ts@@ -2437,8 +2437,15 @@ function parseInlineImpl(// line ending, not to the spaces before it.if (end < text.length && text[end] === '\n') {const line = content.endsWith('\r') ? content.slice(0, -1) : content;- const trimmed = line.replace(/ +$/, '');- if (line.length - trimmed.length >= 2) {+ // Counted from the end: a pattern such as / +$/ retries from every+ // space in a long run that does not end the line, so its time grows+ // with the square of the run.+ let spaces = 0;+ while (line[line.length - 1 - spaces] === ' ') {+ spaces++;+ }+ const trimmed = line.slice(0, line.length - spaces);+ if (spaces >= 2) {if (trimmed.length > 0) {appendInlineText(nodes, delimiterNodes, trimmed);}diff --git a/packages/core/src/Markdown/trailingSpaces.test.tsx b/packages/core/src/Markdown/trailingSpaces.test.tsxnew file mode 100644index 0000000000..3c14a93c6b--- /dev/null+++ b/packages/core/src/Markdown/trailingSpaces.test.tsx@@ -0,0 +1,45 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {describe, expect, it} from 'vitest';+import {parseInlineAst} from './parser';++type Json = {type: string; value?: string};++const types = (markdown: string): string[] =>+ (parseInlineAst(markdown) as unknown as Json[]).map(node =>+ node.type === 'text' ? `text(${node.value})` : node.type,+ );++describe('a hard line break from trailing spaces (CommonMark 0.31 §6.7)', () => {+ it.each([+ ['two spaces break the line', 'a \nb', ['text(a)', 'break', 'text(b)']],+ ['more spaces break it too', 'a \nb', ['text(a)', 'break', 'text(b)']],+ ['one space is a soft break', 'a \nb', ['text(a \nb)']],+ [+ 'a CRLF line ending keeps its break',+ 'a \r\nb',+ ['text(a)', 'break', 'text(b)'],+ ],+ ['spaces inside the line stay text', 'a b\nc', ['text(a b\nc)']],+ ])('%s: %j', (_, markdown, expected) => {+ expect(types(markdown)).toEqual(expected);+ });++ it.each([+ ['one line with a long run of spaces', `a${' '.repeat(40_000)}b\nc`],+ ['many lines with runs of spaces', `a${' '.repeat(1_000)}b\n`.repeat(40)],+ ['a long run of spaces ending the line', `a${' '.repeat(40_000)}\nb`],+ ])('reads %s (40 KB) within 100 ms', (_, markdown) => {+ // CPU time, not elapsed time: on a loaded test machine, other work+ // stretches elapsed time but not the time this parse spends computing.+ parseInlineAst(markdown);+ let least = Number.POSITIVE_INFINITY;+ for (let round = 0; round < 5; round++) {+ const started = process.cpuUsage();+ parseInlineAst(markdown);+ const used = process.cpuUsage(started);+ least = Math.min(least, (used.user + used.system) / 1000);+ }+ expect(least).toBeLessThan(100);+ });+});