diff --git a/packages/lexical-markdown/src/MarkdownExport.ts b/packages/lexical-markdown/src/MarkdownExport.ts index 066a212ae68..23402b35b8f 100644 --- a/packages/lexical-markdown/src/MarkdownExport.ts +++ b/packages/lexical-markdown/src/MarkdownExport.ts @@ -26,9 +26,10 @@ import {isEmptyParagraph, transformersByType} from './utils'; export function createMarkdownExport( transformers: Array, - isNewlineDelimited: boolean = true, + shouldPreserveNewLines: boolean = false, ): (node?: ElementNode) => string { const byType = transformersByType(transformers); + const isNewlineDelimited = !shouldPreserveNewLines; // Export only uses text formats that are responsible for single format // e.g. it will filter out *** (bold, italic) and instead use separate ** and * diff --git a/packages/lexical-markdown/src/MarkdownImport.ts b/packages/lexical-markdown/src/MarkdownImport.ts index 2cdaea613a7..a8e4d0f61aa 100644 --- a/packages/lexical-markdown/src/MarkdownImport.ts +++ b/packages/lexical-markdown/src/MarkdownImport.ts @@ -45,7 +45,7 @@ type TextFormatTransformersIndex = Readonly<{ export function createMarkdownImport( transformers: Array, - shouldIncludeBlankLines = false, + shouldPreserveNewLines = false, ): (markdownString: string, node?: ElementNode) => void { const byType = transformersByType(transformers); const textFormatTransformersIndex = createTextFormatTransformersIndex( @@ -86,7 +86,7 @@ export function createMarkdownImport( const children = root.getChildren(); for (const child of children) { if ( - !shouldIncludeBlankLines && + !shouldPreserveNewLines && isEmptyParagraph(child) && root.getChildrenSize() > 1 ) { diff --git a/packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts b/packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts index 0a70d6bc1c2..e5eb74bda6e 100644 --- a/packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts +++ b/packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts @@ -28,8 +28,7 @@ describe('Markdown', () => { md: string; skipExport?: true; skipImport?: true; - shouldIncludeBlankLines?: true; - isNewlineDelimited?: false; + shouldPreserveNewLines?: true; }>; const URL = 'https://lexical.dev'; @@ -152,13 +151,12 @@ describe('Markdown', () => { { html: '

Hello




world!

', md: '# Hello\n\n\n\n**world**!', - shouldIncludeBlankLines: true, + shouldPreserveNewLines: true, }, { html: '

Hello

hi


world


hi

hello
hello


hi


hi

', - isNewlineDelimited: false, md: '# Hello\nhi\n\n**world**\n\nhi\n> hello\n> hello\n\n# hi\n\nhi', - shouldIncludeBlankLines: true, + shouldPreserveNewLines: true, }, { // Import only: export will use * instead of _ due to registered transformers order @@ -238,7 +236,7 @@ describe('Markdown', () => { html, md, skipImport, - shouldIncludeBlankLines, + shouldPreserveNewLines, } of IMPORT_AND_EXPORT) { if (skipImport) { continue; @@ -262,7 +260,7 @@ describe('Markdown', () => { md, [...TRANSFORMERS, HIGHLIGHT_TEXT_MATCH_IMPORT], undefined, - shouldIncludeBlankLines, + shouldPreserveNewLines, ), { discrete: true, @@ -275,7 +273,12 @@ describe('Markdown', () => { }); } - for (const {html, md, skipExport, isNewlineDelimited} of IMPORT_AND_EXPORT) { + for (const { + html, + md, + skipExport, + shouldPreserveNewLines, + } of IMPORT_AND_EXPORT) { if (skipExport) { continue; } @@ -312,7 +315,7 @@ describe('Markdown', () => { $convertToMarkdownString( TRANSFORMERS, undefined, - isNewlineDelimited, + shouldPreserveNewLines, ), ), ).toBe(md); diff --git a/packages/lexical-markdown/src/index.ts b/packages/lexical-markdown/src/index.ts index a62bb28f648..24d19291cbd 100644 --- a/packages/lexical-markdown/src/index.ts +++ b/packages/lexical-markdown/src/index.ts @@ -72,11 +72,11 @@ function $convertFromMarkdownString( markdown: string, transformers: Array = TRANSFORMERS, node?: ElementNode, - shouldIncludeBlankLines = false, + shouldPreserveNewLines = false, ): void { const importMarkdown = createMarkdownImport( transformers, - shouldIncludeBlankLines, + shouldPreserveNewLines, ); return importMarkdown(markdown, node); } @@ -84,9 +84,12 @@ function $convertFromMarkdownString( function $convertToMarkdownString( transformers: Array = TRANSFORMERS, node?: ElementNode, - isNewlineDelimited: boolean = true, + shouldPreserveNewLines: boolean = false, ): string { - const exportMarkdown = createMarkdownExport(transformers, isNewlineDelimited); + const exportMarkdown = createMarkdownExport( + transformers, + shouldPreserveNewLines, + ); return exportMarkdown(node); } diff --git a/packages/lexical-playground/src/plugins/ActionsPlugin/index.tsx b/packages/lexical-playground/src/plugins/ActionsPlugin/index.tsx index 23ebbd485c1..2c710d6cc2d 100644 --- a/packages/lexical-playground/src/plugins/ActionsPlugin/index.tsx +++ b/packages/lexical-playground/src/plugins/ActionsPlugin/index.tsx @@ -174,13 +174,13 @@ export default function ActionsPlugin({ PLAYGROUND_TRANSFORMERS, undefined, // node // TODO: make a playground setting for this in lexical-playground/src/Settings.tsx - true, // include blank lines + true, // shouldPreserveNewLines ); } else { const markdown = $convertToMarkdownString( PLAYGROUND_TRANSFORMERS, undefined, //node - false, // isNewlineDelimited + true, // shouldPreserveNewLines ); root .clear()