Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Markdown): keep lazy continuation lines fast in deeply nested input (#7080)

Cindy Zhang67efe5b33297
diff --git a/.changeset/markdown-lazy-continuation-linear.md b/.changeset/markdown-lazy-continuation-linear.md
new file mode 100644
index 0000000000..8bc88a975a
--- /dev/null
+++ b/.changeset/markdown-lazy-continuation-linear.md
@@ -0,0 +1,8 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Markdown: keep lazy continuation lines fast in deeply nested input
+@cixzhang
+
+`Markdown` no longer slows to seconds on a deeply nested blockquote or list followed by a lazy continuation line, as a crafted message can be: checking whether the line continues a paragraph now reuses what the parser already read at each level, and checking a long line for a thematic break no longer copies it. Lists and blockquotes nest at most exactly 100 levels deep.
diff --git a/packages/core/src/Markdown/__tests__/MarkdownNesting.a11y.chromium.spec.ts b/packages/core/src/Markdown/__tests__/MarkdownNesting.a11y.chromium.spec.ts
index 124362ef99..ab82c9aa3d 100644
--- a/packages/core/src/Markdown/__tests__/MarkdownNesting.a11y.chromium.spec.ts
+++ b/packages/core/src/Markdown/__tests__/MarkdownNesting.a11y.chromium.spec.ts
@@ -94,7 +94,7 @@ test.describe('Markdown renders deeply nested input', () => {
await expect(page.locator('#storybook-root')).toContainText('deepest');
const depth = await renderedDepth(page);
expect(depth).toBeGreaterThan(50);
- expect(depth).toBeLessThanOrEqual(101);
+ expect(depth).toBeLessThanOrEqual(100);
expect(errors).toEqual([]);
});
}
diff --git a/packages/core/src/Markdown/blockNesting.test.tsx b/packages/core/src/Markdown/blockNesting.test.tsx
index 37d6870b5a..975aed5684 100644
--- a/packages/core/src/Markdown/blockNesting.test.tsx
+++ b/packages/core/src/Markdown/blockNesting.test.tsx
@@ -56,12 +56,13 @@ function parseTime(markdown: string): number {
}
describe('lists and blockquotes nest at most 100 deep', () => {
- it('nests a list 100 deep exactly, and reads deeper items as text', () => {
+ it('nests a list at most 100 deep, and reads deeper items as text', () => {
const at = (levels: number) =>
blockDepth(parseMarkdownAst(indentedList(levels)).children as never);
expect(at(50)).toBe(50);
- expect(at(101)).toBe(101);
- expect(at(150)).toBe(101);
+ expect(at(100)).toBe(100);
+ expect(at(101)).toBe(100);
+ expect(at(150)).toBe(100);
});
it.each([
@@ -76,7 +77,7 @@ describe('lists and blockquotes nest at most 100 deep', () => {
expect(() => parseMarkdownAst(markdown)).not.toThrow();
const depth = blockDepth(parseMarkdownAst(markdown).children as never);
expect(depth).toBeGreaterThan(50);
- expect(depth).toBeLessThanOrEqual(101);
+ expect(depth).toBeLessThanOrEqual(100);
// The budget leaves room for a loaded test machine.
expect(parseTime(markdown)).toBeLessThan(5000);
});
@@ -93,3 +94,33 @@ describe('lists and blockquotes nest at most 100 deep', () => {
},
);
});
+
+describe('lazy continuation lines in deeply nested input', () => {
+ it.each([
+ ['blockquotes', `${'> '.repeat(20_000)}a\nlazy`],
+ ['lists', `${'- '.repeat(20_000)}a\nlazy`],
+ [
+ 'lists and blockquotes',
+ `${Array.from({length: 20_000}, (_, level) => (level % 2 === 0 ? '> ' : '- ')).join('')}a\nlazy`,
+ ],
+ ['blockquotes, with two lazy lines', `${'> '.repeat(20_000)}a\nlazy\nmore`],
+ ])('parse %s 20,000 deep (40 KB) within 100 ms', (_, markdown) => {
+ parseMarkdownAst(markdown);
+ let fastest = Number.POSITIVE_INFINITY;
+ for (let round = 0; round < 5; round++) {
+ const started = performance.now();
+ parseMarkdownAst(markdown);
+ fastest = Math.min(fastest, performance.now() - started);
+ }
+ expect(fastest).toBeLessThan(100);
+ });
+
+ it('still continue the innermost paragraph', () => {
+ const [quote] = parseMarkdownAst('> > a\nlazy').children as never as Json[];
+ expect(quote.type).toBe('blockquote');
+ const [inner] = quote.children ?? [];
+ expect(inner.type).toBe('blockquote');
+ expect(inner.children?.[0]?.type).toBe('paragraph');
+ expect(JSON.stringify(inner.children)).toContain('lazy');
+ });
+});
diff --git a/packages/core/src/Markdown/parser.ts b/packages/core/src/Markdown/parser.ts
index bcd9df6e59..df6e8afad0 100644
--- a/packages/core/src/Markdown/parser.ts
+++ b/packages/core/src/Markdown/parser.ts
@@ -568,14 +568,33 @@ type ResolvedOptions = {
* render.
*/
readonly blockDepth: number;
+ /**
+ * The blocks each nested parse inside a lazy-continuation probe produced,
+ * keyed by depth and input, shared by the whole document. Internal only:
+ * a probe asks whether nested content ends in a paragraph, and finds the
+ * answer here when an earlier probe already read the same content one
+ * level up, so probes at every nesting level cost no more than one parse.
+ * Parses outside a probe record nothing.
+ */
+ readonly nestedParses?: {
+ map: Map<string, MarkdownAstBlockContent<RuntimeExtensionNode>[]> | null;
+ };
+ /** Whether this parse runs inside a lazy-continuation probe. */
+ readonly probing?: boolean;
};
/**
* The deepest lists and blockquotes may nest, as emphasis is capped
- * (MAX_EMPHASIS_DEPTH). Content nested deeper is one paragraph of text.
+ * (MAX_EMPHASIS_DEPTH): at most 100 levels. Content nested deeper is one
+ * paragraph of text.
*/
const MAX_BLOCK_NESTING = 100;
+/** The key a nested parse of `input` at `depth` is remembered under. */
+function nestedParseKey(depth: number, input: string): string {
+ return `${depth}\u0000${input}`;
+}
+
/**
* Every resolved options object is built here, so all of them share one key
* set in one order — and therefore one hidden class. The parser reads these
@@ -599,6 +618,8 @@ function makeResolvedOptions(
baseOffset: fields.baseOffset,
linkDefs: fields.linkDefs,
blockDepth: fields.blockDepth ?? 0,
+ nestedParses: fields.nestedParses,
+ probing: fields.probing,
};
}
@@ -2646,23 +2667,28 @@ function getIndent(line: string): number {
/** HR: 3+ identical markers (-, *, _) optionally separated by spaces. */
function isHorizontalRule(line: string): boolean {
const trimmed = line.trim();
- if (trimmed.length < 3) {
- return false;
- }
- const stripped = trimmed.replace(/ /g, '');
- if (stripped.length < 3) {
- return false;
- }
- const ch = stripped[0];
- if (ch !== '-' && ch !== '*' && ch !== '_') {
+ const ch = trimmed[0];
+ if (
+ trimmed.length < 3 ||
+ (ch !== '-' && ch !== '*' && ch !== '_') ||
+ !trimmed.endsWith(ch)
+ ) {
return false;
}
- for (let idx = 1; idx < stripped.length; idx++) {
- if (stripped[idx] !== ch) {
+ // Three or more of one marker, spaces between allowed. Both ends are
+ // checked first and the scan stops at the first other character, so a long
+ // line that is not a rule costs next to nothing — this runs on every line
+ // at every nesting level.
+ let count = 0;
+ for (let idx = 0; idx < trimmed.length; idx++) {
+ const character = trimmed[idx];
+ if (character === ch) {
+ count++;
+ } else if (character !== ' ') {
return false;
}
}
- return true;
+ return count >= 3;
}
/**
@@ -2801,7 +2827,17 @@ function endsInParagraph(
}
function sourceEndsInParagraph(source: string, opts: ResolvedOptions): boolean {
- return endsInParagraph(parseMarkdownImpl(source, nested(opts)));
+ // A parse at the next depth may already have read this content: the probe
+ // at each nesting level then costs a lookup, not another parse of every
+ // level below it.
+ const record = opts.nestedParses;
+ if (record != null) {
+ record.map ??= new Map();
+ }
+ const known = record?.map?.get(nestedParseKey(opts.blockDepth + 1, source));
+ return endsInParagraph(
+ known ?? parseMarkdownImpl(source, {...nested(opts), probing: true}),
+ );
}
function splitTableRow(line: string): string[] {
@@ -3136,15 +3172,21 @@ export function parseMarkdownAstInternal(
function parseMarkdownImpl(
input: string,
- baseOpts: ResolvedOptions,
+ callerOpts: ResolvedOptions,
): MarkdownAstBlockContent<RuntimeExtensionNode>[] {
- if (baseOpts.blockDepth > MAX_BLOCK_NESTING) {
+ // Content at depth 100 sits inside 100 lists or blockquotes: the cap.
+ if (callerOpts.blockDepth >= MAX_BLOCK_NESTING) {
// Nested deeper than the cap: the content is one paragraph of text.
const text = input.trim();
return text === ''
? []
- : [{type: 'paragraph', children: parseInlineEntry(text, baseOpts)}];
+ : [{type: 'paragraph', children: parseInlineEntry(text, callerOpts)}];
}
+ // One document's nested parses share one record of what they produced.
+ const baseOpts: ResolvedOptions =
+ callerOpts.nestedParses == null
+ ? {...callerOpts, nestedParses: {map: null}}
+ : callerOpts;
// Collect this input's link reference definitions and strip their lines,
// then merge them with any definitions inherited from an enclosing parse
// (the incremental parser passes the whole document's definitions in; a
@@ -3486,6 +3528,10 @@ function parseMarkdownImpl(
opts,
);
}
+ const nestedParses = baseOpts.nestedParses?.map;
+ if (baseOpts.probing === true && nestedParses != null) {
+ nestedParses.set(nestedParseKey(baseOpts.blockDepth, input), blocks);
+ }
return blocks;
}