-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: whitespace being stripped from generateJSON (#5158)
- Loading branch information
Showing
1 changed file
with
4 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,22 @@ | ||
import { Extensions, getSchema } from '@tiptap/core' | ||
import { DOMParser } from '@tiptap/pm/model' | ||
import { DOMParser, ParseOptions } from '@tiptap/pm/model' | ||
import { parseHTML } from 'zeed-dom' | ||
|
||
/** | ||
* Generates a JSON object from the given HTML string and converts it into a Prosemirror node with content. | ||
* @param {string} html - The HTML string to be converted into a Prosemirror node. | ||
* @param {Extensions} extensions - The extensions to be used for generating the schema. | ||
* @param {ParseOptions} options - The options to be supplied to the parser. | ||
* @returns {Record<string, any>} - The generated JSON object. | ||
* @example | ||
* const html = '<p>Hello, world!</p>' | ||
* const extensions = [...] | ||
* const json = generateJSON(html, extensions) | ||
* console.log(json) // { type: 'doc', content: [{ type: 'paragraph', content: [{ type: 'text', text: 'Hello, world!' }] }] } | ||
*/ | ||
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 = parseHTML(html) as unknown as Node | ||
|
||
return DOMParser.fromSchema(schema).parse(dom).toJSON() | ||
return DOMParser.fromSchema(schema).parse(dom, options).toJSON() | ||
} |