经过一番挖掘,这是我如何让它工作的:
- 使用 .fromJSON 创建节点
- 根据编辑器使用的模式制作 DOMSerializer
- 将节点传递给序列化器
我的代码如下。
const { schema } = require("prosemirror-schema-basic")
const { Node, DOMSerializer } = require("prosemirror-model")
const jsdom = require("jsdom").JSDOM
let dom = new jsdom('<!DOCTYPE html><div id="content"></div>')
let doc = dom.window.document
let target = doc.querySelector("div")
//Demo JSON output from ProseMirror
let content = {
"doc": {
"type": "doc",
"content": [{
"type": "paragraph",
"attrs": {
"align": "left"
},
"content": [{
"type": "text",
"text": "My sample text"
}]
}]
},
"selection": {
"type": "text",
"anchor": 16,
"head": 16
}
}
let contentNode = Node.fromJSON(schema, content.doc)
DOMSerializer
.fromSchema(schema)
.serializeFragment(contentNode.content, {
"document": doc
}, target)
console.log(doc.getElementById("content").innerHTML)
//<p>My sample text</p>
于 2018-09-14T14:16:47.720 回答