Skip to content

Commit 3f7dca7

Browse files
authored
[lexical-markdown] set shouldMergeAdjacentLines default to be false (#6660)
1 parent 2889ccd commit 3f7dca7

File tree

4 files changed

+95
-9
lines changed

4 files changed

+95
-9
lines changed

packages/lexical-markdown/src/MarkdownTransformers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ export const LINK: TextMatchTransformer = {
550550

551551
export function normalizeMarkdown(
552552
input: string,
553-
shouldMergeAdjacentLines = true,
553+
shouldMergeAdjacentLines = false,
554554
): string {
555555
const lines = input.split('\n');
556556
let inCodeBlock = false;

packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts

+86-3
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ describe('Markdown', () => {
100100
// Multiline paragraphs: https://spec.commonmark.org/dingus/?text=Hello%0Aworld%0A!
101101
html: '<p><span style="white-space: pre-wrap;">Helloworld!</span></p>',
102102
md: ['Hello', 'world', '!'].join('\n'),
103+
shouldMergeAdjacentLines: true,
103104
skipExport: true,
104105
},
105106
{
@@ -125,6 +126,7 @@ describe('Markdown', () => {
125126
// Multiline list items: https://spec.commonmark.org/dingus/?text=-%20Hello%0A-%20world%0A!%0A!
126127
html: '<ul><li value="1"><span style="white-space: pre-wrap;">Hello</span></li><li value="2"><span style="white-space: pre-wrap;">world!!</span></li></ul>',
127128
md: '- Hello\n- world\n!\n!',
129+
shouldMergeAdjacentLines: true,
128130
skipExport: true,
129131
},
130132
{
@@ -314,6 +316,7 @@ describe('Markdown', () => {
314316
// https://spec.commonmark.org/dingus/?text=%3E%20Hello%0Aworld%0A!
315317
html: '<blockquote><span style="white-space: pre-wrap;">Helloworld!</span></blockquote>',
316318
md: '> Hello\nworld\n!',
319+
shouldMergeAdjacentLines: true,
317320
skipExport: true,
318321
},
319322
{
@@ -332,11 +335,13 @@ describe('Markdown', () => {
332335
customTransformers: [MDX_HTML_TRANSFORMER],
333336
html: '<p><span style="white-space: pre-wrap;">Some HTML in mdx:</span></p><pre spellcheck="false" data-language="MyComponent"><span style="white-space: pre-wrap;">From HTML: Some Text</span></pre>',
334337
md: 'Some HTML in mdx:\n\n<MyComponent>Some Text</MyComponent>',
338+
shouldMergeAdjacentLines: true,
335339
},
336340
{
337341
customTransformers: [MDX_HTML_TRANSFORMER],
338342
html: '<p><span style="white-space: pre-wrap;">Some HTML in mdx:</span></p><pre spellcheck="false" data-language="MyComponent"><span style="white-space: pre-wrap;">From HTML: Line 1Some Text</span></pre>',
339343
md: 'Some HTML in mdx:\n\n<MyComponent>Line 1\nSome Text</MyComponent>',
344+
shouldMergeAdjacentLines: true,
340345
skipExport: true,
341346
},
342347
];
@@ -448,7 +453,7 @@ describe('Markdown', () => {
448453
}
449454
});
450455

451-
describe('normalizeMarkdown', () => {
456+
describe('normalizeMarkdown - shouldMergeAdjacentLines = true', () => {
452457
it('should combine lines separated by a single \n unless they are in a codeblock', () => {
453458
const markdown = `
454459
A1
@@ -482,7 +487,7 @@ E2
482487
483488
E3
484489
`;
485-
expect(normalizeMarkdown(markdown)).toBe(`
490+
expect(normalizeMarkdown(markdown, true)).toBe(`
486491
A1A2
487492
488493
A3
@@ -519,6 +524,84 @@ E3
519524
| --- | --- |
520525
| c | d |
521526
`;
522-
expect(normalizeMarkdown(markdown)).toBe(markdown);
527+
expect(normalizeMarkdown(markdown, true)).toBe(markdown);
528+
});
529+
});
530+
531+
describe('normalizeMarkdown - shouldMergeAdjacentLines = false', () => {
532+
it('should not combine lines separated by a single \n', () => {
533+
const markdown = `
534+
A1
535+
A2
536+
537+
A3
538+
539+
\`\`\`md
540+
B1
541+
B2
542+
543+
B3
544+
\`\`\`
545+
546+
C1
547+
C2
548+
549+
C3
550+
551+
\`\`\`js
552+
D1
553+
D2
554+
555+
D3
556+
\`\`\`
557+
558+
\`\`\`single line code\`\`\`
559+
560+
E1
561+
E2
562+
563+
E3
564+
`;
565+
expect(normalizeMarkdown(markdown, false)).toBe(`
566+
A1
567+
A2
568+
569+
A3
570+
571+
\`\`\`md
572+
B1
573+
B2
574+
575+
B3
576+
\`\`\`
577+
578+
C1
579+
C2
580+
581+
C3
582+
583+
\`\`\`js
584+
D1
585+
D2
586+
587+
D3
588+
\`\`\`
589+
590+
\`\`\`single line code\`\`\`
591+
592+
E1
593+
E2
594+
595+
E3
596+
`);
597+
});
598+
599+
it('tables', () => {
600+
const markdown = `
601+
| a | b |
602+
| --- | --- |
603+
| c | d |
604+
`;
605+
expect(normalizeMarkdown(markdown, false)).toBe(markdown);
523606
});
524607
});

packages/lexical-markdown/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function $convertFromMarkdownString(
8585
transformers: Array<Transformer> = TRANSFORMERS,
8686
node?: ElementNode,
8787
shouldPreserveNewLines = false,
88-
shouldMergeAdjacentLines = true,
88+
shouldMergeAdjacentLines = false,
8989
): void {
9090
const sanitizedMarkdown = shouldPreserveNewLines
9191
? markdown

packages/lexical-playground/__tests__/e2e/Markdown.spec.mjs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,7 @@ const IMPORTED_MARKDOWN_HTML = html`
13101310
bold italic strikethrough
13111311
</strong>
13121312
<span data-lexical-text="true">text,</span>
1313+
<br />
13131314
<strong
13141315
class="PlaygroundEditorTheme__textBold PlaygroundEditorTheme__textItalic PlaygroundEditorTheme__textStrikethrough"
13151316
data-lexical-text="true">
@@ -1407,7 +1408,9 @@ const IMPORTED_MARKDOWN_HTML = html`
14071408
dir="ltr">
14081409
<span data-lexical-text="true">Blockquotes text goes here</span>
14091410
<br />
1410-
<span data-lexical-text="true">And secondline after</span>
1411+
<span data-lexical-text="true">And second</span>
1412+
<br />
1413+
<span data-lexical-text="true">line after</span>
14111414
</blockquote>
14121415
<blockquote
14131416
class="PlaygroundEditorTheme__quote PlaygroundEditorTheme__ltr"
@@ -1485,9 +1488,9 @@ const IMPORTED_MARKDOWN_HTML = html`
14851488
class="PlaygroundEditorTheme__listItem PlaygroundEditorTheme__ltr"
14861489
dir="ltr"
14871490
value="1">
1488-
<span data-lexical-text="true">
1489-
And can be nested and multiline as well
1490-
</span>
1491+
<span data-lexical-text="true">And can be nested</span>
1492+
<br />
1493+
<span data-lexical-text="true">and multiline as well</span>
14911494
</li>
14921495
</ol>
14931496
</li>

0 commit comments

Comments
 (0)