If I have and HTML document (perhaps from a legacy system), how do I convert it to the Delta format, especially on the backend (eg Node.js server)?
If this isn't possible at the moment, what would be involved in adding support for this, as I think it would be very useful.
Example of running Quill in node.js with jsdom
It converts a Delta to HTML but you can probably do the opposite too.
That looks like it would work, but I can imagine it being very slow (and probably pretty error prone) to complete.
Emulating DOM APIs server side is slow but I would argue it's the least error prone, since the same code Quill uses to convert HTML to a Delta would be used. Of course this is limited by the quality of libraries to emulate the DOM, so that may be the concern.
If you don't want to involve Quill at all, the general approach is to parse the HTML string into some tree structure, and recursively traverse it to build up a Delta, similar to how Quill's clipboard matchers work. The more you know about your input HTML structure and the more consistent that structure is, the easier your task, since you will have far fewer cases to deal with.
I put together a node package to convert html or plain text to and from a Quill Delta.
My team used it to update our data model to include both Quill's Delta and HTML. This allows us to render on the client without an instance of Quill.
See node-quill-converter.
It features the following functions:
Behind the scenes it uses an instance of JSDOM. This may make it best suited for migration scripts as performance has not been tested in a typical app request lifecycle.
@joelcolucci Is memory leak issue resolved now? I have to convert HTML to Delta on nodejs server side. So i need this package to implement on node side
I am having exactly the same requirement as @rikh42. Data is stored as "HTML document" and need to migrate to Quill supported format. @joelcolucci's solution works and I am not very much concerned about performance considering a one time activity, but at places it is not having the close match for most of the content that I am trying to migrate. The content has child nodes nested deep and varies from div to [un]ordered lists with li content as images with text + inline style and style tags. I was reading this article and last part demos setting a tweet by creating a tweet blot. Can we do something similar like wrap a div with a class name maybe 'legacy' and register this blot?
Most helpful comment
I put together a node package to convert html or plain text to and from a Quill Delta.
My team used it to update our data model to include both Quill's Delta and HTML. This allows us to render on the client without an instance of Quill.
See node-quill-converter.
It features the following functions:
Behind the scenes it uses an instance of JSDOM. This may make it best suited for migration scripts as performance has not been tested in a typical app request lifecycle.