html-to-prosemirror

1.0.1 • Public • Published

HTML to Prosemirror

(JS)

(This package is based on html-to-prosemirror, which was originally written for PHP.)

Takes HTML and outputs ProseMirror JSON.

Installation

npm install html-to-prosemirror --save

Usage

const Renderer = require("html-to-prosemirror").Renderer;
 
const renderer = new Renderer();
 
console.log(
    JSON.stringify(
        renderer.render(`<p>Example Text</p>`)
    )
);
/*
    {
        type: "doc",
        content: [
            {
                type: "paragraph",
                content: [
                    {
                        type: "text",
                        text: "Example Text"
                    }
                ]
            }
        ]
    }
*/

Supported Nodes

  • BulletList
  • CodeBlock
  • CodeBlockWrapper (with pre)
  • HardBreak
  • Heading
  • Image (different from the scrumpy's implementations)
  • ListItem
  • OrderedList
  • Paragraph

Supported Marks

  • Bold
  • Code
  • Italic
  • Link

Custom Nodes

Define your node as a class -

const Node = require("html-to-prosemirror").Node;
 
class CustomNode extends Node {
    matching () {
        return this.DOMNode.nodeName === "USER"; // uses `jsdom` library
    }
 
    data () {
        return {
            type: "user",
            attrs: {
                id: this.DOMNode.getAttribute("data-id")
            }
        };
    }
}

Feed it to renderer instance -

renderer.addNode(CustomNode);

Package Sidebar

Install

npm i html-to-prosemirror

Weekly Downloads

246

Version

1.0.1

License

ISC

Unpacked Size

29.8 kB

Total Files

29

Last publish

Collaborators

  • envolt