An open source design system that's fully customizable and agent ready
TypeScript
fix(Markdown): end a list at a thematic break (#7087)
Cindy Zhang34d32e6528b5
diff --git a/.changeset/markdown-thematic-break-ends-list.md b/.changeset/markdown-thematic-break-ends-list.mdnew file mode 100644index 0000000000..f18801f33e--- /dev/null+++ b/.changeset/markdown-thematic-break-ends-list.md@@ -0,0 +1,8 @@+---+'@astryxdesign/core': patch+---++[fix] Markdown: end a list at a thematic break+@cixzhang++`Markdown` now reads a line such as `* * *` or `- - -` after a list item as a thematic break that ends the list, as CommonMark specifies, rather than as another item holding a nested list.diff --git a/packages/core/src/Markdown/listThematicBreak.test.tsx b/packages/core/src/Markdown/listThematicBreak.test.tsxnew file mode 100644index 0000000000..0c9f5bdee8--- /dev/null+++ b/packages/core/src/Markdown/listThematicBreak.test.tsx@@ -0,0 +1,49 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {describe, expect, it} from 'vitest';+import {parseMarkdownAst} from './parser';++type Json = {type: string; value?: string; children?: Json[]};++/** The document's blocks as compact markup. */+function blocks(markdown: string): string {+ const show = (node: Json): string =>+ node.type === 'text'+ ? JSON.stringify(node.value)+ : node.children == null+ ? node.type+ : `${node.type}[${node.children.map(show).join(', ')}]`;+ return (parseMarkdownAst(markdown).children as unknown as Json[])+ .map(show)+ .join(' | ');+}++describe('a thematic break after a list item (CommonMark 0.31 §4.1)', () => {+ it.each([+ ['`* * *` after a `-` item', '- item\n* * *'],+ ['`- - -` after a `-` item', '- item\n- - -'],+ ['`* * *` after a `*` item', '* item\n* * *'],+ ['a break after a blank line', '- item\n\n* * *'],+ ['a break after an ordered item', '1. item\n* * *'],+ ])('ends the list: %s', (_, markdown) => {+ expect(blocks(markdown)).toMatch(+ /^list\[listItem\[paragraph\["item"\]\]\] \| thematicBreak$/,+ );+ });++ it.each([+ [+ 'a break indented into the item',+ '- a\n * * *\n b',+ 'list[listItem[paragraph["a"], thematicBreak, paragraph["b"]]]',+ ],+ ['an item holding a break', '- * * *', 'list[listItem[thematicBreak]]'],+ [+ 'two items',+ '- a\n- b',+ 'list[listItem[paragraph["a"]], listItem[paragraph["b"]]]',+ ],+ ])('leaves %s as it was', (_, markdown, expected) => {+ expect(blocks(markdown)).toBe(expected);+ });+});diff --git a/packages/core/src/Markdown/parser.ts b/packages/core/src/Markdown/parser.tsindex bc5cf3f9e1..3335a16272 100644--- a/packages/core/src/Markdown/parser.ts+++ b/packages/core/src/Markdown/parser.ts@@ -3097,7 +3097,11 @@ function parseList(let loose = false;let index = startIndex;- while (index < lines.length && itemPattern.test(lines[index])) {+ // A line that is a thematic break is one, not an item, even where an item+ // could start (CommonMark 0.31 §4.1): `* * *` after `- item` ends the list.+ const startsItem = (line: string) =>+ itemPattern.test(line) && !isHorizontalRule(line);+ while (index < lines.length && startsItem(lines[index])) {const content = ordered? lines[index].replace(new RegExp(`^ *\\d+${escDelim} `), ''): lines[index].replace(/^ *[-*+] /, '');@@ -3213,7 +3217,7 @@ function parseList(if (lookahead > index &&lookahead < lines.length &&- itemPattern.test(lines[lookahead])+ startsItem(lines[lookahead])) {loose = true;index = lookahead;diff --git a/packages/richtext/src/markdownListThematicBreak.test.ts b/packages/richtext/src/markdownListThematicBreak.test.tsnew file mode 100644index 0000000000..5b2919488a--- /dev/null+++ b/packages/richtext/src/markdownListThematicBreak.test.ts@@ -0,0 +1,31 @@+// 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; children?: Json[]};++const CORE_TO_RICH: Record<string, string> = {+ list: 'list',+ thematicBreak: 'horizontalrule',+ paragraph: 'paragraph',+};++describe('a thematic break after a list item, on both surfaces', () => {+ it.each([+ '- item\n* * *',+ '- item\n- - -',+ '* item\n* * *',+ '- item\n\n* * *',+ ])('reads %j as a list and then a break, as core does', markdown => {+ const rich = (+ JSON.parse(markdownToEditorStateJSON(markdown)) as {root: Json}+ ).root.children?.map(node => node.type);+ const core = (parseMarkdownAst(markdown).children as unknown as Json[]).map(+ node => CORE_TO_RICH[node.type] ?? node.type,+ );+ expect(rich).toEqual(['list', 'horizontalrule']);+ expect(core).toEqual(rich);+ });+});