如何将html转换为draftjs?

问题描述

我已经尝试过基本软件包,但是我似乎不了解最新情况,

这是我尝试过的东西;

const {
  convertFromHTML,ContentState
} = require('draft-js');
const htmlToDraft = require('html-to-draftjs');

const converter = () => {
    const sampleMarkup =
      '<b>Bold text</b>,<i>Italic text</i><br/ ><br />' +
      '<a href="http://www.google.com">Example link</a>';

    const blocksFromHTML = convertFromHTML(sampleMarkup);
    const state = ContentState.createFromBlockArray(blocksFromHTML);

    console.log('state: ',state);
}

converter();

非常清楚要使用哪个库。 我看起来很奇怪,我期望的输出看起来像这样;

{
  "blocks": [
    {
      "depth": 0,"inlinestyleRanges": [
        {
          "length": 9,"style": "BOLD","offset": 0
        },{
          "length": 12,"style": "ITALIC","offset": 11
        }
      ],"entityRanges": [
        {
          "length": 12,"key": 0,"offset": 25
        }
      ],"data": {},"text": "Bold text,Italics text\n\nexample link ","key": "9jc4q","type": "unstyled"
    }
  ],"entityMap": {
    "0": {
      "type": "LINK","mutability": "MUTABLE","data": {
        "url": "http://www.google.com","targetoption": "_blank"
      }
    }
  }
}

有什么见解? (服务器端代码

解决方法

const sampleMarkup =
  '<b>Bold text</b>,<i>Italic text</i><br/ ><br />' +
  '<a href="http://www.facebook.com">Example link</a>';

const blocksFromHTML = convertFromHTML(sampleMarkup);
const state = ContentState.createFromBlockArray(
  blocksFromHTML.contentBlocks,blocksFromHTML.entityMap,);

this.state = {
  editorState: EditorState.createWithContent(state),};