I’d like to insert some html content into a [div] tag using the node schema. Inserting the [div] tag itself was rather straight forward. How should I code the parseDOM and toDOM so that the html content can be placed within the [div] tags?
You could put the HTML in an attribute of the node, and have parseDOM and toDOM use innerHTML to read the attribute and put it back into the <div>. You’ll probably at some point want to define a node view for such a node to prevent it from re-rendering on every update (which is the default behavior when attributes change).
We are using the approach Marijn described for foreign HTML content.
We have a special node which has an attribute that contains an arbitrary HTML string. This node type does not allow children in the PM document. We then have a node view which makes a few adjustments to the HTML string before rendering it inside the nodeview dom element. The nodeview has logic to avoid re-parsing and re-rendering the HTML content unless strictly necessary.
There’s a bunch more you can do with it too, but that approach has worked well for us so far and allows for further extension. For example, we have a command which converts the node and its HTML content into a simplified form that maps nicely to nodes in our ProseMirror document schema.
Sorry to revive this topic but I’ve been misusing ProseMirror a bit (using a single Node to show a lot of content fetched from the database) and I needed to inject external HTML into a Node, so I wrote a HTML string to DOMOutputSpecArray converter.
I didn’t want to spin up a new ProseMirror instance each time because I might need to do this hundreds of times in a single document, also I’m using a NodeView for creating the document with lots of controls and consistently different markup, and simple HTML/toDOM-style view when printing to a PDF so I couldn’t just use NodeView.
Once I will have some time, I will refactor my code so it will work as intended within the ProseMirror philosophy but right now I can contribute this hack to others that are stuck in a position like I am.