@dvargas92495/html-to-draftjs

1.5.1 • Public • Published

This is a forked version of html-to-draftjs, as it seems like the project hasn't been maintained since December, 2019.

HTML To DraftJS

A library for converting plain HTML to DraftJS Editor content. Build for use with react-draft-wysiwyg.

Installation

npm install html-to-draftjs --save

Usage

import { EditorState, ContentState } from 'draft-js';
import htmlToDraft from 'html-to-draftjs';

const blocksFromHtml = htmlToDraft(this.props.content);
const { contentBlocks, entityMap } = blocksFromHtml;
const contentState = ContentState.createFromBlockArray(contentBlocks, entityMap);
const editorState = EditorState.createWithContent(contentState);

(optional) customChunkRenderer

Use to define additional html nodes. Only supports atomic blocks.

  • nodeName: string - the name of the node, in lowercase
  • node: HTMLElement - the parsed node itself

This renderer function is executed before any other html to draft conversion. Return nothing (or something falsy) to continue with the normal translation.

Example:

htmlToDraft('<hr/>', (nodeName, node) => {
  if (nodeName === 'hr') {
    return {
      type: 'HORIZONTAL_RULE',
      mutability: 'MUTABLE',
      data: {}
    };
  }
})

Take Care: Plz not use version 1.2.0 it has build issues.

Dependencies (0)

    Dev Dependencies (19)

    Package Sidebar

    Install

    npm i @dvargas92495/html-to-draftjs

    Weekly Downloads

    0

    Version

    1.5.1

    License

    MIT

    Unpacked Size

    217 kB

    Total Files

    7

    Last publish

    Collaborators

    • dvargas92495