Lexical Text Editor: What should I save to the database?
Asked Answered
I

3

9

I'm building a rich text editor for a question and answer site.

As far as I know Lexical lets us get output data to JSON, JSON string, HTML, Markdown and others.

Based on the recommended best practices, what data should I save to a MySQL or MongoDB database?

This is a new site.

Impanation answered 29/5, 2022 at 1:38 Comment(8)
The reason Lexical has options is because you have options. "Best practices" are best in different situations. That aside, please don't ask for recommendations (opinion-based question) on StackOverflow; take a moment to read How to Ask.Hauge
@Hauge I have no specific conditions. Thank you for reminding.Impanation
If I may suggest, you might consider JSON, as it's not very complicated to work with and is easy to convert back.Hauge
Thanks for the suggest. JSON (type string) or JSON (type JSON) on MySQL? dev.mysql.com/doc/refman/8.0/en/json.htmlImpanation
Either, I guess. Probably JSON string so it's easier to store in the DB?Hauge
@Hauge If we keep the lexical JSON Node, what if we switch to a different editor one day?Impanation
Then you would do HTML?Hauge
I was in same situation to consider which format is best for my case. I want to store in format that can re-render in editor for editing later. The JSON format cause large size when store in database. The HTML format is not good for editing later. Finally, I decided to store in JSON format in a separate table with compress table column.Treatise
B
2

I had a similar problem recently, and I think you should choose an output format that has a specification, i.e. HTML or markdown.

Because these formats can be read by other text parsers. If one day you don't want to use lexical, or your other components aren't able to use lexical, you can still easily use an off-the-shelf parser without having to reinvent the wheel.

So never use the JSON format, because its schema is not universal. As for using HTML or markdown, it depends on what your editor does.

If you're making a completely rich text editor, I think it's definitely better to store it as HTML, because storing it as markdown loses a lot of formats.

But if all you're making is a markdown editor, then storing as markdown would be better. After all ...... your goal is markdown isn't it? Storing as markdown ensures that no extra styles are added and there are more parsers to choose from.

Bimanous answered 22/6 at 14:13 Comment(0)
G
0

After all, json data is a string with a specific predefined structure, you can convert any json to string using JSON.stringify(), and revert it from syring to json format with JSON.parse()

Gavrilla answered 26/12, 2023 at 15:13 Comment(1)
How does this address the question?Antidote
I
-1

It looks like it is recommended by Lexical to store a JSON string for the Lexical editor state

// Handler to store content
const onSubmit = () => {
  await saveContent(JSON.stringify(editor.getEditorState()));
}

https://lexical.dev/docs/concepts/editor-state

Then you can serialize/deserialized as described here: https://lexical.dev/docs/concepts/serialization#lexical---json

Ipsambul answered 30/11, 2022 at 23:5 Comment(2)
"... recommended by Lexical to store a JSON ...", Is there a sentence in the documentation that states this? Can you provide the link.Impanation
@R.M.Reza, dgeorg said "It looks like", not "they say" nor "they recommend". It is, in fact, shown as an example in the Lexical official documentation. Just recommend modifying the sentence to prevent confusion. Hope you have a good time coding :)Husking

© 2022 - 2024 — McMap. All rights reserved.