Skip to content

Bug: Markdown Shortcuts Transformer wrongly removes the matched node before the replace function runs #7563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rodrigomotion opened this issue May 22, 2025 · 2 comments · May be fixed by #7564
Labels

Comments

@rodrigomotion
Copy link

rodrigomotion commented May 22, 2025

When using the markdown shortcuts plugin with custom transformers, if a transformer's replace function returns false to cancel the transformation, the leading node of the matching node is still removed from the document. This happens because leadingNode.remove() is called before checking the return value of the replace function in this line

This seems unintended since it doesn't respect the documentation of the replace function: @return return false to cancel the transform, even though the regex matched. Lexical will then search for the next transformer.

Fixing this would enable writing transformers that do not replace the content even if they match, under certain conditions. For example preventing turning headers into numbered lists if you type 1. Header, like Notion and other popular editors do.

Lexical version: v0.31.2

Steps To Reproduce

  1. Create a markdown transformer that returns false from its replace function
  2. Type a markdown shortcut that would trigger the transformer
  3. The document is modified despite the transformation being cancelled

The current behaviour

The leading node gets removed even if the replace is cancelled.

The expected behavior

Returning false from the replace function should cancel the transform and not remove the nodes.

Impact of fix

This issue prevents applying markdown transformers conditionally. For example, if you don't want to transform headings into numbered lists which is a common use case.

Let me know what you think. Happy to put up a PR.

@etrepum
Copy link
Collaborator

etrepum commented May 22, 2025

Sounds like a bug, I would recommend changing it and seeing if it breaks any expectations in the e2e or unit tests and go from there

@rodrigomotion
Copy link
Author

Made a PR with the simplest solution, all unit and e2e tests pass, but also I think most if not all transformers in the playground never return false, I think it could be improved (for example splitText still runs, I don't know if it's a potential issue or not) but didn't want to move things around too much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants