fix: streamline html package generation functions to work in sync as other generate functions

This commit is contained in:
bdbch 2024-06-20 14:33:03 +02:00
parent 0bdd09fb53
commit 1fa718a3f9
4 changed files with 10 additions and 50 deletions

View File

@ -1,4 +1,4 @@
import { DOMParser } from '@tiptap/pm/model'
import { type ParseOptions, DOMParser } from '@tiptap/pm/model'
import { Extensions } from '../types.js'
import { elementFromString } from '../utilities/elementFromString.js'
@ -10,9 +10,9 @@ import { getSchema } from './getSchema.js'
* @param extensions The extensions to use for the schema
* @returns The generated JSONContent
*/
export function generateJSON(html: string, extensions: Extensions): Record<string, any> {
export function generateJSON(html: string, extensions: Extensions, options?: ParseOptions): Record<string, any> {
const schema = getSchema(extensions)
const dom = elementFromString(html)
return DOMParser.fromSchema(schema).parse(dom).toJSON()
return DOMParser.fromSchema(schema).parse(dom, options).toJSON()
}

View File

@ -1,7 +1,6 @@
import { Extensions, getSchema, JSONContent } from '@tiptap/core'
import { Node } from '@tiptap/pm/model'
import { getHTMLFromFragment } from './getHTMLFromFragment.js'
import {
Extensions, generateHTML as coreGenerateHTML, JSONContent,
} from '@tiptap/core'
/**
* Generates HTML from a ProseMirror JSON content object.
@ -27,8 +26,5 @@ import { getHTMLFromFragment } from './getHTMLFromFragment.js'
* const html = generateHTML(doc, extensions)
*/
export function generateHTML(doc: JSONContent, extensions: Extensions): string {
const schema = getSchema(extensions)
const contentNode = Node.fromJSON(schema, doc)
return getHTMLFromFragment(contentNode, schema)
return coreGenerateHTML(doc, extensions)
}

View File

@ -1,6 +1,5 @@
import { Extensions, getSchema } from '@tiptap/core'
import { DOMParser, ParseOptions } from '@tiptap/pm/model'
import { parseHTML } from 'zeed-dom'
import { Extensions, generateJSON as CoreGenerateJSON } from '@tiptap/core'
import { ParseOptions } from '@tiptap/pm/model'
/**
* Generates a JSON object from the given HTML string and converts it into a Prosemirror node with content.
@ -15,8 +14,5 @@ import { parseHTML } from 'zeed-dom'
* console.log(json) // { type: 'doc', content: [{ type: 'paragraph', content: [{ type: 'text', text: 'Hello, world!' }] }] }
*/
export function generateJSON(html: string, extensions: Extensions, options?: ParseOptions): Record<string, any> {
const schema = getSchema(extensions)
const dom = parseHTML(html) as unknown as Node
return DOMParser.fromSchema(schema).parse(dom, options).toJSON()
return CoreGenerateJSON(html, extensions, options)
}

View File

@ -1,32 +0,0 @@
import { DOMSerializer, Node, Schema } from '@tiptap/pm/model'
import { createHTMLDocument, VHTMLDocument } from 'zeed-dom'
/**
* Returns the HTML string representation of a given document node.
*
* @param doc - The document node to serialize.
* @param schema - The Prosemirror schema to use for serialization.
* @returns The HTML string representation of the document fragment.
*
* @example
* ```typescript
* const html = getHTMLFromFragment(doc, schema)
* ```
*/
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 zeedDocument.render()
}