Repository navigation
Persisting Markdown and Styling with Lexical #7714
Replies: 1 comment
|
You can export Markdown with import { $convertToMarkdownString, TRANSFORMERS } from '@lexical/markdown';
const markdown = editor.getEditorState().read(() =>
$convertToMarkdownString(TRANSFORMERS),
);
// Store markdown as a text value in your backend.This serializes the current node tree; it doesn't recover the original Markdown source byte-for-byte. If preserving the exact input spelling/whitespace is a requirement, store that source separately. The Markdown helpers export the features covered by the supplied transformers. For alignment, font size, and other rich formatting, I would store Lexical JSON as the canonical editable document, with Markdown as an optional derived export: // Save a committed editor-state snapshot.
const jsonString = JSON.stringify(editor.getEditorState().toJSON());
// Restore into an editor with the same node types registered.
editor.setEditorState(editor.parseEditorState(jsonString));These are the documented JSON persistence APIs. JSON is still text and can be stored in a text or JSON database column. The distinction matters because ordinary Markdown has no standard font-size or paragraph-alignment syntax, and the built-in Markdown transformers don't encode those styles. Lexical's node JSON can: text nodes serialize their If Markdown must be your only storage format, define an extension/custom transformer and a matching renderer for the extra styling, or deliberately restrict the editor to Markdown-representable features. A custom syntax will require support in every consumer of that Markdown. I can describe the public serialization APIs, but can't establish how Meta's internal applications store their content. I checked the docs and node schemas; I haven't run your editor. |
Uh oh!
There was an error while loading. Please reload this page.
I'm currently migrating to Lexical for our Markdown editor and working through the documentation.
I have a couple of questions:
Can the raw Markdown text be retrieved from the editor state? I need to store it in our backend and serve it later.
How are styles like left alignment, font size, etc., persisted? Specifically, is there a standard way to represent these in Markdown (or an extended syntax) so that they can be stored as plain text in a database and rendered later with the correct styling?
I'm curious how Meta handles this internally — how Markdown content is stored and reconstructed on the frontend. Would someone be open to chatting? If so, feel free to grab a time here: calendly.com/zehuazhang.
Also, if you know of any maintainers or contributors who might be the right folks to speak with, could you tag them here? I’m happy to coordinate scheduling on my end.
All reactions