Skip to content

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

TypeScript

fix(richtext): write a link address's backslashes escaped, so they read back (#7098)

Cindy Zhang84fdbd16fe3b
diff --git a/packages/richtext/src/markdownAngleDestinations.test.ts b/packages/richtext/src/markdownAngleDestinations.test.ts
index 4bf30bfa6d..ce314cc939 100644
--- a/packages/richtext/src/markdownAngleDestinations.test.ts
+++ b/packages/richtext/src/markdownAngleDestinations.test.ts
@@ -263,15 +263,28 @@ describe('angle-bracket destinations holding parentheses and backslashes', () =>
expect(richLinks(exportLinkTo(url))).toEqual([{url, title: null}]);
});
+ it.each([
+ 'https://e.com/a\\*b',
+ 'https://e.com/a\\',
+ 'https://e.com/a\\(b',
+ 'https://e.com/a\\b',
+ 'https://e.com/a\\\\b',
+ ])(
+ 'writes a link made in the editor to %j, backslashes and all, so it reads back',
+ url => {
+ expect(richLinks(exportLinkTo(url))).toEqual([{url, title: null}]);
+ },
+ );
+
it('reads back every link made in the editor across 4,000 addresses', () => {
let state = 4000;
const random = () => {
state = (state * 1103515245 + 12345) % 2147483648;
return state / 2147483648;
};
- // Backslashes outside angle brackets and backticks are left out: they
- // are an older, separate gap in how addresses are written.
- const alphabet = [...'ab/. ()<>"\'#:_*[]&;'];
+ // Backticks are left out: inside an angle-bracket destination they can
+ // still pair as code, a separate gap.
+ const alphabet = [...'ab/. ()<>"\'#:_*[]&;\\'];
const lost: string[] = [];
for (let round = 0; round < 4000; round++) {
let tail = '';
diff --git a/packages/richtext/src/markdownLink.ts b/packages/richtext/src/markdownLink.ts
index 19bf2f51b6..0520aeef5d 100644
--- a/packages/richtext/src/markdownLink.ts
+++ b/packages/richtext/src/markdownLink.ts
@@ -43,7 +43,10 @@ export function markdownDestination(url: string): string {
// (CommonMark §6.3).
return `<${url.replace(/[\\<>]/g, '\\$&')}>`;
}
- return isBalanced(url) ? url : url.replace(/[()]/g, '\\$&');
+ // A backslash before punctuation would read back as an escape, so every
+ // backslash is written escaped; so is every parenthesis that does not
+ // balance.
+ return url.replace(isBalanced(url) ? /\\/g : /[\\()]/g, '\\$&');
}
/**