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] Fix: normalize markdown in $convertFromMarkdownString to comply with CommonMark spec #6608

Merged
merged 11 commits into from
Sep 10, 2024

Conversation

GermanJablo
Copy link
Contributor

@GermanJablo GermanJablo commented Sep 6, 2024

Problem

In markdown, the concept of "empty paragraphs" does not exist.
Blocks must be separated by an empty line, and non-empty adjacent lines must be merged.

Let's take this markdown as an example:

one
two

three

Currently, it is converted to the following (incorrect):

<p>one<br>two</p>
<p>three</p>

When in fact, the correct output should be this (proof):

<p>onetwo</p>
<p>three</p>

For context, we are trying to import real mdx files into Lexical, and because of this issue, the current output contains some errors, such as this one here.

Solution

At first I thought it would be enough to remove all single line breaks (\n), which were not accompanied by further consecutive line breaks (\n\n).

However, the solution was not that easy, as there were some tricky edgecases. To mention just a couple of examples:

  • within a code block, no line breaks should be removed (ref).
  • consecutive heading + paragraph should not be combined, but list-item + "paragraph" should (ref).

That's why I wrote a function called sanitizeMarkdown to cover these and all the other cases I found, and which is now run inside $convertFromMarkdownString.

A few tests required changes. Since the correct result was not obvious, I left some comments linking to permalinks in the CommonMark playground.

Future work

If one would like to add a hard line break, there are 3 ways to do so in markdown (source):

  1. an html <br> tag,
  2. ending the line with two spaces,
  3. or ending the line with \.

Right now, the only option that works with Lexical is the first one. I've left a TO-DO comment indicating that it would be nice to support the other 2 in the future.

Test plan

Before

Screen.Recording.2024-09-08.at.12.45.19.AM.mov

After

Screen.Recording.2024-09-08.at.12.43.14.AM.mov

Copy link

vercel bot commented Sep 6, 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 Sep 9, 2024 1:31pm
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 9, 2024 1:31pm

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

github-actions bot commented Sep 6, 2024

size-limit report 📦

Path Size
lexical - cjs 29.61 KB (0%)
lexical - esm 29.49 KB (0%)
@lexical/rich-text - cjs 38.07 KB (0%)
@lexical/rich-text - esm 31.3 KB (0%)
@lexical/plain-text - cjs 36.72 KB (0%)
@lexical/plain-text - esm 28.65 KB (0%)
@lexical/react - cjs 39.89 KB (0%)
@lexical/react - esm 32.79 KB (0%)

@GermanJablo GermanJablo changed the title [lexical-markdown] Fix: markdown transformer for links separated by newline [lexical-markdown] Fix: sanitize markdown in $convertFromMarkdownString to comply with CommonMark spec Sep 8, 2024
@GermanJablo GermanJablo marked this pull request as ready for review September 8, 2024 03:54
etrepum
etrepum previously approved these changes Sep 8, 2024
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.

Approving because it does seem like an incremental improvement but I think there's still other classes of edge cases in here

packages/lexical-markdown/src/MarkdownTransformers.ts Outdated Show resolved Hide resolved
packages/lexical-markdown/src/MarkdownTransformers.ts Outdated Show resolved Hide resolved
@GermanJablo
Copy link
Contributor Author

GermanJablo commented Sep 9, 2024

Thanks for your comments @etrepum, I already solved them

@GermanJablo GermanJablo changed the title [lexical-markdown] Fix: sanitize markdown in $convertFromMarkdownString to comply with CommonMark spec [lexical-markdown] Fix: normalize markdown in $convertFromMarkdownString to comply with CommonMark spec Sep 9, 2024
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.

The lack of spec compliance makes it hard to review thoroughly since it's easy to find things that don't work correctly, but it does pass our tests and I think this particular change is unlikely to cause new problems.

@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Sep 9, 2024
@potatowagon
Copy link
Contributor

The lack of spec compliance makes it hard to review thoroughly since it's easy to find things that don't work correctly, but it does pass our tests and I think this particular change is unlikely to cause new problems.

thankyou for your review

@potatowagon potatowagon added this pull request to the merge queue Sep 10, 2024
Merged via the queue into facebook:main with commit 8123ca7 Sep 10, 2024
79 checks passed
@GermanJablo GermanJablo deleted the code-md branch September 10, 2024 14:48
potatowagon added a commit that referenced this pull request Sep 12, 2024
…kdownString to comply with CommonMark spec (#6608)"

This reverts commit 8123ca7.
github-merge-queue bot pushed a commit that referenced this pull request Sep 12, 2024
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.

4 participants