tiptap/packages/html/getHtmlFromFragment.ts

16 lines
503 B
TypeScript
Raw Normal View History

2020-09-04 19:44:27 +08:00
import { Node, DOMSerializer } from 'prosemirror-model'
import { Schema } from 'prosemirror-model'
2020-09-10 15:36:52 +08:00
import { JSDOM } from 'jsdom'
2020-09-04 19:44:27 +08:00
export default function getHtmlFromFragment(doc: Node, schema: Schema): string {
const fragment = DOMSerializer
.fromSchema(schema)
.serializeFragment(doc.content)
2020-09-10 15:36:52 +08:00
const temporaryDocument = new JSDOM(`<!DOCTYPE html>`).window.document
2020-09-04 19:44:27 +08:00
const container = temporaryDocument.createElement('div')
container.appendChild(fragment)
return container.innerHTML
}