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