An open source design system that's fully customizable and agent ready
TypeScript
fix(Markdown, richtext): a destination opening with < must be a whole angle-bracket destination (#7086)
Cindy Zhang5e67ff8bd792
diff --git a/.changeset/markdown-invalid-angle-destinations.md b/.changeset/markdown-invalid-angle-destinations.mdnew file mode 100644index 0000000000..fb2ed621a8--- /dev/null+++ b/.changeset/markdown-invalid-angle-destinations.md@@ -0,0 +1,8 @@+---+'@astryxdesign/core': patch+---++[fix] Markdown: show a link whose destination opens with `<` but is no angle-bracket destination as text+@cixzhang++`Markdown` now shows `[a](<b>c>)`, `[a](<b)`, and `[link](<foo\>)` as text, as CommonMark specifies: a destination that opens with `<` must be one whole angle-bracket destination, with no line ending inside, even after a backslash. Such links used to fall back to a link to the whole text between the parentheses.diff --git a/packages/core/src/Markdown/angleDestinations.test.tsx b/packages/core/src/Markdown/angleDestinations.test.tsxindex 175e615678..0bb0af4fca 100644--- a/packages/core/src/Markdown/angleDestinations.test.tsx+++ b/packages/core/src/Markdown/angleDestinations.test.tsx@@ -29,7 +29,13 @@ describe('angle-bracket destinations (CommonMark 0.31 §6.3)', () => {['[a](<b\\<c>)', ['link(b<c)']],['[a](<>)', ['link()']],['[a](<b\nc>)', []],- ['[a](<b>c>)', ['link(<b>c>)']],+ ['[a](<b>c>)', []],+ ['', []],+ ['[a](<b)', []],+ ['[a](<b c)', []],+ ['[link](<foo\\>)', []],+ ['[a](<b<c>)', []],+ ['[a](<b\\\nc>)', []],['[a](b c)', ['link(b c)']],['[a](<b(c>) and [d](e)', ['link(b(c)', 'link(e)']],])('reads %j', (markdown, expected) => {@@ -56,7 +62,19 @@ describe('angle-bracket destinations (CommonMark 0.31 §6.3)', () => {'data: text/html,',];for (let round = 0; round < 500; round++) {- const body = pick(['x', 'a(b)', 'a(b', 'a\\>b', 'a\\<b', ' x ', '']);+ const body = pick([+ 'x',+ 'a(b)',+ 'a(b',+ 'a\\>b',+ 'a\\<b',+ ' x ',+ '',+ 'a>b',+ 'a<b',+ 'a\\',+ 'a\\\nb',+ ]);const title = pick(['', ' "t"', " 't'", ' (t)']);const markdown = `${pick(['[a]', '![a]'])}(${pick(['', ' '])}<${pick(schemes)}${body}>${title})`;for (const target of targets(markdown)) {diff --git a/packages/core/src/Markdown/parser.ts b/packages/core/src/Markdown/parser.tsindex fe94c9f78c..bc5cf3f9e1 100644--- a/packages/core/src/Markdown/parser.ts+++ b/packages/core/src/Markdown/parser.ts@@ -1109,12 +1109,15 @@ const INLINE_DESTINATION_WITH_TITLE =/^\s*(?:<((?:[^<>\n\\]|\\.)*)>|([^\s<]\S*?))(?:\s+(?:"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|\((?:[^()\\]|\\.)*\)))?\s*$/s;/**- * Where an inline link's or image's destination ends when it is written in- * angle brackets — the index of the `)` that closes the link — or null when- * the content after `open` (the `(`) is not an angle-bracket destination.- * Inside the brackets parentheses are plain characters, so `<b(c>` is the- * destination `b(c`; a line ending or unescaped `<` there makes it no- * destination at all, so the link is text (CommonMark 0.31 §6.3).+ * Where an inline link's or image's destination ends when it opens with `<`+ * — the index of the `)` that closes the link — `'refused'` when it opens+ * with `<` but is no angle-bracket destination, or null when the content+ * after `open` (the `(`) does not open with `<`. Inside the brackets+ * parentheses are plain characters, so `<b(c>` is the destination `b(c`. A+ * destination that opens with `<` must be one whole angle-bracket+ * destination — no line ending, even escaped, and no unescaped `<` inside;+ * then only spaces, an optional title, and the `)` — or the link is text:+ * `[a](<b>c>)` and `[a](<b)` are no links (CommonMark 0.31 §6.3).*/function angleDestinationClose(text: string,@@ -1129,18 +1132,20 @@ function angleDestinationClose(}for (index++; index < text.length; index++) {const character = text[index];- if (character === '\\') {+ if (+ character === '\\' &&+ text[index + 1] !== '\n' &&+ text[index + 1] !== '\r'+ ) {index++;- } else if (character === '\n' || character === '\r') {+ } else if (character === '\n' || character === '\r' || character === '<') {return 'refused';- } else if (character === '<') {- return null;} else if (character === '>') {break;}}if (index >= text.length) {- return null;+ return 'refused';}// After the brackets: spaces, an optional title, spaces, and the `)`.index++;@@ -1156,7 +1161,7 @@ function angleDestinationClose(} else if (quote === '(' && text[index] === '(') {// A title in parentheses holds no unescaped `(`; stopping here// also keeps every search short.- return null;+ return 'refused';}}index++;@@ -1164,7 +1169,7 @@ function angleDestinationClose(index++;}}- return text[index] === ')' ? index : null;+ return text[index] === ')' ? index : 'refused';}/**diff --git a/packages/richtext/src/markdownAngleDestinations.test.ts b/packages/richtext/src/markdownAngleDestinations.test.tsindex 3a7db24f5c..4bf30bfa6d 100644--- a/packages/richtext/src/markdownAngleDestinations.test.ts+++ b/packages/richtext/src/markdownAngleDestinations.test.ts@@ -11,6 +11,7 @@ import {} from 'lexical';import {parseInlineAst} from '@astryxdesign/core/Markdown/parser';import {DEFAULT_NODES} from './editorNodes';+import {coreRefusesUrl} from './markdownCharacterReferences';import {DEFAULT_TRANSFORMERS} from './markdownTable';import {$exportMarkdownKeepingSource,@@ -134,9 +135,61 @@ describe('angle-bracket link destinations (CommonMark 0.31 §6.3)', () => {});describe('angle-bracket destination edges', () => {- it('reads `<b>c>` as a destination as it stands, as core does', () => {- const markdown = '[a](<b>c>)';- expect(richLinks(markdown).map(({url}) => url)).toEqual(coreUrls(markdown));+ it.each(['[a](<b>c>)', '[link](<foo\\>)', '[a](<b)', '[a](<b<c>)'])(+ 'does not link %j, which is no angle-bracket destination, as core does',+ markdown => {+ expect(richLinks(markdown)).toEqual([]);+ expect(coreUrls(markdown)).toEqual([]);+ },+ );++ it('writes a link made in the editor whose address opens with `<` so it reads back', () => {+ const editor = newEditor();+ editor.update(+ () => {+ $getRoot()+ .clear()+ .append(+ $createParagraphNode().append(+ $createLinkNode('<b').append($createTextNode('x')),+ ),+ );+ },+ {discrete: true},+ );+ const exported = editor+ .getEditorState()+ .read(() => $exportMarkdownKeepingSource([...DEFAULT_TRANSFORMERS]));+ expect(exported).toBe('[x](<\\<b>)');+ expect(richLinks(exported)).toEqual([{url: '<b', title: null}]);+ });++ it('stores no unsafe address, however an angle destination is written', () => {+ let state = 40;+ const random = () => {+ state = (state * 1103515245 + 12345) % 2147483648;+ return state / 2147483648;+ };+ const pick = <T>(items: ReadonlyArray<T>): T =>+ items[Math.floor(random() * items.length)];+ const schemes = [+ 'javascript:',+ 'JaVaScRiPt:',+ 'javascript:',+ 'javascript:',+ 'java\\script:',+ 'vbscript:',+ 'data:text/html,',+ 'data: text/html,',+ ];+ for (let round = 0; round < 300; round++) {+ const body = pick(['x', 'a(b)', 'a\\>b', 'a>b', 'a<b', ' x ', 'a\\', '']);+ const title = pick(['', ' "t"']);+ const markdown = `[a](<${pick(schemes)}${body}>${title})`;+ for (const {url} of richLinks(markdown)) {+ expect(coreRefusesUrl(url), markdown).toBe(false);+ }+ }});it('reads an escaped bracket inside the angle brackets as part of the address', () => {@@ -172,3 +225,66 @@ describe('angle-bracket destination edges', () => {expect(richLinks(markdown)).toEqual([]);});});++/** A link made in the editor to `url`, exported. */+function exportLinkTo(url: string): string {+ const editor = newEditor();+ editor.update(+ () => {+ $getRoot()+ .clear()+ .append(+ $createParagraphNode().append(+ $createLinkNode(url).append($createTextNode('x')),+ ),+ );+ },+ {discrete: true},+ );+ return editor+ .getEditorState()+ .read(() => $exportMarkdownKeepingSource([...DEFAULT_TRANSFORMERS]));+}++describe('angle-bracket destinations holding parentheses and backslashes', () => {+ it('links `[a](<b)c>)` to `b)c`, as core does', () => {+ const markdown = '[a](<b)c>)';+ expect(richLinks(markdown)).toEqual([{url: 'b)c', title: null}]);+ expect(coreUrls(markdown)).toEqual(['b)c']);+ });++ it.each([+ 'https://example.com/a b)',+ 'https://example.com/a (b',+ 'https://e.com/a\\b c',+ 'https://e.com/a\\> b',+ 'https://e.com/a b\\',+ ])('writes a link made in the editor to %j 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/. ()<>"\'#:_*[]&;'];+ const lost: string[] = [];+ for (let round = 0; round < 4000; round++) {+ let tail = '';+ const length = 1 + Math.floor(random() * 8);+ for (let index = 0; index < length; index++) {+ tail += alphabet[Math.floor(random() * alphabet.length)];+ }+ const url = `https://e.com/${tail}`;+ const read = richLinks(exportLinkTo(url));+ if (read.length !== 1 || read[0].url !== url) {+ lost.push(url);+ }+ }+ expect(lost).toEqual([]);+ });+});diff --git a/packages/richtext/src/markdownCharacterReferences.ts b/packages/richtext/src/markdownCharacterReferences.tsindex 062631619c..32c75e3548 100644--- a/packages/richtext/src/markdownCharacterReferences.ts+++ b/packages/richtext/src/markdownCharacterReferences.ts@@ -322,6 +322,65 @@ function pairedDelimiters(return pair;}+/**+ * Where a destination that opens with `<` ends — the index of the `)` that+ * closes the link — read as core Markdown reads it (CommonMark 0.31 §6.3):+ * `'refused'` when it opens with `<` but is no angle-bracket destination,+ * and null when the content after `open` (the `(`) does not open with `<`.+ * Inside the brackets parentheses are plain characters; a line ending, even+ * escaped, or an unescaped `<` makes it no destination; after the `>` come+ * only spaces, an optional title, and the `)`.+ */+function angleDestinationEnd(+ text: string,+ open: number,+): number | 'refused' | null {+ let index = open + 1;+ while (text[index] === ' ' || text[index] === '\t') {+ index++;+ }+ if (text[index] !== '<') {+ return null;+ }+ for (index++; index < text.length; index++) {+ const character = text[index];+ if (+ character === '\\' &&+ text[index + 1] !== '\n' &&+ text[index + 1] !== '\r'+ ) {+ index++;+ } else if (character === '\n' || character === '\r' || character === '<') {+ return 'refused';+ } else if (character === '>') {+ break;+ }+ }+ if (index >= text.length) {+ return 'refused';+ }+ index++;+ while (/\s/.test(text[index] ?? '')) {+ index++;+ }+ const quote = text[index];+ if (quote === '"' || quote === "'" || quote === '(') {+ const closer = quote === '(' ? ')' : quote;+ for (index++; index < text.length && text[index] !== closer; index++) {+ if (text[index] === '\\') {+ index++;+ } else if (quote === '(' && text[index] === '(') {+ return 'refused';+ }+ }+ index++;+ while (/\s/.test(text[index] ?? '')) {+ index++;+ }+ }+ return text[index] === ')' ? index : 'refused';+}+/*** Whether core Markdown refuses a link to `destination`, as written in the* source: its own parser decides, so both surfaces refuse the same@@ -380,7 +439,11 @@ export function protectRefusedLinks(markdown: string): ProtectedMarkdown {index = markdown.indexOf('](', index + 1)) {const open = pair[index];- const close = pair[index + 1];+ // A destination in angle brackets ends where core ends it — parentheses+ // inside the brackets are plain characters — so `[a](<b)c>)` is probed+ // whole, not as `<b`.+ const angleClose = angleDestinationEnd(markdown, index + 1);+ const close = typeof angleClose === 'number' ? angleClose : pair[index + 1];if (index < probedTo || open === -1 || close === -1) {continue;}diff --git a/packages/richtext/src/markdownLink.ts b/packages/richtext/src/markdownLink.tsindex 62c60b3817..19bf2f51b6 100644--- a/packages/richtext/src/markdownLink.ts+++ b/packages/richtext/src/markdownLink.ts@@ -38,28 +38,29 @@ function isBalanced(url: string): boolean {/** `url` as a Markdown destination that reads back as `url`. */export function markdownDestination(url: string): string {- if (/\s/.test(url)) {- // Only an angle-bracket destination may hold a space (CommonMark §6.3).- return `<${url.replace(/[<>]/g, '\\$&')}>`;+ if (/\s/.test(url) || url.startsWith('<')) {+ // Only an angle-bracket destination may hold a space or open with `<`+ // (CommonMark §6.3).+ return `<${url.replace(/[\\<>]/g, '\\$&')}>`;}return isBalanced(url) ? url : url.replace(/[()]/g, '\\$&');}/*** Lexical's link pattern, with a destination in angle brackets too: any- * characters but brackets and line endings, spaces included.+ * characters but brackets and line endings, spaces included. A destination+ * that opens with `<` is an angle-bracket one or no destination at all+ * (CommonMark §6.3), so `[a](<b>c>)` stays text, as core reads it.*/const LINK_WITH_ANGLE_DESTINATION =- /(?:\[(.+?)\])(?:\((?:(<[^<>\n]*>|[^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))/;+ /(?:\[(.+?)\])(?:\((?:(<[^<>\n]*>|[^()\s<][^()\s]*)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))/;export const LINK_KEEPING_DESTINATIONS: TextMatchTransformer = {...LINK,importRegExp: LINK_WITH_ANGLE_DESTINATION,replace: (textNode, match) => {const destination = match[2];- // Only a whole angle-bracket destination drops its brackets; `<b>c>`- // is a destination as it stands, as core reads it.- if (destination == null || !/^<[^<>\n]*>$/.test(destination)) {+ if (destination == null || !destination.startsWith('<')) {return LINK.replace?.(textNode, match);}// The link's address is what the angle brackets hold.