Skip to content

[lexical-markdown] Bug fix: Prevent transform from removing nodes if the replace function returns false #7564

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

Merged
merged 3 commits into from
May 25, 2025

Conversation

rodrigomotion
Copy link
Contributor

@rodrigomotion rodrigomotion commented May 23, 2025

Description

Currently if you return false from a Markdown transformer replace function, the expectation is that the transform will be cancelled, however the matched nodes are still being modified (removed) because the leading node gets removed before checking the return value of the replace function. This prevents implementing some functionality, such as not turning headers into numbered lists, and could cause other issues since when the next transform runs it won't have the leading node anymore.

Describe the changes in this pull request

I tried to keep it simple and just moved the remove function so that it runs only if the replace function does not return false. All tests pass.

Closes #7563

Test plan

Ran all the unit and e2e tests and they all pass.

Before and after video

https://share.cleanshot.com/JYWVlb4d

Copy link

vercel bot commented May 23, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 23, 2025 8:44pm
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 23, 2025 8:44pm

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 23, 2025
@rodrigomotion rodrigomotion changed the title [WIP][lexical-markdown] But fix: Prevent transform from removing nodes if the replace function returns false [WIP][lexical-markdown] Bug fix: Prevent transform from removing nodes if the replace function returns false May 23, 2025
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label May 23, 2025
@rodrigomotion
Copy link
Contributor Author

Seems like one of the test suites is timing out but it's unrelated to this change?

@etrepum
Copy link
Collaborator

etrepum commented May 23, 2025

Likely, I will restart it. Do you plan to write some additional tests or make any other changes before review? It is still marked WIP.

@rodrigomotion
Copy link
Contributor Author

rodrigomotion commented May 23, 2025

Likely, I will restart it. Do you plan to write some additional tests or make any other changes before review? It is still marked WIP.

I was trying to write a test, but I'm not sure how to set this up without changing some of the transformers, so I won't unless someone thinks it's required and can guide me on how to set a up a test for this. If the current tests pass I'll leave it as is.

@rodrigomotion rodrigomotion changed the title [WIP][lexical-markdown] Bug fix: Prevent transform from removing nodes if the replace function returns false [lexical-markdown] Bug fix: Prevent transform from removing nodes if the replace function returns false May 23, 2025
@etrepum
Copy link
Collaborator

etrepum commented May 23, 2025

You should be able to write a unit test which wouldn't require changing other code, there's at least one transformer defined in the unit tests for the markdown package to test something that can't be reproduced in the playground.

@rodrigomotion
Copy link
Contributor Author

You should be able to write a unit test which wouldn't require changing other code, there's at least one transformer defined in the unit tests for the markdown package to test something that can't be reproduced in the playground.

Ok I added a couple of tests, the first of which fails for the old behaviour. I added one for the normal header transforms to also avoid regressions with that and confirm my test was actually testing the right thing.

etrepum
etrepum previously approved these changes May 23, 2025
Copy link
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tentatively looks good, other than this typo in the tests. Will wait to see the result of the full test run.

@@ -213,6 +223,18 @@ const CODE_TAG_COUNTER_EXAMPLE: MultilineElementTransformer = {
type: 'multiline-element',
};

export const CENCELED_HEADING_REPLACE_EXAMPLE: ElementTransformer = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export const CENCELED_HEADING_REPLACE_EXAMPLE: ElementTransformer = {
export const CANCELED_HEADING_REPLACE_EXAMPLE: ElementTransformer = {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops, will fix that, thanks!

],
});

registerMarkdownShortcuts(editor, [CENCELED_HEADING_REPLACE_EXAMPLE]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
registerMarkdownShortcuts(editor, [CENCELED_HEADING_REPLACE_EXAMPLE]);
registerMarkdownShortcuts(editor, [CANCELED_HEADING_REPLACE_EXAMPLE]);

@etrepum etrepum added this pull request to the merge queue May 25, 2025
Merged via the queue into facebook:main with commit 2564dd4 May 25, 2025
39 checks passed
@facebook facebook deleted a comment from darikprescott May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Markdown Shortcuts Transformer wrongly removes the matched node before the replace function runs
3 participants