npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@webiny/lexical-converter

v5.41.2

Published

[![](https://img.shields.io/npm/dw/@webiny/lexical-converter.svg)](https://www.npmjs.com/package/@webiny/llexical-lexical-converter) [![](https://img.shields.io/npm/v/@webiny/lexical-converter.svg)](https://www.npmjs.com/package/@webiny/lexical-converter)

Downloads

2,625

Readme

@webiny/lexical-converter

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.