1.0.0 • Published 5 years ago

html-to-prosemirror-js v1.0.0

Weekly downloads
2
License
ISC
Repository
-
Last release
5 years ago

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-js --save

Usage

const Renderer = require("html-to-prosemirror-js").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);