2020-09-03 22:22:08 +08:00
|
|
|
import getSchema from './getSchema'
|
2020-09-04 17:59:09 +08:00
|
|
|
import { Node as ProseMirrorNode, DOMSerializer } from 'prosemirror-model'
|
|
|
|
import { Extensions } from '../types'
|
2020-09-04 18:50:10 +08:00
|
|
|
import { Schema } from 'prosemirror-model'
|
2020-09-03 22:22:08 +08:00
|
|
|
|
2020-09-04 18:50:10 +08:00
|
|
|
export default function generateHtml(doc: object, schema: (Extensions | Schema)): string {
|
|
|
|
let useSchema
|
2020-09-03 22:22:08 +08:00
|
|
|
|
2020-09-04 18:50:10 +08:00
|
|
|
if (Array.isArray(schema)) {
|
|
|
|
useSchema = getSchema(schema as Extensions)
|
|
|
|
} else {
|
|
|
|
useSchema = schema as Schema
|
|
|
|
}
|
|
|
|
|
|
|
|
const contentNode = ProseMirrorNode.fromJSON(useSchema, doc)
|
|
|
|
const temporaryDocument = document.implementation.createHTMLDocument()
|
2020-09-04 17:59:18 +08:00
|
|
|
const container = temporaryDocument.createElement('div')
|
2020-09-03 22:22:08 +08:00
|
|
|
|
|
|
|
const fragment = DOMSerializer
|
2020-09-04 18:50:10 +08:00
|
|
|
.fromSchema(useSchema)
|
2020-09-03 22:22:08 +08:00
|
|
|
.serializeFragment(contentNode.content)
|
|
|
|
|
2020-09-04 17:59:18 +08:00
|
|
|
container.appendChild(fragment)
|
2020-09-03 22:22:08 +08:00
|
|
|
|
2020-09-04 17:59:18 +08:00
|
|
|
return container.innerHTML
|
2020-09-03 22:22:08 +08:00
|
|
|
}
|