tiptap/packages/core/src/utils/generateHtml.ts

20 lines
631 B
TypeScript
Raw Normal View History

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 17:59:09 +08:00
export default function generateHtml(doc: object, extensions: Extensions): string {
const schema = getSchema(extensions)
let contentNode = ProseMirrorNode.fromJSON(schema, doc)
let temporaryDocument = document.implementation.createHTMLDocument()
2020-09-04 17:59:18 +08:00
const container = temporaryDocument.createElement('div')
const fragment = DOMSerializer
.fromSchema(schema)
.serializeFragment(contentNode.content)
2020-09-04 17:59:18 +08:00
container.appendChild(fragment)
2020-09-04 17:59:18 +08:00
return container.innerHTML
}