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][lexical-playground] Feature: Option to include blanklines in markdown render #6020

Merged
merged 13 commits into from May 17, 2024

Conversation

potatowagon
Copy link
Contributor

@potatowagon potatowagon commented May 3, 2024

Description

Describe the changes in this pull request

option to make the number of blank lines consistent between markdown and the rendered markdown

Closes: #

To enable:

$convertFromMarkdownString(
  ...,
  true // shouldPreserveNewLines -> set to true,
)

$convertToMarkdownString(
   ...,
   true // shouldPreserveNewLines -> set to true,
);

These flags are optional. Defaults to old behavior of removing newlines on import and delimiting with newlines on export.

Test plan

Before

Screen.Recording.2024-05-03.at.10.16.38.PM.mov

blank lines are ignored and removed

After

Screen.Recording.2024-05-03.at.10.18.02.PM.mov

number of blank lines consistent between markdown and rendered markdown


Added settings in playground to toggle newline preservation

Screen.Recording.2024-05-17.at.1.44.36.AM.mov

Unit and e2e tests pass locally

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

vercel bot commented May 3, 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 May 17, 2024 0:56am
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 17, 2024 0:56am

Copy link

github-actions bot commented May 3, 2024

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
packages/lexical/dist/Lexical.js 23.93 KB (0%) 479 ms (0%) 175 ms (+1113.49% 🔺) 654 ms
packages/lexical-rich-text/dist/LexicalRichText.js 34.59 KB (0%) 692 ms (0%) 207 ms (+521% 🔺) 899 ms
packages/lexical-plain-text/dist/LexicalPlainText.js 34.51 KB (0%) 691 ms (0%) 195 ms (+490.96% 🔺) 885 ms

@fantactuka
Copy link
Contributor

LGTM, I believe there were some PRs in past with similar idea. More broader discussion (covered it a bit offline with Gerard & folks): it seems like best route for MD plugin is to eliminate out of the box import and export parsing and instead start relying on external library (e.g., remark), while plugin will only responsible to connect Lexical and 3rd party lib. That would allow delegating this kind of configuration to those libs.

Shortcuts is a bit more lightweight functionality (and handles simple formats only) so it could still remain a part of the plugin (that way we're not bloating editor with full MD parsing library for simple shortcuts).


export function createMarkdownExport(
transformers: Array<Transformer>,
isNewlineDelimited: boolean = true,
Copy link
Member

Choose a reason for hiding this comment

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

I believe the two are the same feature, do we need a different flag name for these?

I would probably call it preserveNewLines

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that makes sense to standardise the flag name for both import and export, will change!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed in 89a7b30

firstChild == null ||
(node.getChildrenSize() === 1 &&
$isTextNode(firstChild) &&
MARKDOWN_EMPTY_LINE_REG_EXP.test(firstChild.getTextContent()))
Copy link
Member

Choose a reason for hiding this comment

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

Is this intentional? I believe this regex is particularly important on top of the default Node isEmpty

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i moved this to packages/lexical-markdown/src/utils.ts for reuse

$isTextNode(firstChild) &&
MARKDOWN_EMPTY_LINE_REG_EXP.test(firstChild.getTextContent()))
);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zurfyx here it is

@GermanJablo
Copy link
Contributor

it seems like best route for MD plugin is to eliminate out of the box import and export parsing and instead start relying on external library (e.g., remark) @fantactuka

I'm not very familiar with Remark, but I have a hard time seeing how a generic library could correctly parse Lexical's editorState.

Separate topic, I do like this PR. In my app I use markdown for the tests, and to make the conversion idempotent I do something quite dirty: I add a specific string to the empty paragraphs, and after the conversion is done I delete them.

@potatowagon
Copy link
Contributor Author

potatowagon commented May 16, 2024

note: there is a change to blockquote behavior.

with shouldPreserveNewLines = false (old behavior)

hello

text

becomes

>hello

text

because of newline delimeter


with shouldPreserveNewLines = true (new optional behavior)

hello

text

^imagine no newlines seperating them

becomes

>hello
>text

tho this is consistent with other markdown surfaces such as github & dillinger where texts right below blockquotes get included in the blockquote

Screenshot 2024-05-17 at 2 17 58 AM

zurfyx
zurfyx previously approved these changes May 16, 2024
@@ -116,6 +128,7 @@ function exportChildren(
exportTextFormat(child, child.getTextContent(), textTransformersIndex),
);
} else if ($isElementNode(child)) {
// empty para returns ""
Copy link
Member

Choose a reason for hiding this comment

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

If you think this comment adds some value, let's write "paragraph" instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed in 6a42853


export function createMarkdownExport(
transformers: Array<Transformer>,
shouldPreserveNewLines: boolean = false,
Copy link
Member

Choose a reason for hiding this comment

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

Do you think the preserveNewLines comment we discussed applies here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i hope i understood u correctly, its been applied in 89a7b30 ,

isNewlineDelimited replaced with shouldPreserveNewLines,

preserveNewLines is named as shouldPreserveNewLines to be consistent with boolean naming conventions

@github-actions github-actions bot added the extended-tests Run extended e2e tests on a {R label May 16, 2024
Copy link
Contributor

@Sahejkm Sahejkm left a comment

Choose a reason for hiding this comment

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

LGTM

@potatowagon potatowagon added this pull request to the merge queue May 17, 2024
Merged via the queue into main with commit f02e163 May 17, 2024
52 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 {R
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants