An open source design system that's fully customizable and agent ready
TypeScript
fix(Markdown, richtext): read a code fence's language after spaces, on both surfaces (#7072)
Cindy Zhang937c3894f80b
diff --git a/.changeset/markdown-fence-info-leading-space.md b/.changeset/markdown-fence-info-leading-space.mdnew file mode 100644index 0000000000..8c08935be2--- /dev/null+++ b/.changeset/markdown-fence-info-leading-space.md@@ -0,0 +1,8 @@+---+'@astryxdesign/core': patch+---++[fix] Markdown: read a code fence's language after spaces+@cixzhang++`Markdown` now reads a fenced code block's language as the first word of its info string after any spaces, as CommonMark specifies, so `~~~ js` and ` ``` js ` are JavaScript blocks rather than blocks with no language. Fences written without a space read as before.diff --git a/packages/core/src/Markdown/fenceInfo.test.tsx b/packages/core/src/Markdown/fenceInfo.test.tsxnew file mode 100644index 0000000000..d193ff9d7e--- /dev/null+++ b/packages/core/src/Markdown/fenceInfo.test.tsx@@ -0,0 +1,47 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {describe, expect, it} from 'vitest';+import {parseMarkdownAst} from './parser';++type Json = {type: string; lang?: string | null; meta?: string; value?: string};++/** The first block's language, meta, and code. */+function code(markdown: string): {+ lang: string | null;+ meta: string | null;+ value: string;+} {+ const [block] = parseMarkdownAst(markdown).children as unknown as Json[];+ return {+ lang: block.lang ?? null,+ meta: block.meta ?? null,+ value: block.value ?? '',+ };+}++describe("a fence's info string (CommonMark 0.31 §4.5)", () => {+ it.each([+ ['~~~ js\nx\n~~~\n', {lang: 'js', meta: null, value: 'x'}],+ ['``` js\nx\n```\n', {lang: 'js', meta: null, value: 'x'}],+ [+ '~~~ python title="a.py"\nx\n~~~\n',+ {lang: 'python', meta: 'title="a.py"', value: 'x'},+ ],+ [+ '``` {.haskell .numberLines}\nx\n```\n',+ {lang: '{.haskell', meta: '.numberLines}', value: 'x'},+ ],+ [+ '~~~python title="a.py"\nx\n~~~\n',+ {lang: 'python', meta: 'title="a.py"', value: 'x'},+ ],+ ['~~~c++ \nx\n~~~\n', {lang: 'c++', meta: null, value: 'x'}],+ ['~~~\nx\n~~~\n', {lang: null, meta: null, value: 'x'}],+ ['~~~ \nx\n~~~\n', {lang: null, meta: null, value: 'x'}],+ ])(+ 'reads the language of %j from its first word, after any spaces',+ (markdown, expected) => {+ expect(code(markdown)).toEqual(expected);+ },+ );+});diff --git a/packages/core/src/Markdown/parser.ts b/packages/core/src/Markdown/parser.tsindex 87eaeeeeb5..81e20d9fd8 100644--- a/packages/core/src/Markdown/parser.ts+++ b/packages/core/src/Markdown/parser.ts@@ -3134,7 +3134,9 @@ function parseMarkdownImpl(const fenceMatch = line.match(/^(`{3,}|~{3,})/);if (fenceMatch) {const fence = fenceMatch[1];- const info = line.slice(fence.length);+ // The rest of the line is the info string; its first word, after any+ // spaces, is the language (CommonMark 0.31 §4.5).+ const info = line.slice(fence.length).trim();const language = info.match(/^(\S+)/)?.[1] ?? null;const legacyLanguage = info.match(/^(\w*)/)?.[1] || null;const meta =diff --git a/packages/richtext/src/markdownCodeFence.test.ts b/packages/richtext/src/markdownCodeFence.test.tsindex 7bfa0692e0..e648ecc838 100644--- a/packages/richtext/src/markdownCodeFence.test.ts+++ b/packages/richtext/src/markdownCodeFence.test.ts@@ -154,6 +154,20 @@ describe.each(['~~~', '```'])('%s fenced code lines', fence => {});});+describe.each(['~~~', '```'])('%s fence info after a space', fence => {+ it('reads the language after the space, and keeps the info as written through an edit', () => {+ const markdown = `${fence} js\nx = 1\n${fence}\n`;+ expect(blocks(markdown)).toEqual([{language: 'js', code: 'x = 1'}]);+ expect(blocks(markdown)).toEqual(coreBlocks(markdown));+ expect(editAndExport(markdown, setCode('x = 2'))).toBe(+ `${fence} js\nx = 2\n${fence}\n`,+ );+ expect(editAndExport(markdown, code => code.setLanguage('ruby'))).toBe(+ `${fence}ruby\nx = 1\n${fence}\n`,+ );+ });+});+describe('tilde code fences', () => {it.each(['~~~~\n~~~\ninner\n~~~\n~~~~\n',diff --git a/packages/richtext/src/markdownCodeFence.ts b/packages/richtext/src/markdownCodeFence.tsindex 26fff84816..c9dd29fd9d 100644--- a/packages/richtext/src/markdownCodeFence.ts+++ b/packages/richtext/src/markdownCodeFence.ts@@ -9,9 +9,9 @@* stands in for Lexical's CODE, and TILDE_CODE reads the `~~~` fences CODE* does not (CommonMark 0.31 §4.5, spec:AST-061 FR5). Both read the opening* line as core Markdown does: the rest of the line after the fence is the- * info string, never code; the language is the text right after the fence- * up to the first space (so `c++` stays `c++`), and a block whose info- * string starts with a space has none, as core reads it. A block closes at+ * info string, never code; the language is its first word, after any+ * spaces (so `~~~ c++` reads `c++`), as core and CommonMark read it. A+ * block closes at* a fence of its own character at least as long, or at the end of the* document, and its code is exactly the lines between, indentation and* blank lines included, as core reads it. Each keeps its fence and info@@ -62,7 +62,7 @@ const fenceInfo = createState('astryxFenceInfo', {/** The language core Markdown reads from an info string, or null. */function languageOf(info: string): string | null {- return /^\S+/.exec(info)?.[0] ?? null;+ return /^\S+/.exec(info.trimStart())?.[0] ?? null;}/** An opening fence line: its fence and the info string after it. */@@ -139,10 +139,9 @@ const exportFencedCode: ElementTransformer['export'] = node => {let written = info ?? '';if ((languageOf(written) ?? '') !== language) {// The language changed: it leads, before the rest of the info string.+ const previous = languageOf(written);const rest =- languageOf(written) == null- ? ''- : written.slice(languageOf(written)?.length).trim();+ previous == null ? '' : written.trimStart().slice(previous.length).trim();written = rest === '' ? language : `${language} ${rest}`;}// A fence outlasts any run of its character in the code.