Core Development Concepts > Extending and Customizing
Lexical Tools
Learn how to use Lexical tools to manipulate Lexical state.
This feature is available since Webiny v5.38.0.
- how to parse HTML to Lexical state
- how to convert Lexical state to HTML
Overview
Webiny uses the Lexical text editor framework to power its Rich Text Editor components. It’s very powerful, and enables us to build a feature rich and extendable text editors. The downside is that the output of the editor is not HTML, as you would expect from a WYSIWYG editor.
To help you convert Lexical’s native state shape into HTML, and vice-versa, we’ve created a set of utilities, which reside in our @webiny/lexical-converter
package.
This article demonstrates how to use this utility package to convert your HTML or Lexical data into the desired form.
HTML to Lexical
To parse HTML to a valid Lexical state, Lexical editor requires a DOM document. Every environment is different, so there’s an extra step you need to take before you use our tool, and that is converting your HTML to a DOM document.
Browsers have a native DOMParser class you can just instantiate and parse your HTML.
Node.js is a bit more involved, because DOM is not a native thing for Node.js. You’ll need to use a library like JSDOM, to parse your HTML to a DOM document.
Browser Usage
In the browser environment, you can simply use the DOMParser
class.
Node.js Usage
In the Node.js environment, first install a DOM library of choice. We’ll use JSDOM:
Then use it to parse your HTML, and get a DOM document:
Convert DOM to Lexical State
Once you have your DOM document, you can convert it to Lexical state:
Custom Nodes
You can configure your Lexical parser with custom Lexical nodes. This will often be necessary if your HTML contains tags unknown to Webiny.
Custom Node Mapper
You might also need to perform some actions on your node instance, after the DOM is parsed into a Lexical state. To process each parsed node, configure the parser with a nodeMapper
property:
Lexical to HTML
To convert raw Lexical state to HTML, Lexical editor requires DOM document
to be present globally. In the browser, this means on the window
object (window.document
), and in Node.js, this means on the global
object (global.document
).
Browser Usage
In the browser environment, DOM is present by definition, so no extra steps are necessary.
Node.js Usage
In the Node.js environment, first install a DOM library of choice. We’ll use JSDOM:
Then use it to set up the environment:
To see a real-life example of this setup, have a look at our default Lexical to HTML implementation.
Once DOM objects are configured, you can render your Lexical state to HTML. Webiny provides a Lexical transformer utility, which allows to render to various output formats.
Render to an HTML String
To convert Lexical state to plain HTML string, use the toHtml
method. This will simply output everything as a string
.
Render to an Array of Objects
To convert Lexical state to an array of objects, where each object represents a top-level block element, use the flatten
method. Each block will be converted to a standalone HTML string, and you will also get an instance of the Lexical Node object along with the block HTML.
The output of this method will look something like this:
To see various use cases for these methods, have a look at the test file.