Skip to content

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

TypeScript

fix(richtext): keep a thematic break written under a block as written when the block changes (#7017)

Cindy Zhangefac39f55f6a
diff --git a/packages/richtext/src/markdownSource.test.tsx b/packages/richtext/src/markdownSource.test.tsx
index 80a1dbc64c..db01a0f66f 100644
--- a/packages/richtext/src/markdownSource.test.tsx
+++ b/packages/richtext/src/markdownSource.test.tsx
@@ -339,6 +339,104 @@ describe('Markdown source preservation (spec:AST-062)', () => {
);
});
+ it('keeps a rule beside an edited block as written (FR2)', () => {
+ const contents = (markdown: string) =>
+ splitMarkdownChunks(markdown).map(chunk => chunk.content);
+ // A thematic break is a chunk of its own.
+ expect(contents('- item\n---\n')).toEqual(['- item', '---']);
+ expect(contents('> quote\n***\nAfter\n')).toEqual([
+ '> quote',
+ '***',
+ 'After',
+ ]);
+ expect(contents('- a\n- - -\n- b\n')).toEqual(['- a', '- - -', '- b']);
+ expect(contents('Para\n___\n')).toEqual(['Para', '___']);
+ // A dash-only line under paragraph lines underlines them instead, and a
+ // rule inside fenced code is code.
+ expect(contents('Title\nmore\n---\n')).toEqual(['Title\nmore\n---']);
+ expect(contents('```\n---\n```\n')).toEqual(['```\n---\n```']);
+ // Chunks still join back to the input exactly.
+ const crlf = '- item\r\n---\r\nAfter\r\n';
+ expect(
+ splitMarkdownChunks(crlf)
+ .map(chunk => chunk.leading + chunk.content + chunk.trailing)
+ .join(''),
+ ).toBe(crlf);
+ // A rule as written keeps the line break after it, whatever follows;
+ // the block above a `***` or `___` rule keeps its line break even when it
+ // changes, but above `---`, which could underline a paragraph as a
+ // heading, a changed block is set apart by a blank line.
+ const edit = (markdown: string, needles: ReadonlyArray<string>) =>
+ editAndExport(markdown, () => {
+ for (const needle of needles) {
+ $appendToTextContaining(needle, '!');
+ }
+ });
+ expect(edit('- item\n* * *\nAfter\n', ['After'])).toBe(
+ '- item\n* * *\nAfter!\n',
+ );
+ expect(edit('- item\n* * *\nAfter\n', ['item'])).toBe(
+ '- item!\n* * *\nAfter\n',
+ );
+ expect(edit('- item\n* * *\nAfter\n', ['item', 'After'])).toBe(
+ '- item!\n* * *\nAfter!\n',
+ );
+ expect(edit('Para\n___\nAfter\n', ['Para'])).toBe('Para!\n___\nAfter\n');
+ expect(edit('- item\n---\nAfter\n', ['item', 'After'])).toBe(
+ '- item!\n\n---\nAfter!\n',
+ );
+ // Blank lines stay blank lines.
+ expect(edit('- item\n\n* * *\n\nAfter\n', ['item'])).toBe(
+ '- item!\n\n* * *\n\nAfter\n',
+ );
+ // CRLF is kept as written.
+ expect(edit('- item\r\n* * *\r\nAfter\r\n', ['After'])).toBe(
+ '- item\r\n* * *\r\nAfter!\r\n',
+ );
+ // Consecutive rules, untouched, are byte for byte.
+ expect(edit('---\n***\n___\nText\n', ['Text'])).toBe(
+ '---\n***\n___\nText!\n',
+ );
+ // A block inserted after a rule follows its line break; a rule moved to
+ // the end is set apart from what now comes before it.
+ expect(
+ editAndExport('- item\n* * *\nAfter\n', () => {
+ const rule = $getRoot()
+ .getChildren()
+ .find(node => node.getType() === 'horizontalrule');
+ const inserted = $createParagraphNode().append($createTextNode('New'));
+ rule?.insertAfter(inserted);
+ }),
+ ).toBe('- item\n* * *\nNew\n\nAfter\n');
+ expect(
+ editAndExport('- item\n* * *\nAfter\n', () => {
+ const rule = $getRoot()
+ .getChildren()
+ .find(node => node.getType() === 'horizontalrule');
+ if (rule != null) {
+ $getRoot().append(rule);
+ }
+ }),
+ ).toBe('- item\n\nAfter\n\n* * *\n');
+ // A paragraph added after a rule that ended the document is set apart.
+ expect(
+ editAndExport('Para\n\n* * *\n', () => {
+ $getRoot().append(
+ $createParagraphNode().append($createTextNode('More')),
+ );
+ }),
+ ).toBe('Para\n\n* * *\n\nMore\n');
+ // Deleting the rule never joins the blocks it separated.
+ expect(
+ editAndExport('- item\n---\nAfter\n', () => {
+ $getRoot()
+ .getChildren()
+ .find(node => node.getType() === 'horizontalrule')
+ ?.remove();
+ }),
+ ).toBe('- item\n\nAfter\n');
+ });
+
it('imports the same structure as importing the whole document at once', () => {
// Node state aside, chunked import must build exactly the tree Lexical's
// own import builds from the list-normalized document, soft breaks
diff --git a/packages/richtext/src/markdownSource.ts b/packages/richtext/src/markdownSource.ts
index eb361c3662..2964f59111 100644
--- a/packages/richtext/src/markdownSource.ts
+++ b/packages/richtext/src/markdownSource.ts
@@ -77,6 +77,11 @@ interface GroupRecord {
readonly canonicalHash?: string;
/** The line ending the group's source uses. */
readonly lineEnding?: string;
+ /**
+ * The group right after this one in the source, recorded only when no
+ * blank line separates them (a thematic break written under a block).
+ */
+ readonly next?: string;
}
/**
@@ -90,6 +95,7 @@ interface SerializedGroupRecord {
t?: string;
h?: string;
e?: string;
+ x?: string;
}
/** Facts about the whole imported document, kept on the root. */
@@ -122,6 +128,7 @@ function parseGroupRecord(value: unknown): GroupRecord | null {
trailing: typeof serialized.t === 'string' ? serialized.t : '\n\n',
canonicalHash: typeof serialized.h === 'string' ? serialized.h : '',
lineEnding: typeof serialized.e === 'string' ? serialized.e : '\n',
+ ...(typeof serialized.x === 'string' ? {next: serialized.x} : {}),
};
}
@@ -141,6 +148,9 @@ function unparseGroupRecord(
serialized.t = record.trailing;
}
serialized.h = record.canonicalHash;
+ if (record.next != null) {
+ serialized.x = record.next;
+ }
if (record.lineEnding !== '\n') {
serialized.e = record.lineEnding;
}
@@ -168,11 +178,21 @@ const documentState = createState('astryxMdDocument', {
const BLANK_LINE = /^[ \t]*\r?$/;
const FENCE_OPEN = /^ {0,3}(`{3,}|~{3,})/;
+// A thematic break line (CommonMark 0.31), and the dash-only lines that
+// underline a paragraph line above them as a heading instead.
+const THEMATIC_BREAK_LINE = /^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*\r?$/;
+const SETEXT_DASHES = /^ {0,3}-+[ \t]*\r?$/;
+// Lines that start a block other than a paragraph.
+const BLOCK_START =
+ /^ {0,3}(?:[-*+][ \t]|\d{1,9}[.)][ \t]|>|#{1,6}(?:[ \t]|\r?$)|\|)/;
/**
* Splits Markdown into chunks: runs of non-blank lines, with a fenced code
- * block kept whole across blank lines. Joining every chunk's leading, content,
- * and trailing text gives back the input exactly.
+ * block kept whole across blank lines, and a thematic break always a chunk of
+ * its own, so editing the block beside a rule never rewrites the rule
+ * (spec:AST-062 FR2). A dash-only line under paragraph lines underlines them
+ * instead, and stays with them. Joining every chunk's leading, content, and
+ * trailing text gives back the input exactly.
*/
export function splitMarkdownChunks(markdown: string): Array<MarkdownChunk> {
const lines = markdown.split('\n');
@@ -191,6 +211,8 @@ export function splitMarkdownChunks(markdown: string): Array<MarkdownChunk> {
while (index < lines.length) {
const start = index;
let fence: string | null = null;
+ // Whether every line so far is paragraph text.
+ let isParagraph = true;
while (index < lines.length) {
const line = lines[index];
if (fence != null) {
@@ -206,11 +228,25 @@ export function splitMarkdownChunks(markdown: string): Array<MarkdownChunk> {
if (BLANK_LINE.test(line)) {
break;
}
+ const isRule =
+ THEMATIC_BREAK_LINE.test(line) &&
+ !(isParagraph && index > start && SETEXT_DASHES.test(line));
+ if (isRule && index > start) {
+ // The rule starts a chunk of its own.
+ break;
+ }
const open = FENCE_OPEN.exec(line);
if (open != null) {
fence = open[1];
}
+ if (open != null || BLOCK_START.test(line)) {
+ isParagraph = false;
+ }
index++;
+ if (isRule) {
+ // And ends it.
+ break;
+ }
}
let content = lines.slice(start, index).join('\n');
let trailing = '';
@@ -478,6 +514,9 @@ function $recordGroups(
content + (covered[covered.length - 1]?.trailing ?? ''),
'\n',
),
+ ...(!isLastGroup && !ENDS_WITH_BLANK_LINE.test(trailing)
+ ? {next: `${importId}:${nextChunk}`}
+ : {}),
};
group.forEach((node, position) => {
$setState(
@@ -660,6 +699,11 @@ interface ExportPiece {
readonly text: string;
/** Recorded bytes after the piece, when they belong to this document. */
readonly trailing: string | null;
+ /** Whether the piece is the group's recorded source, unchanged. */
+ readonly isAsWritten: boolean;
+ /** The piece's group, and the group that followed it in the source. */
+ readonly group: string | null;
+ readonly next: string | null;
}
const ENDS_WITH_BLANK_LINE = /\n[ \t]*\r?\n[ \t\r]*$/;
@@ -669,6 +713,47 @@ const ENDS_WITH_BLANK_LINE = /\n[ \t]*\r?\n[ \t\r]*$/;
* new blocks in canonical form, all in the document's envelope. Reads the tree
* without changing it, so it runs in a read of the live editor state.
*/
+/** Whether `text` is one thematic break line, as written. */
+function isRuleLine(text: string): boolean {
+ return !text.includes('\n') && THEMATIC_BREAK_LINE.test(text);
+}
+
+/**
+ * Whether the bytes recorded after `piece` still separate it from `next`.
+ * Bytes ending in a blank line separate any two blocks. A single line break
+ * only follows or precedes a thematic break (a rule is a chunk of its own),
+ * and it belongs to the block it follows:
+ *
+ * - after a rule as written, it stays whatever comes next, because no line
+ * after a rule can join it;
+ * - between the same two blocks, both as written, it stays;
+ * - after a changed block, it stays only above the same rule as written when
+ * that rule cannot underline the block as a heading (`***`, `___`, or
+ * spaced dashes, not `---`).
+ *
+ * Everywhere else — next to a moved, inserted, deleted, or changed block — a
+ * blank line separates the two, so no block can join its neighbor.
+ */
+function keepsRecordedTrailing(piece: ExportPiece, next: ExportPiece): boolean {
+ // The last block's recorded trailing is empty: it separates nothing.
+ if (piece.trailing == null || !piece.trailing.includes('\n')) {
+ return false;
+ }
+ if (ENDS_WITH_BLANK_LINE.test(piece.trailing)) {
+ return true;
+ }
+ if (piece.isAsWritten && isRuleLine(piece.text)) {
+ return true;
+ }
+ if (piece.next == null || piece.next !== next.group || !next.isAsWritten) {
+ return false;
+ }
+ return (
+ piece.isAsWritten ||
+ (isRuleLine(next.text) && !SETEXT_DASHES.test(next.text))
+ );
+}
+
export function $exportMarkdownKeepingSource(
transformers: ReadonlyArray<Transformer>,
): string {
@@ -710,6 +795,9 @@ export function $exportMarkdownKeepingSource(
? record.content
: withLineEnding(record.content, lineEnding),
trailing: isOwn ? (record.trailing ?? null) : null,
+ isAsWritten: isOwn,
+ group: record.group,
+ next: record.next ?? null,
});
continue;
}
@@ -724,6 +812,9 @@ export function $exportMarkdownKeepingSource(
isOwn && record?.lineEnding != null ? record.lineEnding : lineEnding,
),
trailing: isOwn ? (record?.trailing ?? null) : null,
+ isAsWritten: false,
+ group: record?.group ?? null,
+ next: isOwn ? (record?.next ?? null) : null,
});
}
const separator = lineEnding + lineEnding;
@@ -733,10 +824,9 @@ export function $exportMarkdownKeepingSource(
pieces.forEach((piece, position) => {
output += piece.text;
if (position < pieces.length - 1) {
- output +=
- piece.trailing != null && ENDS_WITH_BLANK_LINE.test(piece.trailing)
- ? piece.trailing
- : separator;
+ output += keepsRecordedTrailing(piece, pieces[position + 1])
+ ? (piece.trailing ?? '')
+ : separator;
}
});
return output + (document?.trailing ?? '');