An open source design system that's fully customizable and agent ready
TypeScript
fix(Markdown): keep a streamed document's first-line indentation, as the full parse does (#7104)
Cindy Zhang1c58ef6308dc
diff --git a/.changeset/markdown-streaming-first-line-indent.md b/.changeset/markdown-streaming-first-line-indent.mdnew file mode 100644index 0000000000..4ba1a409b6--- /dev/null+++ b/.changeset/markdown-streaming-first-line-indent.md@@ -0,0 +1,8 @@+---+'@astryxdesign/core': patch+---++[fix] Markdown: keep the indentation of a message's first line+@cixzhang++`Markdown` now keeps the indentation of a message's first line, as it already did for every later line: two bullets indented by the same amount stay side by side, an indented numbered list keeps each item, and an indented table keeps its columns. A message that started with indented content used to render it differently from the same text after its first line.diff --git a/packages/core/src/Markdown/incrementalIndentation.test.tsx b/packages/core/src/Markdown/incrementalIndentation.test.tsxnew file mode 100644index 0000000000..98ff59d2e1--- /dev/null+++ b/packages/core/src/Markdown/incrementalIndentation.test.tsx@@ -0,0 +1,36 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {describe, expect, it} from 'vitest';+import {+ createIncrementalState,+ parseMarkdown,+ parseMarkdownIncremental,+} from './parser';++describe("a streamed document keeps its first line's indentation", () => {+ it.each([+ ['sibling items indented two spaces', ' - a\n - b'],+ ['ordered items indented one space', ' 1. a\n 2. b'],+ ['a paragraph indented one space', ' 1'],+ ['two paragraphs, each indented', ' a\n\n b'],+ ['a table indented two spaces', ' | a | b |\n | - | - |\n | 1 | 2 |'],+ ['a heading marker indented three spaces', ' # h'],+ ])('gives %s the full parse, at once and streamed', (_, markdown) => {+ const full = parseMarkdown(markdown);+ expect(+ parseMarkdownIncremental(markdown, createIncrementalState()),+ ).toEqual(full);+ const state = createIncrementalState();+ let streamed = parseMarkdownIncremental('', state);+ for (let end = 1; end <= markdown.length; end++) {+ streamed = parseMarkdownIncremental(markdown.slice(0, end), state);+ }+ expect(streamed).toEqual(full);+ });++ it('still drops blank lines before the first block', () => {+ expect(+ parseMarkdownIncremental('\n\n - a\n - b', createIncrementalState()),+ ).toEqual(parseMarkdown('\n\n - a\n - b'));+ });+});diff --git a/packages/core/src/Markdown/parser.ts b/packages/core/src/Markdown/parser.tsindex 8f197a9eb8..f80369edd7 100644--- a/packages/core/src/Markdown/parser.ts+++ b/packages/core/src/Markdown/parser.ts@@ -4763,8 +4763,13 @@ function parseMarkdownIncrementalAstBlocks(cache.settledEnd = nextSettledEnd;}- const trimmedUnsettledInput = unsettledInput.trim();- // String#trim removes the CR that belongs to the final content line of a+ // Blank lines before the tail and whitespace after it carry nothing, but the+ // first line's indentation does: it decides a list's indent and whether a+ // line is a heading, as in a full parse of the same text.+ const trimmedUnsettledInput = unsettledInput+ .replace(/^(?:[ \t]*\r?\n)+/, '')+ .trimEnd();+ // Trimming the end removes the CR that belongs to the final content line of a// CRLF snapshot along with trailing blank lines. Keep that one byte so// source ranges and delimiter content remain identical to a full parse.const unsettledRaw =