From 3f7dca77ef1097808b50f388e54ab60cef913ee2 Mon Sep 17 00:00:00 2001 From: Sherry Date: Thu, 26 Sep 2024 11:51:16 +0800 Subject: [PATCH] [lexical-markdown] set shouldMergeAdjacentLines default to be false (#6660) --- .../src/MarkdownTransformers.ts | 2 +- .../__tests__/unit/LexicalMarkdown.test.ts | 89 ++++++++++++++++++- packages/lexical-markdown/src/index.ts | 2 +- .../__tests__/e2e/Markdown.spec.mjs | 11 ++- 4 files changed, 95 insertions(+), 9 deletions(-) diff --git a/packages/lexical-markdown/src/MarkdownTransformers.ts b/packages/lexical-markdown/src/MarkdownTransformers.ts index bb83ad0af10..b724b3ba0ca 100644 --- a/packages/lexical-markdown/src/MarkdownTransformers.ts +++ b/packages/lexical-markdown/src/MarkdownTransformers.ts @@ -550,7 +550,7 @@ export const LINK: TextMatchTransformer = { export function normalizeMarkdown( input: string, - shouldMergeAdjacentLines = true, + shouldMergeAdjacentLines = false, ): string { const lines = input.split('\n'); let inCodeBlock = false; diff --git a/packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts b/packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts index c7c9874bb2f..d4c1a90148a 100644 --- a/packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts +++ b/packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts @@ -100,6 +100,7 @@ describe('Markdown', () => { // Multiline paragraphs: https://spec.commonmark.org/dingus/?text=Hello%0Aworld%0A! html: '

Helloworld!

', md: ['Hello', 'world', '!'].join('\n'), + shouldMergeAdjacentLines: true, skipExport: true, }, { @@ -125,6 +126,7 @@ describe('Markdown', () => { // Multiline list items: https://spec.commonmark.org/dingus/?text=-%20Hello%0A-%20world%0A!%0A! html: '', md: '- Hello\n- world\n!\n!', + shouldMergeAdjacentLines: true, skipExport: true, }, { @@ -314,6 +316,7 @@ describe('Markdown', () => { // https://spec.commonmark.org/dingus/?text=%3E%20Hello%0Aworld%0A! html: '
Helloworld!
', md: '> Hello\nworld\n!', + shouldMergeAdjacentLines: true, skipExport: true, }, { @@ -332,11 +335,13 @@ describe('Markdown', () => { customTransformers: [MDX_HTML_TRANSFORMER], html: '

Some HTML in mdx:

From HTML: Some Text
', md: 'Some HTML in mdx:\n\nSome Text', + shouldMergeAdjacentLines: true, }, { customTransformers: [MDX_HTML_TRANSFORMER], html: '

Some HTML in mdx:

From HTML: Line 1Some Text
', md: 'Some HTML in mdx:\n\nLine 1\nSome Text', + shouldMergeAdjacentLines: true, skipExport: true, }, ]; @@ -448,7 +453,7 @@ describe('Markdown', () => { } }); -describe('normalizeMarkdown', () => { +describe('normalizeMarkdown - shouldMergeAdjacentLines = true', () => { it('should combine lines separated by a single \n unless they are in a codeblock', () => { const markdown = ` A1 @@ -482,7 +487,7 @@ E2 E3 `; - expect(normalizeMarkdown(markdown)).toBe(` + expect(normalizeMarkdown(markdown, true)).toBe(` A1A2 A3 @@ -519,6 +524,84 @@ E3 | --- | --- | | c | d | `; - expect(normalizeMarkdown(markdown)).toBe(markdown); + expect(normalizeMarkdown(markdown, true)).toBe(markdown); + }); +}); + +describe('normalizeMarkdown - shouldMergeAdjacentLines = false', () => { + it('should not combine lines separated by a single \n', () => { + const markdown = ` +A1 +A2 + +A3 + +\`\`\`md +B1 +B2 + +B3 +\`\`\` + +C1 +C2 + +C3 + +\`\`\`js +D1 +D2 + +D3 +\`\`\` + +\`\`\`single line code\`\`\` + +E1 +E2 + +E3 +`; + expect(normalizeMarkdown(markdown, false)).toBe(` +A1 +A2 + +A3 + +\`\`\`md +B1 +B2 + +B3 +\`\`\` + +C1 +C2 + +C3 + +\`\`\`js +D1 +D2 + +D3 +\`\`\` + +\`\`\`single line code\`\`\` + +E1 +E2 + +E3 +`); + }); + + it('tables', () => { + const markdown = ` +| a | b | +| --- | --- | +| c | d | +`; + expect(normalizeMarkdown(markdown, false)).toBe(markdown); }); }); diff --git a/packages/lexical-markdown/src/index.ts b/packages/lexical-markdown/src/index.ts index 671b56e5e4e..06bd85dd492 100644 --- a/packages/lexical-markdown/src/index.ts +++ b/packages/lexical-markdown/src/index.ts @@ -85,7 +85,7 @@ function $convertFromMarkdownString( transformers: Array = TRANSFORMERS, node?: ElementNode, shouldPreserveNewLines = false, - shouldMergeAdjacentLines = true, + shouldMergeAdjacentLines = false, ): void { const sanitizedMarkdown = shouldPreserveNewLines ? markdown diff --git a/packages/lexical-playground/__tests__/e2e/Markdown.spec.mjs b/packages/lexical-playground/__tests__/e2e/Markdown.spec.mjs index 505b038ce28..b434d62be55 100644 --- a/packages/lexical-playground/__tests__/e2e/Markdown.spec.mjs +++ b/packages/lexical-playground/__tests__/e2e/Markdown.spec.mjs @@ -1310,6 +1310,7 @@ const IMPORTED_MARKDOWN_HTML = html` bold italic strikethrough text, +
@@ -1407,7 +1408,9 @@ const IMPORTED_MARKDOWN_HTML = html` dir="ltr"> Blockquotes text goes here
- And secondline after + And second +
+ line after
- - And can be nested and multiline as well - + And can be nested +
+ and multiline as well