Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: streamline html package generation functions to work as expected #5257

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/core/src/helpers/generateJSON.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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()
}
12 changes: 4 additions & 8 deletions packages/html/src/generateHTML.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)
}
10 changes: 3 additions & 7 deletions packages/html/src/generateJSON.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)
}
32 changes: 0 additions & 32 deletions packages/html/src/getHTMLFromFragment.ts

This file was deleted.