An open source design system that's fully customizable and agent ready
TypeScript
fix(Markdown): keep a link or image title out of its destination (#6981)
Cindy Zhang884cab425901
diff --git a/.changeset/markdown-link-title-destination.md b/.changeset/markdown-link-title-destination.mdnew file mode 100644index 0000000000..a9e1908877--- /dev/null+++ b/.changeset/markdown-link-title-destination.md@@ -0,0 +1,9 @@+---+'@astryxdesign/core': patch+---++[fix] Markdown links and images go to their destination when the source also gives them a title++`[notes](https://example.com/notes "Release notes")` linked to `https://example.com/notes "Release notes"` — an address that does not exist — and a titled image pointed at a source that could not load. A title after the destination, in double quotes, single quotes, or parentheses, is no longer part of the link or image address, and a destination in angle brackets may contain spaces. Content in any other shape keeps its meaning.++@cixzhangdiff --git a/packages/core/src/Markdown/Markdown.test.tsx b/packages/core/src/Markdown/Markdown.test.tsxindex 9f60398f1a..08e72b1adf 100644--- a/packages/core/src/Markdown/Markdown.test.tsx+++ b/packages/core/src/Markdown/Markdown.test.tsx@@ -329,6 +329,17 @@ describe('Markdown', () => {expect(link.getAttribute('href')).toBe('https://example.com');});+ it('links to the destination, not the destination plus its title', () => {+ render(+ <Markdown>+ {'[notes](https://example.com/notes "Release notes")'}+ </Markdown>,+ );+ expect(screen.getByText('notes').getAttribute('href')).toBe(+ 'https://example.com/notes',+ );+ });+it('adds target="_blank" to external links', () => {render(<Markdown>{'[ext](https://example.com)'}</Markdown>);const link = screen.getByText('ext');diff --git a/packages/core/src/Markdown/parser.test.ts b/packages/core/src/Markdown/parser.test.tsindex 80bbe0c9d1..f2f54c82a6 100644--- a/packages/core/src/Markdown/parser.test.ts+++ b/packages/core/src/Markdown/parser.test.ts@@ -283,6 +283,45 @@ describe('parseInline', () => {}});+ it('keeps a link title out of the destination', () => {+ const cases = [+ ['[t](https://example.com/notes "Notes")', 'https://example.com/notes'],+ ["[t](https://example.com/notes 'Notes')", 'https://example.com/notes'],+ ['[t](https://example.com/notes (Notes))', 'https://example.com/notes'],+ ['[t](https://example.com "Say \\"hi\\"")', 'https://example.com'],+ ['[t](<https://example.com/a b> "Spaced")', 'https://example.com/a b'],+ [+ '[t](https://example.com/wiki/Foo_(bar) "Wiki")',+ 'https://example.com/wiki/Foo_(bar)',+ ],+ ] as const;+ for (const [source, href] of cases) {+ expect(parseInline(source)[0], source).toMatchObject({+ type: 'link',+ href,+ });+ }+ });++ it('keeps an image title out of the source, inline and standalone', () => {+ expect(parseInline('')).toEqual([+ {type: 'image', src: 'img.png', alt: 'alt'},+ ]);+ expect(parseMarkdown('')[0]).toMatchObject({+ type: 'image',+ src: 'img.png',+ });+ });++ it('keeps link content that has no title shape as the destination', () => {+ // A space without a quoted title is not a destination and title pair, so+ // the content keeps its released meaning.+ expect(parseInline('[t](https://example.com/a b)')[0]).toMatchObject({+ type: 'link',+ href: 'https://example.com/a b',+ });+ });+it('handles parentheses in image URLs', () => {const result = parseInline('.png)');expect(result[0].type).toBe('image');diff --git a/packages/core/src/Markdown/parser.ts b/packages/core/src/Markdown/parser.tsindex e31d3b7c1c..09c3411496 100644--- a/packages/core/src/Markdown/parser.ts+++ b/packages/core/src/Markdown/parser.ts@@ -1046,6 +1046,21 @@ function matchReferenceImage(// ---------------------------------------------------------------------------/** Find closing ')' that balances nested parentheses. */+// The content between an inline link's or image's parentheses: a `<…>`+// destination or one without spaces, then an optional `"…"`, `'…'`, or `(…)`+// title after whitespace (CommonMark 0.31, link destinations and titles).+const INLINE_DESTINATION_WITH_TITLE =+ /^\s*(?:<([^<>\n]*)>|([^\s<]\S*?))(?:\s+(?:"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|\((?:[^()\\]|\\.)*\)))?\s*$/s;++/**+ * The destination of an inline link or image, without its title. Content in+ * any other shape keeps its released meaning: all of it is the destination.+ */+function inlineDestination(content: string): string {+ const match = INLINE_DESTINATION_WITH_TITLE.exec(content);+ return match == null ? content : (match[1] ?? match[2] ?? content);+}+function findClosingParen(text: string, start: number): number {let depth = 1;for (let index = start; index < text.length; index++) {@@ -1511,7 +1526,7 @@ function parseInlineImpl(if (altClose !== -1 && text[altClose + 1] === '(') {const srcClose = findClosingParen(text, altClose + 2);if (srcClose !== -1) {- const src = text.slice(altClose + 2, srcClose);+ const src = inlineDestination(text.slice(altClose + 2, srcClose));if (!isSafeMarkdownParserUrl(src)) {// Dangerous scheme — emit as plain text.nodes.push({type: 'text', value: text.slice(i, srcClose + 1)});@@ -1554,7 +1569,7 @@ function parseInlineImpl(if (textClose !== -1 && text[textClose + 1] === '(') {const urlClose = findClosingParen(text, textClose + 2);if (urlClose !== -1) {- const href = text.slice(textClose + 2, urlClose);+ const href = inlineDestination(text.slice(textClose + 2, urlClose));if (!isSafeMarkdownParserUrl(href)) {// Dangerous scheme — emit as plain text instead of a link.nodes.push({type: 'text', value: text.slice(i, urlClose + 1)});@@ -2713,12 +2728,13 @@ function parseMarkdownImpl(// An unsafe src falls through to the paragraph path and renders as// literal text, the same rule the inline image path applies.const imageMatch = line.match(/^!\[([^\]]*)\]\(([^)]+)\)/);+ const imageSrc = imageMatch ? inlineDestination(imageMatch[2]) : '';if (imageMatch &&line.trim() === imageMatch[0] &&- isSafeMarkdownParserUrl(imageMatch[2])+ isSafeMarkdownParserUrl(imageSrc)) {- pushBlock({type: 'image', alt: imageMatch[1], url: imageMatch[2]});+ pushBlock({type: 'image', alt: imageMatch[1], url: imageSrc});index++;continue;}