Skip to content

HTML to Lexical Content <img> tag conversion issue #7884

Open
@matteo-naif

Description

@matteo-naif

Link to reproduction

Payload Version

3.0.0-beta.90

Node Version

v20

Next.js Version

15.0.0-canary.121

Describe the Bug

When I convert HTML to Lexical the images are not converted.
There is a way to handle it?

Reproduction Steps

Code I'm using to convert HTML -> Lexical

import { createHeadlessEditor } from '@lexical/headless';
import { $generateNodesFromDOM } from '@lexical/html';
import { getEnabledNodes, sanitizeServerEditorConfig } from '@payloadcms/richtext-lexical';
import { JSDOM } from 'jsdom';
import { $getRoot, $getSelection } from 'lexical';
import { defaultEditorConfig } from '@payloadcms/richtext-lexical'
import configPromise from '@payload-config'

const convertHTMLToLexicalNodes = async (htmlString: string) => {

  const yourEditorConfig = defaultEditorConfig

  const headlessEditor = createHeadlessEditor({
    nodes: getEnabledNodes({
      editorConfig: await sanitizeServerEditorConfig(yourEditorConfig, await configPromise),
    }),
  })

  headlessEditor.update(() => {

    // In a headless environment you can use a package such as JSDom to parse the HTML string.
    const dom = new JSDOM(htmlString)

    // Once you have the DOM instance it's easy to generate LexicalNodes.
    const nodes = $generateNodesFromDOM(headlessEditor, dom.window.document)

    // Select the root
    $getRoot().select()

    // Insert them at a selection.
    const selection = $getSelection()
    selection?.insertNodes(nodes)

  }, { discrete: true })

  // Do this if you then want to get the editor JSON
  const editorJSON = headlessEditor.getEditorState().toJSON()

  // Clear Editor state
  headlessEditor.update(() => {
    const root = $getRoot();
    root.clear();
  }, { discrete: true });

  return editorJSON;
};

Original HTML

<p>Lorem Ipsum</p><img src="https://upload.wikimedia.org/wikipedia/commons/7/76/RMS_Republic.jpg">

Converted Lexical (JSON) without image

{
   "root":{
      "children":[
         {
            "children":[
               {
                  "detail":0,
                  "format":0,
                  "mode":"normal",
                  "style":"",
                  "text":"Lorem Ipsum",
                  "type":"text",
                  "version":1
               }
            ],
            "direction":null,
            "format":"",
            "indent":0,
            "type":"paragraph",
            "version":1,
            "textFormat":0,
            "textStyle":""
         }
      ],
      "direction":null,
      "format":"",
      "indent":0,
      "type":"root",
      "version":1
   }
}

Adapters and Plugins

"@payloadcms/db-mongodb": "beta",     "@payloadcms/email-nodemailer": "beta",     "@payloadcms/next": "beta",     "@payloadcms/plugin-cloud-storage": "beta",     "@payloadcms/plugin-form-builder": "beta",     "@payloadcms/plugin-nested-docs": "beta",     "@payloadcms/plugin-seo": "beta",     "@payloadcms/richtext-lexical": "beta",     "@payloadcms/storage-s3": "beta",     "@payloadcms/ui": "beta",

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions