最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Lexical Text Editor: What should I save to the database? - Stack Overflow

programmeradmin6浏览0评论

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 remended best practices, what data should I save to a MySQL or MongoDB database?

This is a new site.

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 remended best practices, what data should I save to a MySQL or MongoDB database?

This is a new site.

Share Improve this question asked May 29, 2022 at 1:38 R.M. RezaR.M. Reza 1,0091 gold badge13 silver badges26 bronze badges 8
  • 1 The reason Lexical has options is because you have options. "Best practices" are best in different situations. That aside, please don't ask for remendations (opinion-based question) on StackOverflow; take a moment to read How to Ask. – code Commented May 29, 2022 at 1:45
  • @code I have no specific conditions. Thank you for reminding. – R.M. Reza Commented May 29, 2022 at 1:51
  • If I may suggest, you might consider JSON, as it's not very plicated to work with and is easy to convert back. – code Commented May 29, 2022 at 3:50
  • Thanks for the suggest. JSON (type string) or JSON (type JSON) on MySQL? dev.mysql./doc/refman/8.0/en/json.html – R.M. Reza Commented May 29, 2022 at 4:06
  • 1 Either, I guess. Probably JSON string so it's easier to store in the DB? – code Commented May 29, 2022 at 19:11
 |  Show 3 more ments

3 Answers 3

Reset to default 3

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 ponents 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 pletely 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.

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()

It looks like it is remended 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

发布评论

评论列表(0)

  1. 暂无评论