facebook / lexical

Lexical is an extensible text editor framework that provides excellent reliability, accessibility and performance.
https://lexical.dev
MIT License
20.03k stars 1.71k forks source link

Bug: Importing from markdown does not respect escaping #4844

Open robfig opened 1 year ago

robfig commented 1 year ago

Steps To Reproduce

  1. Enter \*Hello\* here to see that it is valid markdown which is rendered as expected: https://spec.commonmark.org/dingus/
  2. Go to lexical playground, clear the content, switch to Markdown mode. Enter \*Hello\*
  3. Click the markdown button to import.

Expected: It populates the WYSIWYG editor with the literal text *Hello*

Actual: It displays \Hello\

This is because it ignores the escaping and treats the surrounding asterisks as a format to be applied. The process of applying the text formats should do a negative lookbehind assertion to confirm that the previous character is not a backslash.

Before import image

After import image

robfig commented 1 year ago

Oh interesting, createTextFormatTransformersIndex does indeed add a negative lookbehind for escaping. I'm not sure why it's not working at this point.

robfig commented 1 year ago

OK, it's the switch here that adds the negative assertion depending on userAgent. It turns out my Jest test is classified as IS_APPLE_WEBKIT (user agent "Mozilla/5.0 (linux) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/20.0.3"), although I think it should support the regexp.

So maybe the fix is to change the condition under which it's not used?

    if (IS_SAFARI || IS_IOS || IS_APPLE_WEBKIT) {
      fullMatchRegExpByTag[tag] = new RegExp(
        `(${tagRegExp})(?![${tagRegExp}\\s])(.*?[^${tagRegExp}\\s])${tagRegExp}(?!${tagRegExp})`
      );
    } else {
      fullMatchRegExpByTag[tag] = new RegExp(
        `(?<![\\\\${tagRegExp}])(${tagRegExp})((\\\\${tagRegExp})?.*?[^${tagRegExp}\\s](\\\\${tagRegExp})?)((?<!\\\\)|(?<=\\\\\\\\))(${tagRegExp})(?![\\\\${tagRegExp}])`
      );
    }
ratasorin commented 1 year ago

Hello, I created a pull request that fixes this issue and #4808, you can see if it fits your requirements here: #4813

antsorin commented 9 months ago

Hi everyone.

Any update on this? Note that at the moment, neither import nor export handle the text content escaping, making the markdown support less reliable for production use.

Will @ratasorin's code be reviewed and hopefully merged soon?

cduff commented 3 weeks ago

Duplicate of https://github.com/facebook/lexical/issues/2715?