Skip to content

Commit

Permalink
feat: implement a function for html-to-draftJs conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
chathurabuddi committed Dec 1, 2022
1 parent a6e6a68 commit 901e334
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
24 changes: 24 additions & 0 deletions htmlToDraftJs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { stateFromElement } from 'draft-js-import-element';
import { JSDOM } from "jsdom";

const defaultOptions = {};

const parseHTML = (html) => {
const dom = new JSDOM(html)
const doc = dom.window.document.implementation.createHTMLDocument('');
if (doc.documentElement) {
doc.documentElement.innerHTML = html;
}
return doc.body || doc.createElement('body');
}

const htmlToDraftJs = (html, options) => {
let {parser, ...otherOptions} = options || defaultOptions;
if (parser == null) {
parser = parseHTML;
}
let element = parser(html);
return stateFromElement(element, otherOptions);
}

export default htmlToDraftJs;
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import htmlToDraftJs from './htmlToDraftJs';

export { htmlToDraftJs };

0 comments on commit 901e334

Please sign in to comment.