API Reference - @liveblocks/node-lexical
@liveblocks/node-lexical
provides a Node.js package to export and modify
Lexical documents on the server.
withLexicalDocument
withLexicalDocument
is the main entry point to modifying a document on the
server. It takes a room ID and a
Liveblocks Node client,
and returns a callback used to work with Lexical documents stored in Liveblocks.
- returnsT
Returns the value you return from the
doc
callback.
- roomIdstringRequired
The ID of the room to use.
- clientLiveblocksRequired
The Liveblocks client to use.
- nodesKlass<LexicalNode>[] | LexicalNodeReplacement[]
Optional. The Lexical nodes used in the document. Will extend the default schema which uses Liveblocks mentions and Liveblocks comments.
Returning data
Get your editor’s text content by returning doc.getTextContent
inside the
callback.
Custom nodes
If your Lexical document has custom nodes, they must be passed into the
withLexicalDocument
, similarly to with a front end Lexical client.
Lexical document API
You can easily modify your document with the Lexical document API.
doc.update
Liveblocks provides doc.update
which is a callback function similar to
Lexical’s editor.update
. This makes it easy to use Lexical’s editor functions.
Any edits will be persisted and appear in realtime to connected users as soon as
the update
promise resolves. Unlike Lexical’s editor.update
, this change is
always discrete. The callback can also be an async
function.
- callback() => void
Callback function where you should handle your modifications.
Example usage
Here’s an example of some modifications to a Lexical document.
doc.getTextContent
Returns the text content from the root node as a string
.
- contentstring
Returns the text retrieved from the document.
doc.getEditorState
Returns Lexical’s editorState.
- editorStateEditorState
Your editor’s Lexical state.
doc.getLexicalEditor
Returns a headless Lexical editor. @lexical/headless.
- headlessEditorLexicalEditor
Your headless Lexical editor.
doc.toJSON
Returns a serialized JSON object representation of your document. See Lexical’s Serialization & Deserialization page for more information.
- docAsJsonSerializedEditorState<SerializedLexicalNode>
A serialized JSON object representation of your document.
doc.toMarkdown
Returns a markdown string
of your document. See Lexical’s
@lexical/markdown page for
more information.
- markdownstring
Returns the markdown string.