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

Improve markdown transformers #3886

Merged
merged 4 commits into from
Mar 21, 2023

Conversation

Godefroy
Copy link
Contributor

I need to import a markdown document with tables containing markdown in each cell.

Problem: $convertFromMarkdownString is currently not called inside of table cells and doesn't take a target node as a parameter, it uses root node only.

The TABLE transformer needs to be adapted, as well as createMarkdownImport (which is used by $convertFromMarkdownString) and createMarkdownExport.

This pull request adds:

  • Markdown import inside table cells
  • Markdown export inside table cells
  • Table row header import
  • Emoji import

You can test with this markdown example:

| Table | Col 1 | Col 2 |
|-------|-------|-------|
| Line 1 | *Content* | Multi\nlines |
| Line 2 | Emoji :smile: | `Code` |

@facebook-github-bot
Copy link
Contributor

Hi @Godefroy!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@vercel
Copy link

vercel bot commented Feb 11, 2023

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

Name Status Preview Comments Updated
lexical ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Mar 8, 2023 at 7:59PM (UTC)
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Mar 8, 2023 at 7:59PM (UTC)

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Copy link
Contributor

@acywatson acywatson left a comment

Choose a reason for hiding this comment

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

CI integrity is failing for some reason - looks like prettier

@Godefroy
Copy link
Contributor Author

Godefroy commented Mar 8, 2023

Thanks @acywatson for the code review! I adapted my code.

Also, I took the liberty to remove root.selectEnd(); from createMarkdownImport because I think it's not its job and it was causing the equivalent of an autoFocus when importing a markdown.

}

return output.join('\n');
},
regExp: TABLE_ROW_REG_EXP,
replace: (parentNode, _1, match) => {
// Header row
if (TABLE_ROW_DIVIDER_REG_EXP.test(match[0])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@zurfyx will this be a problem for the tables works you're currently doing? I doubt it, but wanted to check.

@@ -95,7 +95,7 @@ export default function EmojiPickerPlugin() {

useEffect(() => {
// @ts-ignore
import('./emoji-list.ts').then((file) => setEmojis(file.default));
import('../../utils/emoji-list.ts').then((file) => setEmojis(file.default));
Copy link
Contributor

Choose a reason for hiding this comment

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

thanks for this

@@ -41,16 +42,16 @@ type TextFormatTransformersIndex = Readonly<{

export function createMarkdownImport(
transformers: Array<Transformer>,
): (markdownString: string) => void {
): (markdownString: string, node?: ElementNode) => void {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm ok with this - maybe a nit to call it targetNode or something that indicates that it's the parent of whatever the import creates.

Separately, why shouldn't markdown work more like HTML, where you just get back some nodes and then insert them wherever you want?

@fantactuka

I'm ok with this change.

Copy link
Contributor

Choose a reason for hiding this comment

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

Separately, why shouldn't markdown work more like HTML, where you just get back some nodes and then insert them wherever you want?

Yep, should be changed to work that way

@fantactuka fantactuka merged commit 04fdcb2 into facebook:main Mar 21, 2023
This was referenced Mar 24, 2023
@vadimkantorov
Copy link

vadimkantorov commented Dec 13, 2023

Does Lexical/Playground support working with elements like this (Toggle elements mixed with Markdown) and importing/exporting them from/to markdown?

<details>
<summary>

# hello world

</summary>

## sec1
abc

## sec2
def

</details>

hello world

sec1

abc

sec2

def

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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants