Skip to content

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

TypeScript

fix(Markdown): keep a quoted blank line at the end of a code block left open (#7129)

Cindy Zhang8a5b3fb89151
diff --git a/.changeset/markdown-quoted-open-fence-blank-line.md b/.changeset/markdown-quoted-open-fence-blank-line.md
new file mode 100644
index 0000000000..f98164c0ec
--- /dev/null
+++ b/.changeset/markdown-quoted-open-fence-blank-line.md
@@ -0,0 +1,8 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Markdown: keep a quoted blank line at the end of a code block left open
+@cixzhang
+
+A code block left open inside a block quote keeps a blank quoted line at its end as code, as CommonMark reads it. Only the document's own final line ending is left out of an open code block.
diff --git a/packages/core/src/Markdown/openFenceFinalNewline.test.tsx b/packages/core/src/Markdown/openFenceFinalNewline.test.tsx
index 644caf1c75..887a97d4e1 100644
--- a/packages/core/src/Markdown/openFenceFinalNewline.test.tsx
+++ b/packages/core/src/Markdown/openFenceFinalNewline.test.tsx
@@ -61,3 +61,25 @@ describe('a fence left open at the end keeps its code, not the final line ending
},
);
});
+
+describe('a fence left open inside a quote keeps its quoted blank lines', () => {
+ const quoted = (markdown: string) =>
+ (parseMarkdownAst(markdown).children as unknown as Json[])
+ .filter(node => node.type === 'blockquote')
+ .flatMap(node => node.children ?? [])
+ .filter(node => node.type === 'code')
+ .map(node => node.value);
+
+ it.each([
+ ['a quoted blank line', '> ```\n> code\n>\n', 'code\n'],
+ ['the document’s final newline only', '> ```\n> code\n', 'code'],
+ ])('reads %s as CommonMark does', (_, markdown, value) => {
+ expect(quoted(markdown)).toEqual([value]);
+ 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.ts
index a326d916fd..dc3cefc218 100644
--- a/packages/core/src/Markdown/parser.ts
+++ b/packages/core/src/Markdown/parser.ts
@@ -3696,10 +3696,17 @@ function parseMarkdownImpl(
index++;
}
const closed = index < lines.length;
- // A fence left open runs to the end of the input, whose final line
+ // A fence left open in the document runs to its end, whose final line
// ending starts no line of code (CommonMark 0.31 §4.5) — as a closed
- // fence's code ends before its closing line.
- if (!closed && codeLines.length > 0 && lines[lines.length - 1] === '') {
+ // fence's code ends before its closing line. A container's content is
+ // rebuilt from its lines, so its last line is a real one: a quoted
+ // blank line stays code.
+ if (
+ opts.blockDepth === 0 &&
+ !closed &&
+ codeLines.length > 0 &&
+ lines[lines.length - 1] === ''
+ ) {
codeLines.pop();
}
index++; // skip closing fence