diff --git a/packages/lexical-markdown/src/MarkdownExport.ts b/packages/lexical-markdown/src/MarkdownExport.ts index 59bc5e4ea64..e1f705a6b1f 100644 --- a/packages/lexical-markdown/src/MarkdownExport.ts +++ b/packages/lexical-markdown/src/MarkdownExport.ts @@ -203,13 +203,11 @@ function exportTextFormat( ): string { // This function handles the case of a string looking like this: " foo " // Where it would be invalid markdown to generate: "** foo **" - // If the node has no format, we use the original text. - // Otherwise, we escape leading and trailing whitespaces to their corresponding code points, - // ensuring the returned string maintains its original formatting, e.g., "** foo **". - let output = - node.getFormat() === 0 - ? textContent - : escapeLeadingAndTrailingWhitespaces(textContent); + // We instead want to trim the whitespace out, apply formatting, and then + // bring the whitespace back. So our returned string looks like this: " **foo** " + // However, we do not want to export any formatting if the string is just whitespace: " " + const frozenString = textContent.trim(); + let output = frozenString; if (!node.hasFormat('code')) { // Escape any markdown characters in the text content @@ -288,9 +286,9 @@ function exportTextFormat( break; } - output = openingTags + output + closingTagsAfter; + output = output ? openingTags + output + closingTagsAfter : output; // Replace trimmed version of textContent ensuring surrounding whitespace is not modified - return closingTagsBefore + output; + return closingTagsBefore + textContent.replace(frozenString, () => output); } // Get next or previous text sibling a text node, including cases @@ -345,9 +343,3 @@ function hasFormat( ): boolean { return $isTextNode(node) && node.hasFormat(format); } - -function escapeLeadingAndTrailingWhitespaces(textContent: string) { - return textContent.replace(/^\s+|\s+$/g, (match) => { - return [...match].map((char) => '&#' + char.codePointAt(0) + ';').join(''); - }); -} diff --git a/packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts b/packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts index 719c8bff246..8390ecc3289 100644 --- a/packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts +++ b/packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts @@ -408,17 +408,14 @@ describe('Markdown', () => { { html: '

Hello world!

', md: '**Hello ~~world~~**!', - mdAfterExport: '**Hello ~~world~~**!', }, { html: '

Hello world!

', md: '**~~Hello *world*~~**~~!~~', - mdAfterExport: '**~~Hello *world*~~**~~!~~', }, { html: '

Hello world!

', md: '*Hello **world**!*', - mdAfterExport: '*Hello **world**!*', }, { html: '

hello world

', @@ -603,26 +600,20 @@ describe('Markdown', () => { { html: '

Text boldstart text boldend text

', md: 'Text **boldstart [text](https://lexical.dev) boldend** text', - mdAfterExport: - 'Text **boldstart [text](https://lexical.dev) boldend** text', }, { html: '

Text boldstart text boldend text

', md: 'Text **boldstart [`text`](https://lexical.dev) boldend** text', - mdAfterExport: - 'Text **boldstart [`text`](https://lexical.dev) boldend** text', }, { html: '

It works with links too

', md: 'It ~~___works [with links](https://lexical.io)___~~ too', - mdAfterExport: - 'It ***~~works [with links](https://lexical.io)~~*** too', + mdAfterExport: 'It ***~~works [with links](https://lexical.io)~~*** too', }, { html: '

It works with links too!

', md: 'It ~~___works [with links](https://lexical.io) too___~~!', - mdAfterExport: - 'It ***~~works [with links](https://lexical.io) too~~***!', + mdAfterExport: 'It ***~~works [with links](https://lexical.io) too~~***!', }, { html: '

linklink2

', @@ -682,10 +673,6 @@ describe('Markdown', () => { html: '

*Hello* world

', md: '\\*Hello\\* world', }, - { - html: '

 

', - md: '** **', - }, { html: '

[h]elloh[e]llo

', md: '[[h]ello](https://lexical.dev)[h[e]llo](https://lexical.dev)', @@ -706,6 +693,11 @@ describe('Markdown', () => { html: '

[](https://lexical.dev)

', md: '[](https://lexical.dev)', }, + { + html: '

', + md: ' ', + skipImport: true, + }, ]; for (const { diff --git a/packages/lexical-markdown/src/importTextTransformers.ts b/packages/lexical-markdown/src/importTextTransformers.ts index b066fec5261..91f5d3147b5 100644 --- a/packages/lexical-markdown/src/importTextTransformers.ts +++ b/packages/lexical-markdown/src/importTextTransformers.ts @@ -135,10 +135,6 @@ export function importTextTransformers( // Handle escape characters const textContent = textNode.getTextContent(); - const escapedText = textContent - .replace(/\\([*_`~\\])/g, '$1') - .replace(/&#(\d+);/g, (_, codePoint) => { - return String.fromCodePoint(codePoint); - }); + const escapedText = textContent.replace(/\\([*_`~\\])/g, '$1'); textNode.setTextContent(escapedText); }