Skip to content

Commit c68dada

Browse files
committed
fix(editor): align shouldPreserveNewLines between import and export
Import () was using shouldPreserveNewLines=true but export () defaulted to false. This mismatch caused each line in a multi-line body to become a separate ParagraphNode on import, then export joined adjacent non-empty paragraphs with \n\n, turning every \n into \n\n (e.g. aaa\nbbb\nccc → aaa\n\nbbb\n\nccc). Fix: pass shouldPreserveNewLines=true to export too, so ParagraphNodes are joined with just \n — consistent behavior in both directions. Tests added: - Multi-line body round-trips without phantom blank lines - Leading blank lines are preserved through round-trip - Intentional paragraph breaks (empty lines) are preserved
1 parent 8609842 commit c68dada

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

src/components/molecules/MarkdownEditor/MarkdownEditor.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,9 @@ export const MarkdownEditor = forwardRef<HTMLDivElement, MarkdownEditorProps>(
290290
) {
291291
const handleChange = useCallback(
292292
(editorState: EditorState) => {
293-
onChange(editorState.read(() => $convertToMarkdownString(MARKDOWN_TRANSFORMERS)));
293+
onChange(
294+
editorState.read(() => $convertToMarkdownString(MARKDOWN_TRANSFORMERS, undefined, true)),
295+
);
294296
},
295297
[onChange],
296298
);

src/components/molecules/MarkdownEditor/__tests__/utils.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ export function $setMarkdown(editor: LexicalEditor, markdown: string): void {
4848
}
4949

5050
export function $readMarkdown(editor: LexicalEditor): string {
51-
return editor.getEditorState().read(() => $convertToMarkdownString(MARKDOWN_TRANSFORMERS));
51+
return editor
52+
.getEditorState()
53+
.read(() => $convertToMarkdownString(MARKDOWN_TRANSFORMERS, undefined, true));
5254
}
5355

5456
type RenderTestEditorOptions = {

src/components/molecules/MarkdownEditor/transformers.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,22 @@ describe("MARKDOWN_TRANSFORMERS round-trip", () => {
1818
$setMarkdown(editor, "- one\n- two");
1919
expect($readMarkdown(editor)).toBe("- one\n- two");
2020
});
21+
22+
test("a multi-line body round-trips without phantom blank lines", () => {
23+
const editor = createTestHeadlessEditor();
24+
$setMarkdown(editor, "aaa\nbbb\nccc");
25+
expect($readMarkdown(editor)).toBe("aaa\nbbb\nccc");
26+
});
27+
28+
test("leading blank lines are preserved through round-trip", () => {
29+
const editor = createTestHeadlessEditor();
30+
$setMarkdown(editor, "\n\nbody");
31+
expect($readMarkdown(editor)).toBe("\n\nbody");
32+
});
33+
34+
test("intentional paragraph breaks (empty lines) are preserved", () => {
35+
const editor = createTestHeadlessEditor();
36+
$setMarkdown(editor, "aaa\n\nbbb");
37+
expect($readMarkdown(editor)).toBe("aaa\n\nbbb");
38+
});
2139
});

0 commit comments

Comments
 (0)