5.39.6 • Published 2 days ago

@webiny/lexical-converter v5.39.6

Weekly downloads
-
License
-
Repository
-
Last release
2 days ago

@webiny/lexical-converter

npm.io npm.io code style: prettier PRs Welcome

About

This package provides features that will enable you to parse your HTML pages into Lexical editor state object.

Further, this lexical state object can be imported into Webiny's apps like the Page builder and Headless CMS, trough the Webiny's graphql API.

Webiny use the Lexical editor as primary rich text editor across the platform.

Note: This module is built to be used in the node.js environment.

Usage

To parse the HTML to lexical editor state object, you need to import createHtmlToLexicalParser factory function, to create the parser function (with default or custom configuration) and provide the HTML content as parameter. Parser will return Lexical editor state object.

The parser uses the default configuration with the Webiny's Lexical nodes. DOM elements like headings and paragraph, for example, will be converted to our custom Webiny Lexical nodes.

import { createHtmlToLexicalParser } from "@webiny/lexical-converter";

const htmlString = "<p>My paragraph</p>";

// Create a parser function.
const myParser = createHtmlToLexicalParser();

// Parse the HTML string to Lexical editor state object.
const lexicalEditorState = myParser(htmlString);

Here is the result in JSON format. This object structure is a valid Lexical editor state.

{
  "root": {
    "children": [
      {
        "children": [
          {
            "detail": 0,
            "format": 0,
            "mode": "normal",
            "style": "",
            "text": "Space",
            "type": "text",
            "version": 1
          }
        ],
        "direction": null,
        "format": "",
        "indent": 0,
        "styles": [],
        "type": "paragraph-element",
        "version": 1
      }
    ],
    "direction": null,
    "format": "",
    "indent": 0,
    "type": "root",
    "version": 1
  }
}

Configuration

To configure the parser, you can pass an optional configuration object to the parser factory.

import { createHtmlToLexicalParser } from "@webiny/lexical-converter";
import { myCustomTheme } from "./theme/myCustomTheme";
import { MyCustomLexicalNode } from "./lexical/nodes/MyCustomLexicalNode";

const addCustomThemeStyleToHeadings = (node: LexicalNode): LexicalNode => {
  if (node.getType() === "heading-element") {
    return (node as HeadingNode).setThemeStyles([{ styleId: "my-default-id", type: "typography" }]);
  }
  return node;
};

// Create your parser with custom configuration
const myParser = createHtmlToLexicalParser({
  // Lexical editor configuration
  editorConfig: {
    // Add custom nodes for parsing
    nodes: [MyCustomLexicalNode],
    // Add you custom theme
    theme: myCustomTheme
  },
  nodeMapper: addCustomThemeStyleToHeadings,
  normalizeTextNodes: false // Default: true
});

const lexicalEditorState = myParser(htmlString);

To learn more about how to create custom Lexical nodes, please visit Lexical's documentation web page.

5.39.6

2 days ago

5.39.6-beta.1

2 days ago

5.39.6-beta.0

2 days ago

5.39.5

16 days ago

5.39.5-beta.1

16 days ago

5.40.0-beta.2

16 days ago

5.40.0-beta.1

22 days ago

5.39.4

28 days ago

5.39.4-beta.1

29 days ago

5.40.0-beta.0

1 month ago

5.39.4-beta.0

2 months ago

5.39.3

2 months ago

5.39.3-beta.0

2 months ago

5.39.3-beta.1

2 months ago

5.39.2

2 months ago

5.39.2-beta.2

2 months ago

5.39.2-beta.3

2 months ago

5.39.2-beta.1

2 months ago

5.39.2-beta.0

2 months ago

5.38.6

3 months ago

5.39.1

3 months ago

5.39.1-beta.1

3 months ago

5.39.0-beta.2

3 months ago

5.39.0-beta.3

3 months ago

5.39.0

3 months ago

5.39.1-beta.0

3 months ago

5.38.5-beta.0

4 months ago

5.38.5

4 months ago

5.38.4

4 months ago

5.38.4-beta.0

4 months ago

5.38.3

4 months ago

5.38.3-beta.1

4 months ago

5.38.3-beta.0

4 months ago

5.38.2

5 months ago

5.38.2-beta.0

5 months ago

5.38.1

6 months ago

5.38.1-beta.0

6 months ago

5.39.0-beta.1

6 months ago

5.39.0-beta.0

6 months ago

5.38.0

6 months ago

5.38.0-beta.6

6 months ago

5.38.0-beta.5

6 months ago

5.38.0-beta.4

6 months ago

5.38.0-beta.3

6 months ago

5.38.0-beta.2

6 months ago

5.38.0-beta.1

6 months ago

5.38.0-beta.0

6 months ago