Skip to content

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

TypeScript

fix(richtext): read code spans before the marks around them (#7066)

Cindy Zhang28b879f42627
diff --git a/packages/richtext/src/markdownCharacterReferences.ts b/packages/richtext/src/markdownCharacterReferences.ts
index 6a81c4ff47..0261489d74 100644
--- a/packages/richtext/src/markdownCharacterReferences.ts
+++ b/packages/richtext/src/markdownCharacterReferences.ts
@@ -29,7 +29,8 @@
import {$isCodeNode} from '@lexical/code';
import {$isLinkNode} from '@lexical/link';
-import {$dfs} from '@lexical/utils';
+import {$isTableCellNode} from '@lexical/table';
+import {$dfs, $findMatchingParent} from '@lexical/utils';
import {
$isElementNode,
$isTextNode,
@@ -89,6 +90,15 @@ function* absentCharacters(text: string): Generator<string> {
* are searched by position, so the whole is O(n log n).
*/
function codeRanges(text: string): Array<readonly [number, number]> {
+ const {fenced, spans} = codeRangesByKind(text);
+ return [...fenced, ...spans].sort((a, b) => a[0] - b[0]);
+}
+
+/** The fenced code blocks of `text`, and its code spans, each in order. */
+function codeRangesByKind(text: string): {
+ fenced: Array<readonly [number, number]>;
+ spans: Array<readonly [number, number]>;
+} {
const ranges: Array<readonly [number, number]> = [];
// Fenced code blocks, line by line.
let lineStart = 0;
@@ -118,6 +128,7 @@ function codeRanges(text: string): Array<readonly [number, number]> {
if (fence != null) {
ranges.push([fence.start, text.length]);
}
+ const fencedCount = ranges.length;
// Code spans, outside fenced code. A backtick run is a string of backticks
// with no backtick on either side. Backslashes are literal inside a code
// span, so they never hide a closing run; an odd number of backslashes
@@ -191,7 +202,10 @@ function codeRanges(text: string): Array<readonly [number, number]> {
ranges.push([openStart, closer + openLength]);
resumeAt = closer + openLength;
}
- return ranges.sort((a, b) => a[0] - b[0]);
+ return {
+ fenced: ranges.slice(0, fencedCount),
+ spans: ranges.slice(fencedCount).sort((a, b) => a[0] - b[0]),
+ };
}
/** The characters a backslash escapes (CommonMark 0.31 §2.4). */
@@ -508,6 +522,216 @@ export function protectLinkDestinationParentheses(
return {markdown: output + markdown.slice(copied), standIns};
}
+/** The code spans a stand-in pass replaced: each stand-in's code. */
+export interface ProtectedCodeSpans {
+ readonly markdown: string;
+ readonly spans: ReadonlyMap<string, string>;
+ /** Stand-ins for backticks in link destinations and titles. */
+ readonly standIns: ReadonlyMap<string, StandIn>;
+}
+
+/** Whether `text` holds a `|` with no backslash before it. */
+function hasUnescapedPipe(text: string): boolean {
+ for (let index = text.indexOf('|'); index !== -1;) {
+ let backslashes = 0;
+ while (text[index - 1 - backslashes] === '\\') {
+ backslashes++;
+ }
+ if (backslashes % 2 === 0) {
+ return true;
+ }
+ index = text.indexOf('|', index + 1);
+ }
+ return false;
+}
+
+/** A GFM table's delimiter row: cells of hyphens with optional colons. */
+const TABLE_DELIMITER_ROW = /^\s*\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)*\|?\s*$/;
+
+/**
+ * The ranges of `markdown` a code span must not start in: inline link
+ * destinations and titles, from the `(` after a `]` to its paired `)`. The
+ * link reads its destination before any code span there, as core and
+ * CommonMark read it.
+ */
+function linkTargetRanges(
+ markdown: string,
+ code: ReadonlyArray<readonly [number, number]>,
+): Array<readonly [number, number]> {
+ const ranges: Array<readonly [number, number]> = [];
+ if (!markdown.includes('](')) {
+ return ranges;
+ }
+ const pair = pairedDelimiters(markdown, code);
+ for (
+ let index = markdown.indexOf('](');
+ index !== -1;
+ index = markdown.indexOf('](', index + 1)
+ ) {
+ if (pair[index] !== -1 && pair[index + 1] !== -1) {
+ ranges.push([index + 1, pair[index + 1] + 1]);
+ }
+ }
+ return ranges;
+}
+
+/** The ranges of `markdown` that are GFM table rows, header row included. */
+function tableRowRanges(markdown: string): Array<readonly [number, number]> {
+ const ranges: Array<readonly [number, number]> = [];
+ const lines = markdown.split('\n');
+ const starts: number[] = [];
+ let offset = 0;
+ for (const line of lines) {
+ starts.push(offset);
+ offset += line.length + 1;
+ }
+ for (let index = 1; index < lines.length; index++) {
+ const line = lines[index];
+ if (
+ line.includes('|') &&
+ TABLE_DELIMITER_ROW.test(line) &&
+ lines[index - 1].includes('|')
+ ) {
+ let end = index + 1;
+ while (end < lines.length && lines[end].trim() !== '') {
+ end++;
+ }
+ ranges.push([starts[index - 1], starts[end - 1] + lines[end - 1].length]);
+ index = end;
+ }
+ }
+ return ranges;
+}
+
+/**
+ * Returns `markdown` with each code span outside fenced code replaced by a
+ * private-use stand-in, and the code each stands for, as core Markdown reads
+ * it. Code spans bind tighter than emphasis and links (CommonMark 0.31
+ * §6.1), but Lexical's import applies a code span before an earlier mark on
+ * the same line, which breaks every mark around code that follows another
+ * (`~~a~~ ~~`c`~~`); with the code out of the way, the marks pair first and
+ * the code comes back inside them. Backticks in a link's destination or
+ * title are the link's, never a code span: they become stand-ins too, put
+ * back into the link's address and title after import, so Lexical cannot
+ * pair them into code either. In a table row, a span holding an unescaped
+ * `|` is left alone, so the row still splits its cells there, as GFM does.
+ */
+export function protectCodeSpans(markdown: string): ProtectedCodeSpans {
+ const spans = new Map<string, string>();
+ const standIns = new Map<string, StandIn>();
+ if (!markdown.includes('`')) {
+ return {markdown, spans, standIns};
+ }
+ const code = codeRangesByKind(markdown);
+ const targets = linkTargetRanges(markdown, [...code.fenced, ...code.spans]);
+ const rows = tableRowRanges(markdown);
+ // Whether a position lies in one of ranges sorted by start, for positions
+ // asked in order: one pass over the ranges in all.
+ const cursor = (ranges: ReadonlyArray<readonly [number, number]>) => {
+ let next = 0;
+ let coveredTo = -1;
+ return (position: number): boolean => {
+ while (next < ranges.length && ranges[next][0] <= position) {
+ coveredTo = Math.max(coveredTo, ranges[next][1]);
+ next++;
+ }
+ return position < coveredTo;
+ };
+ };
+ const inTarget = cursor(targets);
+ const inRow = cursor(rows);
+ // Replacements in source order: code spans, and backticks in targets.
+ const replacements: Array<{
+ readonly start: number;
+ readonly end: number;
+ readonly code: string | null;
+ }> = [];
+ for (const [start, end] of code.spans) {
+ const source = markdown.slice(start, end);
+ const isInRow = inRow(start);
+ if (inTarget(start) || (isInRow && hasUnescapedPipe(source))) {
+ continue;
+ }
+ const [only, ...rest] = parseInlineAst(source);
+ if (rest.length > 0 || only?.type !== 'inlineCode') {
+ continue;
+ }
+ replacements.push({start, end, code: only.value});
+ }
+ for (const [from, to] of targets) {
+ for (
+ let index = markdown.indexOf('`', from);
+ index !== -1 && index < to;
+ index = markdown.indexOf('`', index + 1)
+ ) {
+ replacements.push({start: index, end: index + 1, code: null});
+ }
+ }
+ replacements.sort((a, b) => a.start - b.start);
+ const available = absentCharacters(markdown);
+ let output = '';
+ let copied = 0;
+ for (const {start, end, code: content} of replacements) {
+ if (start < copied) {
+ continue;
+ }
+ const next = available.next();
+ if (next.done === true) {
+ break;
+ }
+ if (content == null) {
+ standIns.set(next.value, {kind: 'literal', text: '`'});
+ } else {
+ spans.set(next.value, content);
+ }
+ output += markdown.slice(copied, start) + next.value;
+ copied = end;
+ }
+ return {markdown: output + markdown.slice(copied), spans, standIns};
+}
+
+/**
+ * Puts each code span back where its stand-in sits under `node`: the code as
+ * text with the code format, inside whatever marks the stand-in took. In a
+ * table cell an escaped `\|` in code is a `|`, as GFM reads cells.
+ */
+export function $restoreCodeSpans(
+ node: ElementNode,
+ spans: ReadonlyMap<string, string>,
+): void {
+ if (spans.size === 0) {
+ return;
+ }
+ for (const text of node.getAllTextNodes()) {
+ const content = text.getTextContent();
+ const offsets: number[] = [];
+ let offset = 0;
+ for (const character of content) {
+ if (spans.has(character)) {
+ offsets.push(offset, offset + character.length);
+ }
+ offset += character.length;
+ }
+ if (offsets.length === 0) {
+ continue;
+ }
+ const pieces = text.splitText(
+ ...offsets.filter(point => point > 0 && point < content.length),
+ );
+ for (const piece of pieces) {
+ const code = spans.get(piece.getTextContent());
+ if (code == null) {
+ continue;
+ }
+ const inCell = $findMatchingParent(piece, $isTableCellNode) != null;
+ piece.setTextContent(inCell ? code.replace(/\\\|/g, '|') : code);
+ if (!piece.hasFormat('code')) {
+ piece.toggleFormat('code');
+ }
+ }
+ }
+}
+
/**
* Returns `markdown` with every character reference outside code replaced by
* a private-use stand-in that occurs nowhere else in it, and the escaped `&`
diff --git a/packages/richtext/src/markdownCodeSpans.test.ts b/packages/richtext/src/markdownCodeSpans.test.ts
new file mode 100644
index 0000000000..1991e90dfe
--- /dev/null
+++ b/packages/richtext/src/markdownCodeSpans.test.ts
@@ -0,0 +1,399 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+import {describe, expect, it} from 'vitest';
+import {createHeadlessEditor} from '@lexical/headless';
+import {$getRoot, $isTextNode} from 'lexical';
+import {parseInlineAst} from '@astryxdesign/core/Markdown/parser';
+import {parseMarkdownAst} from '@astryxdesign/core/Markdown/parser';
+import {DEFAULT_NODES} from './editorNodes';
+import {DEFAULT_TRANSFORMERS} from './markdownTable';
+import {
+ $exportMarkdownKeepingSource,
+ importMarkdownKeepingSource,
+} from './markdownSource';
+import {
+ editorStateJSONToMarkdown,
+ markdownToEditorStateJSON,
+} from './markdownSerializers';
+
+type Json = Record<string, unknown>;
+
+const FORMATS: ReadonlyArray<readonly [number, string]> = [
+ [1, 'strong'],
+ [2, 'emphasis'],
+ [4, 'delete'],
+ [16, 'code'],
+];
+
+/** Each run of text with the marks around it, as RichText imports it. */
+function richText(markdown: string): string[] {
+ const runs: string[] = [];
+ const visit = (node: Json) => {
+ if (node.type === 'text') {
+ const format = node.format as number;
+ const marks = FORMATS.filter(([bit]) => (format & bit) !== 0)
+ .map(([, name]) => name)
+ .join('+');
+ runs.push(`${node.text as string}|${marks}`);
+ }
+ ((node.children as Json[]) ?? []).forEach(visit);
+ };
+ visit((JSON.parse(markdownToEditorStateJSON(markdown)) as {root: Json}).root);
+ return runs;
+}
+
+/** The same, as core Markdown reads it. */
+function core(markdown: string): string[] {
+ const runs: string[] = [];
+ const visit = (node: Json, marks: string[]) => {
+ if (node.type === 'text') {
+ runs.push(`${node.value as string}|${[...marks].sort(order).join('+')}`);
+ } else if (node.type === 'inlineCode') {
+ runs.push(
+ `${node.value as string}|${[...marks, 'code'].sort(order).join('+')}`,
+ );
+ } else {
+ const next = ['strong', 'emphasis', 'delete'].includes(
+ node.type as string,
+ )
+ ? [...marks, node.type as string]
+ : marks;
+ ((node.children as Json[]) ?? []).forEach(child => visit(child, next));
+ }
+ };
+ const order = (a: string, b: string) =>
+ FORMATS.findIndex(([, name]) => name === a) -
+ FORMATS.findIndex(([, name]) => name === b);
+ (parseMarkdownAst(markdown).children as unknown as Json[]).forEach(node =>
+ visit(node, []),
+ );
+ return runs;
+}
+
+describe('code spans read before the marks around them (CommonMark 0.31 §6.1)', () => {
+ it.each([
+ '~~a~~ ~~`c`~~',
+ '*a* **b `c` d**',
+ '*a* *`code`*',
+ '**bold `code` more**',
+ '`a*b*c` and *x*',
+ '``a`b`` and ~~`x`~~',
+ 'A `\\[x\\]` span',
+ ])('reads %j as core does, and round-trips it', markdown => {
+ expect(richText(markdown)).toEqual(core(markdown));
+ expect(
+ editorStateJSONToMarkdown(markdownToEditorStateJSON(`${markdown}\n`)),
+ ).toBe(`${markdown}\n`);
+ });
+
+ it('reads code in table cells, with an escaped pipe as a pipe', () => {
+ const markdown =
+ '| a | b |\n| --- | --- |\n| ~~x~~ ~~`c`~~ | `p \\| q` |\n';
+ expect(richText(markdown)).toEqual([
+ 'a|',
+ 'b|',
+ 'x|delete',
+ ' |',
+ 'c|delete+code',
+ 'p | q|code',
+ ]);
+ expect(editorStateJSONToMarkdown(markdownToEditorStateJSON(markdown))).toBe(
+ markdown,
+ );
+ });
+
+ it('keeps the marks around code through an edit', () => {
+ const editor = createHeadlessEditor({
+ nodes: [...DEFAULT_NODES],
+ onError(error) {
+ throw error;
+ },
+ });
+ importMarkdownKeepingSource(editor, '*a* **b `c` d** end\n', [
+ ...DEFAULT_TRANSFORMERS,
+ ]);
+ editor.update(
+ () => {
+ const last = $getRoot().getAllTextNodes().at(-1);
+ if (!$isTextNode(last)) {
+ throw new Error('No text');
+ }
+ last.setTextContent(`${last.getTextContent()}!`);
+ },
+ {discrete: true},
+ );
+ const exported = editor
+ .getEditorState()
+ .read(() => $exportMarkdownKeepingSource([...DEFAULT_TRANSFORMERS]));
+ expect(richText(exported)).toEqual(core('*a* **b `c` d** end!'));
+ });
+});
+
+/** Each link's URL with its text runs and their marks, as RichText imports it. */
+function richLinks(markdown: string): Array<{url: string; runs: string[]}> {
+ const links: Array<{url: string; runs: string[]}> = [];
+ const visit = (node: Json, link: {url: string; runs: string[]} | null) => {
+ if (node.type === 'link') {
+ const entry = {url: node.url as string, runs: [] as string[]};
+ links.push(entry);
+ ((node.children as Json[]) ?? []).forEach(child => visit(child, entry));
+ return;
+ }
+ if (node.type === 'text' && link != null) {
+ const format = node.format as number;
+ link.runs.push(
+ `${node.text as string}|${(format & 16) !== 0 ? 'code' : ''}`,
+ );
+ }
+ ((node.children as Json[]) ?? []).forEach(child => visit(child, link));
+ };
+ visit(
+ (JSON.parse(markdownToEditorStateJSON(markdown)) as {root: Json}).root,
+ null,
+ );
+ return links;
+}
+
+describe('code spans in link text', () => {
+ it('shows a refused link holding code whole, as core shows it', () => {
+ const markdown = 'See [`x`](javascript:y) now';
+ expect(richLinks(markdown)).toEqual([]);
+ // Plain text with no marks on either surface, however it is split.
+ const plain = (runs: string[]): string =>
+ runs.map(run => run.replace(/\|$/, '')).join('');
+ expect(richText(markdown).every(run => run.endsWith('|'))).toBe(true);
+ expect(plain(richText(markdown))).toBe(plain(core(markdown)));
+ expect(plain(core(markdown))).toBe('See [`x`](javascript:y) now');
+ });
+
+ it('still links a safe destination, with its code as code', () => {
+ expect(richLinks('See [`x`](https://example.com) now')).toEqual([
+ {url: 'https://example.com', runs: ['x|code']},
+ ]);
+ });
+});
+
+/** Each link's URL and title, as RichText imports it. */
+function richTargets(
+ markdown: string,
+): Array<{url: string; title: string | null}> {
+ const targets: Array<{url: string; title: string | null}> = [];
+ const visit = (node: Json) => {
+ if (node.type === 'link') {
+ targets.push({
+ url: node.url as string,
+ title: (node.title as string | null | undefined) ?? null,
+ });
+ }
+ ((node.children as Json[]) ?? []).forEach(visit);
+ };
+ visit((JSON.parse(markdownToEditorStateJSON(markdown)) as {root: Json}).root);
+ return targets;
+}
+
+/** Each link's or image's URL and title, as core reads it. */
+function coreTargets(
+ markdown: string,
+): Array<{url: string; title: string | null}> {
+ const targets: Array<{url: string; title: string | null}> = [];
+ const visit = (node: Json) => {
+ if (node.type === 'link' || node.type === 'image') {
+ targets.push({
+ url: node.url as string,
+ title: (node.title as string | null | undefined) ?? null,
+ });
+ }
+ ((node.children as Json[]) ?? []).forEach(visit);
+ };
+ (parseInlineAst(markdown) as unknown as Json[]).forEach(visit);
+ return targets;
+}
+
+/** Whether `text` holds a private-use character: a stand-in left behind. */
+const PRIVATE_USE = /[\uE000-\uF8FF]|[\u{F0000}-\u{10FFFD}]/u;
+
+/** Edits the last text of `markdown`'s first block, exports, and re-imports. */
+function editedTargets(markdown: string): {
+ exported: string;
+ targets: Array<{url: string; title: string | null}>;
+} {
+ const editor = createHeadlessEditor({
+ nodes: [...DEFAULT_NODES],
+ onError(error) {
+ throw error;
+ },
+ });
+ importMarkdownKeepingSource(editor, `${markdown} end\n`, [
+ ...DEFAULT_TRANSFORMERS,
+ ]);
+ editor.update(
+ () => {
+ const last = $getRoot().getAllTextNodes().at(-1);
+ if (!$isTextNode(last)) {
+ throw new Error('No text');
+ }
+ last.setTextContent(`${last.getTextContent()}!`);
+ },
+ {discrete: true},
+ );
+ const exported = editor
+ .getEditorState()
+ .read(() => $exportMarkdownKeepingSource([...DEFAULT_TRANSFORMERS]));
+ return {exported, targets: richTargets(exported)};
+}
+
+describe("backticks in a link destination or title are the link's", () => {
+ it.each([
+ '[a](`x`)',
+ '[a](https://e.com/`x`)',
+ '![i](`x`)',
+ '[a](b "`t`")',
+ '`c` then [a](`x`)',
+ ])('reads %j with the destination core reads', markdown => {
+ expect(richTargets(markdown).map(({url}) => url)).toEqual(
+ coreTargets(markdown).map(({url}) => url),
+ );
+ });
+
+ it('keeps a title holding backticks as written', () => {
+ // Core does not draw titles; RichText keeps one for export.
+ expect(richTargets('[a](b "`t`")')).toEqual([{url: 'b', title: '`t`'}]);
+ });
+
+ it.each([
+ '[a](`x`)',
+ '[a](https://e.com/`x`)',
+ '[a](<`x`>)',
+ '![i](`x`)',
+ '[a](b "`t`")',
+ ])('keeps %j through an edit, with no stand-in left behind', markdown => {
+ const before = richTargets(markdown);
+ const {exported, targets} = editedTargets(markdown);
+ expect(PRIVATE_USE.test(exported)).toBe(false);
+ expect(targets).toEqual(before);
+ });
+
+ it('leaves no stand-in in any link, for random backticks in destinations, titles, and text', () => {
+ let state = 11;
+ const random = () => {
+ state = (state * 1103515245 + 12345) % 2147483648;
+ return state / 2147483648;
+ };
+ const pick = <T>(items: ReadonlyArray<T>): T =>
+ items[Math.floor(random() * items.length)];
+ for (let round = 0; round < 200; round++) {
+ const destination = pick([
+ '`x`',
+ 'a`b`c',
+ 'https://e.com/``y``',
+ '<`x`>',
+ '`x`?q=`y`',
+ 'p',
+ ]);
+ const title = pick(['', ' "`t`"', ' "a `b` c"']);
+ const text = pick(['a', '`c`', 'a `c` b', '**`c`**']);
+ const markdown = `${pick(['', '`z` ', '~~s~~ '])}[${text}](${destination}${title})${pick(['', ' `w`'])}`;
+ const json = markdownToEditorStateJSON(markdown);
+ expect(PRIVATE_USE.test(json), markdown).toBe(false);
+ const {exported} = editedTargets(markdown);
+ expect(PRIVATE_USE.test(exported), markdown).toBe(false);
+ }
+ });
+});
+
+describe('a code span holding `|` outside a table', () => {
+ it.each(['~~s~~ ~~`a|b`~~', '*e* **[`a|b`](https://example.com)**'])(
+ 'reads the marks around %j as core does',
+ markdown => {
+ expect(richText(markdown)).toEqual(core(markdown));
+ },
+ );
+
+ it('stays code in link text, after an earlier code span on the line', () => {
+ const markdown = '`x` and [`a|b`](https://example.com)';
+ expect(richLinks(markdown)).toEqual([
+ {url: 'https://example.com', runs: ['a|b|code']},
+ ]);
+ expect(richText(markdown)).toEqual(core(markdown));
+ });
+});
+
+/** Each link's URL and title in a document, as RichText imports it. */
+function allTargets(json: string): Array<{url: string; title: string | null}> {
+ const targets: Array<{url: string; title: string | null}> = [];
+ const visit = (node: Json) => {
+ if (node.type === 'link') {
+ targets.push({
+ url: node.url as string,
+ title: (node.title as string | null | undefined) ?? null,
+ });
+ }
+ ((node.children as Json[]) ?? []).forEach(visit);
+ };
+ visit((JSON.parse(json) as {root: Json}).root);
+ return targets;
+}
+
+describe('a link with backticks in its destination or title after a code-text link', () => {
+ it.each([
+ [
+ '[`a`](u) [b](`x`)',
+ [
+ {url: 'u', title: null},
+ {url: '`x`', title: null},
+ ],
+ ],
+ [
+ '[`a`](u) [b](https://e.com/`x`)',
+ [
+ {url: 'u', title: null},
+ {url: 'https://e.com/`x`', title: null},
+ ],
+ ],
+ [
+ '[`a`](u) [b](c "`t`")',
+ [
+ {url: 'u', title: null},
+ {url: 'c', title: '`t`'},
+ ],
+ ],
+ ])('keeps both links in %j, through an edit', (markdown, expected) => {
+ expect(richTargets(markdown)).toEqual(expected);
+ expect(richTargets(markdown).map(({url}) => url)).toEqual(
+ coreTargets(markdown).map(({url}) => url),
+ );
+ const {exported, targets} = editedTargets(markdown);
+ expect(PRIVATE_USE.test(exported)).toBe(false);
+ expect(targets).toEqual(expected);
+ });
+
+ it('loses no link and leaves no stand-in, for random code-text links before backtick targets', () => {
+ let state = 23;
+ const random = () => {
+ state = (state * 1103515245 + 12345) % 2147483648;
+ return state / 2147483648;
+ };
+ const pick = <T>(items: ReadonlyArray<T>): T =>
+ items[Math.floor(random() * items.length)];
+ for (let round = 0; round < 200; round++) {
+ const before = pick([
+ '[`a`](u)',
+ '`c` [`a`](u)',
+ '[**`a`**](u)',
+ '[x](u)',
+ ]);
+ const destination = pick(['`x`', 'p`y`q', 'https://e.com/``z``', 'p']);
+ const title = pick(['', ' "`t`"', ' "a `b` c"']);
+ const markdown = `${before} and [b](${destination}${title})${pick(['', ' `w`'])}`;
+ const json = markdownToEditorStateJSON(markdown);
+ expect(PRIVATE_USE.test(json), markdown).toBe(false);
+ expect(
+ allTargets(json).map(({url}) => url),
+ markdown,
+ ).toEqual(coreTargets(markdown).map(({url}) => url));
+ const {exported, targets} = editedTargets(markdown);
+ expect(PRIVATE_USE.test(exported), markdown).toBe(false);
+ expect(targets, markdown).toEqual(allTargets(json));
+ }
+ });
+});
diff --git a/packages/richtext/src/markdownSource.test.tsx b/packages/richtext/src/markdownSource.test.tsx
index db01a0f66f..caf10199ad 100644
--- a/packages/richtext/src/markdownSource.test.tsx
+++ b/packages/richtext/src/markdownSource.test.tsx
@@ -26,7 +26,9 @@ import {DEFAULT_NODES} from './editorNodes';
import {normalizeListIndentation} from './markdownListIndentation';
import {
$restoreCharacterReferences,
+ $restoreCodeSpans,
protectCharacterReferences,
+ protectCodeSpans,
} from './markdownCharacterReferences';
import {DEFAULT_TRANSFORMERS} from './markdownTable';
import {
@@ -456,16 +458,20 @@ describe('Markdown source preservation (spec:AST-062)', () => {
});
editor.update(
() => {
+ // The import reads code spans and character references through
+ // stand-ins; the reference does too.
+ const code = protectCodeSpans(
+ normalizeListIndentation(
+ markdown.replace(/^\uFEFF/, '').replace(/\r\n/g, '\n'),
+ ),
+ );
const {markdown: protectedMarkdown, standIns} =
- protectCharacterReferences(
- normalizeListIndentation(
- markdown.replace(/^\uFEFF/, '').replace(/\r\n/g, '\n'),
- ),
- );
+ protectCharacterReferences(code.markdown);
$convertFromMarkdownString(protectedMarkdown, [
...DEFAULT_TRANSFORMERS,
]);
$restoreCharacterReferences($getRoot(), standIns);
+ $restoreCodeSpans($getRoot(), code.spans);
$nestFollowingLists($getRoot());
$joinSoftLineBreaks($getRoot());
},
diff --git a/packages/richtext/src/markdownSource.ts b/packages/richtext/src/markdownSource.ts
index 960f0b4481..6ac218c7c7 100644
--- a/packages/richtext/src/markdownSource.ts
+++ b/packages/richtext/src/markdownSource.ts
@@ -53,8 +53,10 @@ import {isMarkedHardLineBreak} from './markdownHardLineBreak';
import {normalizeListIndentation} from './markdownListIndentation';
import {
$restoreCharacterReferences,
+ $restoreCodeSpans,
protectBackslashEscapes,
protectCharacterReferences,
+ protectCodeSpans,
protectLinkDestinationParentheses,
protectRefusedLinks,
$unwrapRefusedLinks,
@@ -366,7 +368,7 @@ export function importMarkdownKeepingSource(
root.append(holder);
// Lexical imports LF lines; the record keeps the authored endings.
// Adopted plugins' nodes, links core refuses (as their source text),
- // then backslash escapes and parentheses in
+ // then code spans, then backslash escapes and parentheses in
// link destinations, then character references go through as
// stand-ins: plugin nodes come back as extension nodes holding their
// source, escapes and parentheses as the literal characters,
@@ -375,8 +377,12 @@ export function importMarkdownKeepingSource(
withoutCarriageReturns(importChunks[index]?.content ?? chunk.content),
plugins,
);
+ // Links core refuses go first, on the source as written, so one whose
+ // text holds code (`[`x`](javascript:y)`) shows whole as core shows
+ // it; code spans next, so a safe link's code still reads as code.
const refused = protectRefusedLinks(shielded.markdown);
- const escaped = protectBackslashEscapes(refused.markdown);
+ const code = protectCodeSpans(refused.markdown);
+ const escaped = protectBackslashEscapes(code.markdown);
const destinations = protectLinkDestinationParentheses(
escaped.markdown,
);
@@ -386,11 +392,13 @@ export function importMarkdownKeepingSource(
holder,
new Map([
...refused.standIns,
+ ...code.standIns,
...escaped.standIns,
...destinations.standIns,
...referenced.standIns,
]),
);
+ $restoreCodeSpans(holder, code.spans);
$unwrapRefusedLinks(holder);
$restoreExtensionSources(holder, shielded.standIns);
$joinSoftLineBreaks(holder);