lexical icon indicating copy to clipboard operation
lexical copied to clipboard

$convertToMarkdownString does not slashify

Open Mark-Buxbaum opened this issue 1 year ago • 2 comments

If the editor contains text that can be interpreted as markdown, $convertToMarkdownString assumes the value is the same. It should escape any markdown sequences with backslashes.

Lexical version: 0.15.0

Steps To Reproduce

  1. Put the text "**foo**" in your clipboard (using your favorite text editor).

  2. Go to the lexical playground and paste it. image

  3. Click the convert to markdown button. The markdown looks incorrect; it still has the value "**foo**". The proper value is "\*\*foo\*\*", with the backslashes. image

  4. Unclick the markdown button. The playground now shows "foo", adding boldface and removing asterisks. image

The current behavior

Markdown is incorrectly encoded with no backslashes.

The expected behavior

Markdown should be correctly encoded.

Impact of fix

Pasting into the editor should be lossless.

Mark-Buxbaum avatar Jul 24 '24 16:07 Mark-Buxbaum

My first attempt to fix: add this pass to the routine exportTextFormat() in MarkdownExport.ts:

  // Go through the text and if it matches an unslashified tag, add slashes
  for (const transformer of textTransformers) {
    const tagWithSlashes = transformer.tag.replaceAll(/(.)/g,"\\$1" )
    const regexp = new RegExp("(?<!\\\\)" + tagWithSlashes, "g");
    output = output.replaceAll(regexp, tagWithSlashes);
  }

The only downside is that I have to add the reverse transform to $convertFromMarkdownString.

Mark-Buxbaum avatar Jul 24 '24 20:07 Mark-Buxbaum

Related to https://github.com/facebook/lexical/issues/2715?

cduff avatar Nov 06 '24 04:11 cduff

Duplicate of #2715

etrepum avatar Dec 10 '24 18:12 etrepum