Skip to content

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

TypeScript

fix(Markdown): escape only ASCII punctuation with a backslash (#6996)

Cindy Zhangbfb918a977e0
diff --git a/.changeset/markdown-backslash-escapes.md b/.changeset/markdown-backslash-escapes.md
new file mode 100644
index 0000000000..05e0293dd8
--- /dev/null
+++ b/.changeset/markdown-backslash-escapes.md
@@ -0,0 +1,9 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Markdown keeps a backslash unless it escapes punctuation, and a backslash at the end of a line is a line break
+
+`C:\Users\Ada` rendered as `C:UsersAda`: any character after a backslash swallowed it. As CommonMark specifies, a backslash now escapes only ASCII punctuation (`\*`, `\#`, `\\`, …); before a letter, digit, space, or other character it stays as written. A backslash at the end of a line is a hard line break. Image alt text follows the same rule.
+
+@cixzhang
diff --git a/packages/core/src/Markdown/characterReferences.ts b/packages/core/src/Markdown/characterReferences.ts
index 030e27659b..e1a6edf5e6 100644
--- a/packages/core/src/Markdown/characterReferences.ts
+++ b/packages/core/src/Markdown/characterReferences.ts
@@ -76,6 +76,11 @@ export function matchCharacterReference(
return {value: isValid ? String.fromCodePoint(codePoint) : '\uFFFD', end};
}
+/** The characters a backslash escapes: ASCII punctuation (CommonMark 0.31). */
+export function isAsciiPunctuation(character: string | undefined): boolean {
+ return character != null && /^[!-/:-@[-`{-~]$/.test(character);
+}
+
/**
* Resolves backslash escapes and character references the way inline parsing
* does, for text that is shown as written rather than parsed as Markdown, such
@@ -89,7 +94,11 @@ export function decodeLiteralText(text: string): string {
let index = 0;
while (index < text.length) {
const character = text[index];
- if (character === '\\' && index + 1 < text.length) {
+ if (
+ character === '\\' &&
+ index + 1 < text.length &&
+ isAsciiPunctuation(text[index + 1])
+ ) {
output += text[index + 1];
index += 2;
continue;
diff --git a/packages/core/src/Markdown/parser.test.ts b/packages/core/src/Markdown/parser.test.ts
index 74caf33297..563994cafa 100644
--- a/packages/core/src/Markdown/parser.test.ts
+++ b/packages/core/src/Markdown/parser.test.ts
@@ -380,6 +380,32 @@ describe('parseInline', () => {
]);
});
+ it('escapes only ASCII punctuation and keeps other backslashes literal', () => {
+ const textOf = (source: string) =>
+ parseInline(source)
+ .map(node => (node.type === 'text' ? node.content : `<${node.type}>`))
+ .join('');
+ expect(textOf('C:\\Users\\Ada')).toBe('C:\\Users\\Ada');
+ expect(textOf('\\*not emphasis\\* and \\\\ one backslash')).toBe(
+ '*not emphasis* and \\ one backslash',
+ );
+ expect(textOf('caf\\\u00e9 \\a \\1')).toBe('caf\\\u00e9 \\a \\1');
+ });
+
+ it('makes a backslash before a line break a hard break', () => {
+ expect(parseInline('line\\\nnext')).toEqual([
+ {type: 'text', content: 'line'},
+ {type: 'break'},
+ {type: 'text', content: 'next'},
+ ]);
+ });
+
+ it('keeps non-punctuation backslashes in image alt text', () => {
+ expect(parseInline('![C:\\Users \\& more](x.png)')).toEqual([
+ {type: 'image', src: 'x.png', alt: 'C:\\Users & more'},
+ ]);
+ });
+
it('handles parentheses in image URLs', () => {
const result = parseInline('![alt](https://example.com/img_(1).png)');
expect(result[0].type).toBe('image');
diff --git a/packages/core/src/Markdown/parser.ts b/packages/core/src/Markdown/parser.ts
index dca8858653..ef2815bb18 100644
--- a/packages/core/src/Markdown/parser.ts
+++ b/packages/core/src/Markdown/parser.ts
@@ -40,6 +40,7 @@ import type {
} from './plugins/protocol';
import {
decodeLiteralText,
+ isAsciiPunctuation,
matchCharacterReference,
} from './characterReferences';
import {isSafeMarkdownParserUrl} from './url';
@@ -1482,11 +1483,20 @@ function parseInlineImpl(
let i = 0;
while (i < text.length) {
- // --- Escape ---
+ // --- Escape: a backslash escapes ASCII punctuation, and before a line
+ // break it is a hard break; before anything else it is a literal
+ // backslash (CommonMark 0.31, backslash escapes and hard line breaks).
if (text[i] === '\\' && i + 1 < text.length) {
- nodes.push({type: 'text', value: text[i + 1]});
- i += 2;
- continue;
+ if (text[i + 1] === '\n') {
+ nodes.push({type: 'break'});
+ i += 2;
+ continue;
+ }
+ if (isAsciiPunctuation(text[i + 1])) {
+ nodes.push({type: 'text', value: text[i + 1]});
+ i += 2;
+ continue;
+ }
}
// --- Inline code ---