diff --git a/packages/html/src/getHTMLFromFragment.ts b/packages/html/src/getHTMLFromFragment.ts index dcc46a931f9..f625e7b77de 100644 --- a/packages/html/src/getHTMLFromFragment.ts +++ b/packages/html/src/getHTMLFromFragment.ts @@ -1,10 +1,20 @@ import { DOMSerializer, Node, Schema } from '@tiptap/pm/model' import { createHTMLDocument, VHTMLDocument } from 'zeed-dom' -export function getHTMLFromFragment(doc: Node, schema: Schema): string { - const document = DOMSerializer.fromSchema(schema).serializeFragment(doc.content, { +export function getHTMLFromFragment(doc: Node, schema: Schema, options?: { document?: Document }): string { + if (options?.document) { + // The caller is relying on their own document implementation. Use this + // instead of the default zeed-dom. + const wrap = options.document.createElement('div') + + DOMSerializer.fromSchema(schema).serializeFragment(doc.content, { document: options.document }, wrap) + return wrap.innerHTML + } + + // Use zeed-dom for serialization. + const zeedDocument = DOMSerializer.fromSchema(schema).serializeFragment(doc.content, { document: createHTMLDocument() as unknown as Document, }) as unknown as VHTMLDocument - return document.render() + return zeedDocument.render() }