question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to call `convertFromHTML` on NodeJS?

See original GitHub issue

Do you want to request a feature or report a bug?

I want to migrate from my legacy html content in DB to Draft Raw Object.

var raw = converrtToRaw(ContentState.createFromBlockArray(convertFromHTML('<div></div>)))

What is the current behavior?

convertFromHTML requires document object.

ReferenceError: document is not defined
    at getSafeBodyFromHTML (/PROJECT_DIRECTORY/node_modules/draft-js/lib/getSafeBodyFromHTML.js:27:19)

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar.

Run the below codes in Node

const djs = require('draft-js')
console.log(djs.convertFromHTML('<div></div>'))

What is the expected behavior?

return {blocks: [...], ....}

Which versions of Draft.js, and which browser / OS are affected by this issue? Did this work in previous versions of Draft.js?

Node v4.4.4

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
eamontaaffecommented, May 10, 2017

I was just playing around with a similar implementation.

Rather than executing within a synthetic dom, you can use the DOMBuilder input to the convertFromHTML, which is just an alias for convertFromHTMLtoContentBlocks and has this signature:

function convertFromHTMLtoContentBlocks(
    html: string,
    DOMBuilder: Function = getSafeBodyFromHTML,
    blockRenderMap?: DraftBlockRenderMap = DefaultDraftBlockRenderMap,
)

You can instantiate a simple DOMBuilder using jsdom like so:

function simpleDOMBuilder(html) {
    const {
        document,
        HTMLElement,
        HTMLAnchorElement,
    } = (new JSDOM(`<!DOCTYPE html>`)).window;

    global.HTMLElement = HTMLElement;
    global.HTMLAnchorElement = HTMLAnchorElement;

    const doc = document.implementation.createHTMLDocument('foo');
    doc.documentElement.innerHTML = html;
    const root = doc.getElementsByTagName('body')[0];
    return root;
}

Then pass this to convertFromHTML as an argument:

const blocks = convertFromHTML(html, simpleDOMBuilder);

Note: You still need to attach the variables HTMLElement and HTMLAnchorElement to global because they are used for type checking within convertFromHTMLtoContentBlocks.

1reaction
sophiebitscommented, Aug 9, 2016

Cool! There is probably no better way, though you could create a separate VM context to load jsdom and Draft into if you don’t want to pollute your main one. I don’t see us making changes to support this better since convertFromHTML is designed around a browser implementation so I’m going to close this out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nextjs with Draft js - document is not defined when i initiate ...
It errors when I initialize the editorState with a content. const blocksFromHTML = convertFromHTML( "<p>Hey this <strong>editor</strong> rocks ...
Read more >
Data Conversion | Draft.js
This is useful when saving an editor state for storage, conversion to other formats, or other usage within an application. convertFromHTML() #.
Read more >
bytescout - npm
For Node.js. Install it via: npm install bytescout --save. Local development.
Read more >
Convert from HTML to Image - Pdfcrowd
Hi! I need to convert from HTML to image. Is there a way to do it with you tool? If not, which tool...
Read more >
TechTip: Hapi to Authenticate You | Programming - Other
Here we see the Node.js iToolkit being used to call a *PGM object, ... use of tools like html2jade.org to convert from HTML...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found