An open source design system that's fully customizable and agent ready
TypeScript
fix(Markdown): keep a definition-shaped line whose label is over 999 characters (#7123)
Cindy Zhang6fb69ac029fb
diff --git a/.changeset/markdown-long-link-label-lines.md b/.changeset/markdown-long-link-label-lines.mdnew file mode 100644index 0000000000..0ba8465038--- /dev/null+++ b/.changeset/markdown-long-link-label-lines.md@@ -0,0 +1,8 @@+---+'@astryxdesign/core': patch+---++[fix] Markdown: keep a definition-shaped line whose label is over 999 characters+@cixzhang++`Markdown` no longer drops a line shaped like a link reference definition whose label holds more than 999 characters. CommonMark allows no such definition, so the line now shows as text, as a reference with that label already did.diff --git a/packages/core/src/Markdown/longLinkLabels.test.tsx b/packages/core/src/Markdown/longLinkLabels.test.tsxnew file mode 100644index 0000000000..846fe3080e--- /dev/null+++ b/packages/core/src/Markdown/longLinkLabels.test.tsx@@ -0,0 +1,49 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {describe, expect, it} from 'vitest';+import {+ createIncrementalState,+ parseMarkdown,+ parseMarkdownAst,+ parseMarkdownIncremental,+} from './parser';++type Json = {type: string; value?: string; url?: string; children?: Json[]};++const textOf = (node: Json): string =>+ node.value ?? (node.children ?? []).map(textOf).join('');++const blocks = (markdown: string) =>+ parseMarkdownAst(markdown).children as unknown as Json[];++describe('a link label over 999 characters defines nothing (CommonMark 0.31 §4.7)', () => {+ const longLabel = 'a'.repeat(1000);+ const longDefinition = `[${longLabel}]: /u`;++ it('keeps a definition line with a 1,000-character label as text', () => {+ const markdown = `before\n\n${longDefinition}\n\nafter`;+ expect(blocks(markdown).map(node => [node.type, textOf(node)])).toEqual([+ ['paragraph', 'before'],+ ['paragraph', longDefinition],+ ['paragraph', 'after'],+ ]);+ });++ it('still reads a definition with a 999-character label', () => {+ const label = 'a'.repeat(999);+ const [paragraph] = blocks(`[${label}]: /u\n\n[${label}]`);+ expect(paragraph.type).toBe('paragraph');+ expect(paragraph.children?.[0]).toMatchObject({type: 'link', url: '/u'});+ });++ it('streams the long line to the full parse', () => {+ const markdown = `before\n\n${longDefinition}\n\nafter`;+ const state = createIncrementalState();+ let streamed = parseMarkdownIncremental('', state);+ for (let end = 0; end < markdown.length;) {+ end = Math.min(markdown.length, end + 37);+ 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.tsindex 57149010d0..6ef7f39daf 100644--- a/packages/core/src/Markdown/parser.ts+++ b/packages/core/src/Markdown/parser.ts@@ -674,6 +674,9 @@ function resolveOptions(const LINK_DEFINITION_RE =/^ {0,3}\[([^\]^](?:\\.|[^\]\\])*)\]:[ \t]*(?:<([^<>\n]*)>|(\S+))([ \t]+(?:"[^"\n]*"|'[^'\n]*'|\([^()\n]*\)))?[ \t]*$/;+/** The most characters a link label may hold (CommonMark 0.31 §4.7). */+const MAX_LINK_LABEL_LENGTH = 999;+// A line that is nothing but a title — the continuation form allowed when a// definition's destination is followed by its title on the next line.const LINK_TITLE_ONLY_RE = /^ {0,3}(?:"[^"\n]*"|'[^'\n]*'|\([^()\n]*\))[ \t]*$/;@@ -831,7 +834,9 @@ function matchLinkDefinition(line: string,): {label: string; destination: string; hasTitle: boolean} | null {const match = LINK_DEFINITION_RE.exec(line);- if (match == null) {+ // A longer label defines nothing, so the line stays text, as a reference+ // with that label does.+ if (match == null || match[1].length > MAX_LINK_LABEL_LENGTH) {return null;}const label = normalizeLinkLabel(match[1]);@@ -1477,9 +1482,6 @@ interface InlineIndex {openerClose(open: number): number;}-/** The most characters a link label may hold (CommonMark 0.31 §4.7). */-const MAX_LINK_LABEL_LENGTH = 999;-function inlineIndexOf(text: string,linkDefs?: ReadonlyMap<string, string>,