Skip to content
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

[lexical-markdown] Feature: add ability to control finding the end of a node matched by TextMatchTransformer #6681

Merged
merged 6 commits into from
Nov 5, 2024

Conversation

AlessioGr
Copy link
Contributor

@AlessioGr AlessioGr commented Sep 27, 2024

Description

Currently, for a TextMatchTransformer, the importRegExp is used to find a match in a given text. The end of the matched node is determined by the length of the match.

However, in some scenarios, you want to use importRegExp to only match the start of the node. You might then want to determine the end manually, for example via a different regex, or some custom logic.

Use-Case example

A good example is matching inline JSX, e.g. "Hello <InlineBlock prop1="hey">some text</InlineBlock> there!".

In this example, we use importRegExp solely to match <InlineBlock. We use a custom algorithm to then find the proper end of this node (</InlineBlock>). You might think this is possible solely with regex, but it's not.

Example that showcases why:

"Hello <InlineBlock prop1="hey">some <InlineBlock>Sub Block</InlineBlock> text</InlineBlock> there!"

We need custom logic that keeps track of the opening and closing JSX tags in order to find the correct end of the node: the second </InlineBlock>.

This PR adds a getEndIndex property to TextMatchTransformer that allows us to do these kinds of things, by providing our custom algorithm to find the end of the node (Example).

Copy link

vercel bot commented Sep 27, 2024

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 Nov 5, 2024 7:08am
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 5, 2024 7:08am

@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 Sep 27, 2024
Copy link

github-actions bot commented Sep 27, 2024

size-limit report 📦

Path Size
lexical - cjs 29.92 KB (0%)
lexical - esm 29.78 KB (0%)
@lexical/rich-text - cjs 38.57 KB (0%)
@lexical/rich-text - esm 31.63 KB (0%)
@lexical/plain-text - cjs 37.22 KB (0%)
@lexical/plain-text - esm 28.94 KB (0%)
@lexical/react - cjs 40.31 KB (0%)
@lexical/react - esm 33.04 KB (0%)

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.

Test coverage for this new functionality would be nice. It seems like you might also want to be able to let the transform fail if the end doesn't match correctly, maybe a more general solution (e.g. the option to use a function instead of regex for matching altogether) would be more appropriate? There's currently no "compilation" to merge regexes so there wouldn't be much of a disadvantage to having that sort of option.

@AlessioGr
Copy link
Contributor Author

Test coverage for this new functionality would be nice

Will add one after #6682 is merged!

It seems like you might also want to be able to let the transform fail if the end doesn't match correctly

Done! Can now return false as well

maybe a more general solution (e.g. the option to use a function instead of regex for matching altogether) would be more appropriate? There's currently no "compilation" to merge regexes so there wouldn't be much of a disadvantage to having that sort of option.

I think adding a general option would be a good idea - however, I don't always wanna have to handle all that code related to text splitting myself (this:

CleanShot 2024-10-14 at 12 04 37@2x)

Maybe it's better to add that in a separate PR in addition to getEndIndex?

etrepum
etrepum previously approved these changes Oct 16, 2024
@etrepum
Copy link
Collaborator

etrepum commented Oct 21, 2024

@AlessioGr were you planning to still add the test case here now that #6682 is merged?

@AlessioGr
Copy link
Contributor Author

Yep, I am! Will be done this week

@etrepum
Copy link
Collaborator

etrepum commented Nov 5, 2024

Just following up here, looks like we are still waiting on test cases but otherwise this is ready to go

@AlessioGr
Copy link
Contributor Author

@etrepum sorry for the delay - just added a test, so this should be ready now.

Pretty tricky finding a good, simple use-case for a test here, as the actual function we use to match JSX is huge. For this test, I just added a very basic text match converter where getEndIndex is used just for simple JSX opening/closing tag counting. Similar to the other CODE_TAG_COUNTER_EXAMPLE example, but simpler and for text match transformers

@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Nov 5, 2024
@etrepum etrepum added this pull request to the merge queue Nov 5, 2024
Merged via the queue into facebook:main with commit 2c1a8f1 Nov 5, 2024
40 of 43 checks passed
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.

3 participants