refactor generateHtml to allow directly passing a Schema

This commit is contained in:
Hans Pagel 2020-09-04 12:50:10 +02:00
parent c36ddaf8fc
commit 95fca8aa61

View File

@ -1,16 +1,23 @@
import getSchema from './getSchema'
import { Node as ProseMirrorNode, DOMSerializer } from 'prosemirror-model'
import { Extensions } from '../types'
import { Schema } from 'prosemirror-model'
export default function generateHtml(doc: object, extensions: Extensions): string {
const schema = getSchema(extensions)
export default function generateHtml(doc: object, schema: (Extensions | Schema)): string {
let useSchema
let contentNode = ProseMirrorNode.fromJSON(schema, doc)
let temporaryDocument = document.implementation.createHTMLDocument()
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()
const container = temporaryDocument.createElement('div')
const fragment = DOMSerializer
.fromSchema(schema)
.fromSchema(useSchema)
.serializeFragment(contentNode.content)
container.appendChild(fragment)