An open source design system that's fully customizable and agent ready
TypeScript
// Copyright (c) Meta Platforms, Inc. and affiliates.
import {describe, it, expect} from 'vitest';
import {createHeadlessEditor} from '@lexical/headless';
import {$getRoot} from 'lexical';
import {parseInline, parseMarkdown} from '@astryxdesign/core/Markdown/parser';
import {DEFAULT_NODES} from './editorNodes';
import {
editorStateJSONToMarkdown,
markdownToEditorStateJSON,
} from './markdownSerializers';
interface SerializedNode {
readonly type: string;
readonly text?: string;
readonly children?: ReadonlyArray<SerializedNode>;
}
/** The text RichText imports for a one-paragraph document. */
function richTextText(markdown: string): string {
const {root} = JSON.parse(markdownToEditorStateJSON(markdown)) as {
root: SerializedNode;
};
const texts: Array<string> = [];
const visit = (node: SerializedNode) => {
if (node.text != null) {
texts.push(node.text);
}
node.children?.forEach(visit);
};
visit(root);
return texts.join('');
}
/** The text core Markdown renders for the same paragraph. */
function markdownText(markdown: string): string {
return parseInline(markdown)
.map(node =>
'content' in node
? String(node.content)
: 'children' in node && Array.isArray(node.children)
? node.children
.map(child => ('content' in child ? String(child.content) : ''))
.join('')
: '',
)
.join('');
}
// One table for both surfaces (spec:AST-061 DEC-5).
const CASES = [
'©',
'©',
'© and ©',
'Fish & chips',
'&copy;',
'≂̸',
'&unknown;',
'© without a semicolon',
'& copy;',
'� � �',
'�',
'a b',
'\\© is escaped',
// Backslashes inside a code span are literal and close nothing early.
'Paths like `C:\\` & `D:\\` work',
'Path `C:\\` then � and ©',
// An escaped opener, and spans of one, two, and three backticks.
'\\`&` stays text',
'``a ` & b`` and &',
'Then ```a `` & b``` and ©',
// An unclosed run is literal.
'`unclosed & text',
'\\© is escaped too',
'\\\\© follows an escaped backslash',
'code `©` stays',
];
describe('character references (spec:AST-061 FR7, DEC-5)', () => {
it('import exactly as core Markdown renders them', () => {
for (const source of CASES) {
expect(richTextText(`${source}\n`), source).toBe(markdownText(source));
}
});
it('decode the same way in headings, lists, quotes, links, and table cells', () => {
const {root} = JSON.parse(
markdownToEditorStateJSON(
[
'# Café — menu',
'',
'- Fish & chips',
'> © 2026',
'',
'[Terms & conditions](https://example.com/?a=1&b=2 "T&C")',
'',
'| Item & size | Price |',
'| --- | --- |',
'| Tea \\| ½ `©` **©** | � \\© |',
'',
].join('\n'),
),
) as {root: SerializedNode & {children: Array<SerializedNode>}};
const texts: Array<string> = [];
const links: Array<{url?: unknown; title?: unknown}> = [];
const visit = (node: SerializedNode & {url?: unknown; title?: unknown}) => {
if (node.text != null) {
texts.push(node.text);
}
if (node.type === 'link') {
links.push({url: node.url, title: node.title});
}
(node.children as Array<typeof node> | undefined)?.forEach(visit);
};
visit(root);
const all = texts.join('|');
expect(all).toContain('Café — menu');
expect(all).toContain('Fish & chips');
expect(all).toContain('© 2026');
expect(all).toContain('Terms & conditions');
expect(all).toContain('Item & size');
expect(all).toContain('½');
// Inline code keeps the reference; strong text decodes it.
expect(texts).toContain('©');
expect(texts).toContain('©');
// Not a reference, and an escaped one: literal.
expect(all).toContain('�');
expect(all).toContain('©');
expect(links).toEqual([
{url: 'https://example.com/?a=1&b=2', title: 'T&C'},
]);
});
it('decode thousands of references in one pass, without extra nodes', () => {
for (const count of [2000, 6000, 10000]) {
const unit = '© © � \\& &nope; ';
const markdown = `${unit.repeat(count / 5)}\n`;
const started = performance.now();
const {root} = JSON.parse(markdownToEditorStateJSON(markdown)) as {
root: SerializedNode;
};
const elapsed = performance.now() - started;
const paragraph = root.children?.[0];
// One paragraph, one text node: nothing split per reference.
expect(paragraph?.children, String(count)).toHaveLength(1);
const expected = '© © � & &nope; '.repeat(count / 5);
// Compared as a flag, so a mismatch does not print the whole text.
expect(paragraph?.children?.[0].text === expected, String(count)).toBe(
true,
);
expect(elapsed, String(count)).toBeLessThan(5000);
}
});
it('find code spans the CommonMark way: only a run of the same length closes one', () => {
// A double run inside a single-backtick span neither closes it nor opens
// one, so the whole is code and the reference stays as written.
expect(richTextText('`one`` & two`\n')).toBe('one`` & two');
// Fenced code and CRLF: the reference after the fence decodes.
expect(richTextText('```\r\n©\r\n```\r\n\r\n©\r\n')).toBe(
'©©',
);
});
it('stay literal in fenced code, as core Markdown keeps them', () => {
const markdown = '```\n© ©\n```\n';
expect(richTextText(markdown)).toBe('© ©');
expect(parseMarkdown(markdown)[0]).toMatchObject({
content: '© ©',
});
});
it('keep an untouched paragraph exact and write decoded text back so it reads the same (spec:AST-062)', () => {
const markdown = 'A © B &copy; C ©\n';
expect(editorStateJSONToMarkdown(markdownToEditorStateJSON(markdown))).toBe(
markdown,
);
const editor = createHeadlessEditor({
namespace: 'astryx-character-reference-test',
nodes: [...DEFAULT_NODES],
onError(error: Error) {
throw error;
},
});
editor.setEditorState(
editor.parseEditorState(markdownToEditorStateJSON(markdown)),
);
editor.update(
() => {
const text = $getRoot().getAllTextNodes()[0];
text?.setTextContent(`${text.getTextContent()}!`);
},
{discrete: true},
);
const exported = editorStateJSONToMarkdown(
JSON.stringify(editor.getEditorState().toJSON()),
);
// The decoded `©` text is escaped, so it does not decode again.
expect(exported).toBe('A © B \\© C ©!\n');
expect(richTextText(exported)).toBe('A © B © C ©!');
});
});