Skip to content

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

TypeScript

fix(Markdown): start a new list when the bullet changes (#7089)

Cindy Zhang31dca2537499
diff --git a/.changeset/markdown-bullet-change-new-list.md b/.changeset/markdown-bullet-change-new-list.md
new file mode 100644
index 0000000000..56cda367d9
--- /dev/null
+++ b/.changeset/markdown-bullet-change-new-list.md
@@ -0,0 +1,8 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Markdown: start a new list when the bullet changes
+@cixzhang
+
+`Markdown` now starts a new list when a bullet list's marker changes — `- a` then `* b` are two lists — as CommonMark specifies, and as ordered lists already did when their delimiter changes.
diff --git a/packages/core/src/Markdown/listBullets.test.tsx b/packages/core/src/Markdown/listBullets.test.tsx
new file mode 100644
index 0000000000..ec393ce1b6
--- /dev/null
+++ b/packages/core/src/Markdown/listBullets.test.tsx
@@ -0,0 +1,55 @@
+// 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(' | ');
+}
+
+const item = (text: string) => `listItem[paragraph["${text}"]]`;
+
+describe('a list keeps the bullet it starts with (CommonMark 0.31 §5.3)', () => {
+ it.each([
+ ['`-` then `*`', '- a\n* b', [`list[${item('a')}]`, `list[${item('b')}]`]],
+ [
+ '`-`, `+`, then `-`',
+ '- a\n+ b\n- c',
+ [`list[${item('a')}]`, `list[${item('b')}]`, `list[${item('c')}]`],
+ ],
+ [
+ '`-` then `*` after a blank line',
+ '- a\n\n* b',
+ [`list[${item('a')}]`, `list[${item('b')}]`],
+ ],
+ ])('another bullet starts a new list: %s', (_, markdown, expected) => {
+ expect(blocks(markdown)).toBe(expected.join(' | '));
+ });
+
+ it.each([
+ ['the same bullet', '* a\n* b', `list[${item('a')}, ${item('b')}]`],
+ [
+ 'a nested list with another bullet',
+ '- a\n * b',
+ `list[listItem[paragraph["a"], list[${item('b')}]]]`,
+ ],
+ [
+ 'ordered items with one delimiter',
+ '1. a\n2. b',
+ `list[${item('a')}, ${item('b')}]`,
+ ],
+ ])('keeps one list for %s', (_, markdown, expected) => {
+ expect(blocks(markdown)).toBe(expected);
+ });
+});
diff --git a/packages/core/src/Markdown/parser.ts b/packages/core/src/Markdown/parser.ts
index 3335a16272..0fc135c9d7 100644
--- a/packages/core/src/Markdown/parser.ts
+++ b/packages/core/src/Markdown/parser.ts
@@ -3089,9 +3089,12 @@ function parseList(
: null;
const delim = orderedStart ? orderedStart[2] : '.';
const escDelim = `\\${delim}`;
+ // Bullet lists likewise keep the bullet they start with — `-`, `*`, or
+ // `+` — and another bullet starts a new list (CommonMark 0.31 §5.3).
+ const bullet = ordered ? null : lines[startIndex].trimStart()[0];
const itemPattern = ordered
? new RegExp(`^ {${baseIndent}}\\d+${escDelim} `)
- : new RegExp(`^ {${baseIndent}}[-*+] `);
+ : new RegExp(`^ {${baseIndent}}\\${bullet} `);
const start = orderedStart ? parseInt(orderedStart[1], 10) : undefined;