An open source design system that's fully customizable and agent ready
TypeScript
fix(Markdown, richtext): read an indented table with its own columns (#7120)
Cindy Zhang51bf85cd6c8b
diff --git a/.changeset/markdown-indented-table-columns.md b/.changeset/markdown-indented-table-columns.mdnew file mode 100644index 0000000000..87ede89234--- /dev/null+++ b/.changeset/markdown-indented-table-columns.md@@ -0,0 +1,8 @@+---+'@astryxdesign/core': patch+---++[fix] Markdown: read an indented table with its own columns+@cixzhang++`Markdown` no longer adds an empty first column to a table whose rows are indented, such as ` | a | b |`. The indentation before a row's first pipe used to read as a cell of its own.diff --git a/packages/core/src/Markdown/indentedTables.test.tsx b/packages/core/src/Markdown/indentedTables.test.tsxnew file mode 100644index 0000000000..a174ff1ec2--- /dev/null+++ b/packages/core/src/Markdown/indentedTables.test.tsx@@ -0,0 +1,53 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {describe, expect, it} from 'vitest';+import {+ createIncrementalState,+ parseMarkdown,+ parseMarkdownAst,+ parseMarkdownIncremental,+} from './parser';++type Json = {type: string; value?: string; children?: Json[]};++/** Each table row's cell texts. */+function tableRows(markdown: string): string[][] {+ const text = (node: Json): string =>+ node.value ?? (node.children ?? []).map(text).join('');+ const table = (parseMarkdownAst(markdown).children as unknown as Json[]).find(+ node => node.type === 'table',+ );+ return (table?.children ?? []).map(row => (row.children ?? []).map(text));+}++describe("a table row's indentation opens no cell", () => {+ it.each([+ ['indented two spaces', ' | a | b |\n | - | - |\n | 1 | 2 |'],+ ['indented three spaces', ' | a | b |\n | - | - |\n | 1 | 2 |'],+ ['indented with a tab', '\t| a | b |\n\t| - | - |\n\t| 1 | 2 |'],+ ['indented without outer pipes', ' a | b\n - | -\n 1 | 2'],+ ['at the margin, unchanged', '| a | b |\n| - | - |\n| 1 | 2 |'],+ ])('reads a table %s as two columns', (_, markdown) => {+ expect(tableRows(markdown)).toEqual([+ ['a', 'b'],+ ['1', '2'],+ ]);+ });++ it('keeps an empty first cell written inside the pipes', () => {+ expect(tableRows(' | | b |\n | - | - |\n | | 2 |')).toEqual([+ ['', 'b'],+ ['', '2'],+ ]);+ });++ it('streams an indented table to the full parse at every character', () => {+ const markdown = 'Intro\n\n | a | b |\n | - | - |\n | 1 | 2 |\n\nAfter';+ 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(parseMarkdown(markdown));+ });+});diff --git a/packages/core/src/Markdown/parser.ts b/packages/core/src/Markdown/parser.tsindex 996150cc6e..57149010d0 100644--- a/packages/core/src/Markdown/parser.ts+++ b/packages/core/src/Markdown/parser.ts@@ -3197,8 +3197,12 @@ function sourceEndsInParagraph(source: string, opts: ResolvedOptions): boolean {function splitTableRow(line: string): string[] {let start = 0;let end = line.length;- if (line.startsWith('|')) {- start = 1;+ // A row's indentation, and its leading pipe, open no cell.+ while (start < end && (line[start] === ' ' || line[start] === '\t')) {+ start++;+ }+ if (line[start] === '|') {+ start++;while (start < end && line[start] === ' ') {start++;}diff --git a/packages/richtext/src/markdownIndentedTables.test.ts b/packages/richtext/src/markdownIndentedTables.test.tsnew file mode 100644index 0000000000..372485a5dc--- /dev/null+++ b/packages/richtext/src/markdownIndentedTables.test.ts@@ -0,0 +1,43 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {describe, expect, it} from 'vitest';+import {parseMarkdownAst} from '@astryxdesign/core/Markdown/parser';+import {markdownToEditorStateJSON} from './markdownSerializers';++type Json = {type: string; text?: string; value?: string; children?: Json[]};++const textOf = (node: Json): string =>+ node.text ?? node.value ?? (node.children ?? []).map(textOf).join('');++/** Each row's cell texts in the first table found. */+function rowsOf(nodes: ReadonlyArray<Json>, rowType: string): string[][] {+ const table = nodes.find(node => node.type === 'table');+ return (table?.children ?? [])+ .filter(row => row.type === rowType)+ .map(row => (row.children ?? []).map(textOf));+}++const richRows = (markdown: string) =>+ rowsOf(+ (JSON.parse(markdownToEditorStateJSON(markdown)) as {root: Json}).root+ .children ?? [],+ 'tablerow',+ );++const coreRows = (markdown: string) =>+ rowsOf(parseMarkdownAst(markdown).children as unknown as Json[], 'tableRow');++describe('an indented table imports with its own columns, as core reads it', () => {+ it.each([+ ' | a | b |\n | - | - |\n | 1 | 2 |',+ '\t| a | b |\n\t| - | - |\n\t| 1 | 2 |',+ '| a | b |\n| - | - |\n| 1 | 2 |',+ ])('imports %j as two columns', markdown => {+ const expected = [+ ['a', 'b'],+ ['1', '2'],+ ];+ expect(richRows(markdown)).toEqual(expected);+ expect(coreRows(markdown)).toEqual(expected);+ });+});diff --git a/packages/richtext/src/markdownTable.ts b/packages/richtext/src/markdownTable.tsindex c0be205561..339cfdbcfc 100644--- a/packages/richtext/src/markdownTable.ts+++ b/packages/richtext/src/markdownTable.ts@@ -66,15 +66,19 @@ const CELL_TRANSFORMERS: Array<Transformer> = [type ColumnAlignment = 'left' | 'center' | 'right' | null;/**- * Splits a row into trimmed cell sources: one leading pipe, trailing spaces,- * and one trailing pipe are dropped, and the rest splits on unescaped pipes.- * An escaped pipe stays escaped for the cell's inline import.+ * Splits a row into trimmed cell sources: the row's indentation, one leading+ * pipe, trailing spaces, and one trailing pipe are dropped, and the rest+ * splits on unescaped pipes. An escaped pipe stays escaped for the cell's+ * inline import.*/function splitTableRow(line: string): Array<string> {let start = 0;let end = line.length;- if (line.startsWith('|')) {- start = 1;+ while (start < end && (line[start] === ' ' || line[start] === '\t')) {+ start++;+ }+ if (line[start] === '|') {+ start++;while (start < end && line[start] === ' ') {start++;}