Skip to main content

Rich Text to HTML

There are two main approaches:
  1. Generate HTML on demand where it is consumed. This avoids duplicate storage and is recommended.
  2. Add an HTML field whose read hook converts a sibling Rich Text field. This adds API work and may duplicate content.
Use the synchronous converter when records and files are already populated:
Only render trusted content with dangerouslySetInnerHTML. Sanitize untrusted HTML according to your application’s security requirements. Empty editor state, including a lone empty paragraph, converts to an empty wrapping div, or an empty string with disableContainer.

Dynamic population

The async converter can populate relationships and files while converting. In a server component, create a FrogBot instance and pass the asynchronous population function:
Pass an existing FrogBot request instead of frogbot when conversion must preserve its user, locale, and transaction context. Population skips access control unless you set overrideAccess: false.
convertLexicalToHTMLAsync accepts every convertLexicalToHTML option, with async converters, plus populate: getFrogBotPopulateFn options: Client code can use getRestPopulateFn from @frogbotai/richtext-lexical/client. It makes a request for each node that needs population, so server-side population is preferable for larger documents.
getRestPopulateFn sends a GET request to <apiURL>/<collection>/<id> with the browser’s cookies.

HTML field

lexicalHTMLField creates a code field whose afterRead hook converts a sibling Rich Text field to HTML.
The helper generates HTML when the record is read. By default, it removes the generated field before database writes, avoiding duplicate stored content. Set storeInDB: true to retain a caller-supplied value, but conversion still occurs on read rather than automatically during writes. The read hook populates file nodes using the read operation’s request, depth, draft, access, and hidden-field settings, and wraps the output in a div with the class frogbot-richtext.

Custom HTML field converters

Blocks to HTML

Supply block converters by slug when converting on demand:
Pass this converter function as converters when using an HTML conversion API whose node-type parameter matches NodeTypes.

HTML to Rich Text

convertHTMLToLexical needs a DOM implementation and the editor config that defines allowed nodes:
Install jsdom and its types in projects that run this conversion on the server.

Converting HTML with images

Image tags are not uploaded automatically. Upload each file first, then add its record ID and collection slug to the source HTML:
The required attributes are data-lexical-upload-id and data-lexical-upload-relation-to. For external images, download and upload each image, update its DOM element with these attributes, then convert the updated HTML. For bulk migration, parse the source with JSDOM, iterate over its img elements, upload each image with frogbot.create, and set both attributes before converting document.body.innerHTML. Handle download or upload failures explicitly so a partial migration is visible rather than silently losing images. If you already have file IDs, buildEditorState can construct a state directly:
Verify that each referenced file ID exists and that relationTo matches the upload collection slug. Server-side conversion can only reference saved files, not uploads that are still pending in the admin panel.

buildEditorState options

buildEditorState is exported from both the package root and @frogbotai/richtext-lexical/client. Pass the allowed node types as its type argument, such as buildEditorState<DefaultNodeTypes>(...), to type the result. With neither option, the result is an editor state with no content.