Skip to content

Commit

Permalink
Fix error: "_a.startsWith is not a function".
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jul 18, 2023
1 parent 6b00a8f commit 3b6f71d
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 23 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,4 @@ storage
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
.npmrc
.pnp.*
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@tiptap-pro:registry=https://registry.tiptap.dev/
19 changes: 19 additions & 0 deletions dist/html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getSchema } from '@tiptap/core';
import { Node, DOMParser, DOMSerializer } from '@tiptap/pm/model';
import { vdom, createHTMLDocument } from 'zeed-dom';
export function generateJSON(html, extensions) {
const schema = getSchema(extensions);
const dom = vdom(html);
return DOMParser.fromSchema(schema).parse(dom).toJSON();
}
export function generateHTML(doc, extensions) {
const schema = getSchema(extensions);
const contentNode = Node.fromJSON(schema, doc);
return getHTMLFromFragment(contentNode, schema);
}
export function getHTMLFromFragment(doc, schema) {
const document = DOMSerializer.fromSchema(schema).serializeFragment(doc.content, {
document: createHTMLDocument()
});
return document.render();
}
9 changes: 3 additions & 6 deletions dist/tiptap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// import { writeFileSync } from 'node:fs'
import { nanoid } from 'nanoid';
import { generateJSON, generateHTML } from '@tiptap/html';
// import { generateJSON, generateHTML } from '@tiptap/html'
import { generateJSON, generateHTML } from './html.js';
import Color from '@tiptap/extension-color';
import Bold from '@tiptap/extension-bold';
import Document from '@tiptap/extension-document';
Expand Down Expand Up @@ -152,14 +154,9 @@ class JSONDocumentAmender {
}
}
export function parseHTMLDocument(html) {
// writeFileSync('./debug/test-s.html', html, 'utf8')
// some error: "_a.startsWith is not a function"
// https://discuss.prosemirror.net/t/is-there-a-way-to-run-prosemirror-on-nodejs/2517/8
const jsonDoc = generateJSON(html, tiptapExtensions);
const amender = new JSONDocumentAmender();
const htmlDoc = generateHTML(amender.amendNode(jsonDoc), tiptapExtensions);
// writeFileSync('./debug/test.html', htmlDoc, 'utf8')
// writeFileSync('./debug/test.json', JSON.stringify(jsonDoc), 'utf8')
return {
json: jsonDoc,
html: htmlDoc
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webscraper",
"version": "0.2.8",
"version": "0.3.0",
"description": "",
"private": true,
"main": "dist/main.js",
Expand Down Expand Up @@ -78,7 +78,8 @@
"long": "^5.2.3",
"nanoid": "^4.0.2",
"playwright": "^1.36.1",
"zeed-dom": "0.10.5"
"prosemirror-model": "^1.19.3",
"zeed-dom": "git+https://github.com/yiwen-ai/zeed-dom.git#df3c697"
},
"devDependencies": {
"@types/config": "^3.3.0",
Expand Down
17 changes: 11 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/html.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { type Extensions, getSchema, type JSONContent } from '@tiptap/core'
import { Node, DOMParser, DOMSerializer, type Schema } from '@tiptap/pm/model'
import { vdom, createHTMLDocument, type VHTMLDocument } from 'zeed-dom'

export function generateJSON (html: string, extensions: Extensions): Record<string, any> {
const schema = getSchema(extensions)
const dom = vdom(html) as any

return DOMParser.fromSchema(schema).parse(dom).toJSON()
}

export function generateHTML (doc: JSONContent, extensions: Extensions): string {
const schema = getSchema(extensions)
const contentNode = Node.fromJSON(schema, doc)

return getHTMLFromFragment(contentNode, schema)
}

export function getHTMLFromFragment (doc: Node, schema: Schema): string {
const document = DOMSerializer.fromSchema(schema).serializeFragment(doc.content, {
document: createHTMLDocument() as unknown as Document
}) as unknown as VHTMLDocument

return document.render()
}
10 changes: 3 additions & 7 deletions src/tiptap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// import { writeFileSync } from 'node:fs'
import { nanoid } from 'nanoid'
import { generateJSON, generateHTML } from '@tiptap/html'
// import { generateJSON, generateHTML } from '@tiptap/html'
import { generateJSON, generateHTML } from './html.js'
import Color from '@tiptap/extension-color'
import Bold from '@tiptap/extension-bold'
import Document from '@tiptap/extension-document'
Expand Down Expand Up @@ -176,15 +178,9 @@ export function parseHTMLDocument (html: string): {
json: any
html: string
} {
// writeFileSync('./debug/test-s.html', html, 'utf8')
// some error: "_a.startsWith is not a function"
// https://discuss.prosemirror.net/t/is-there-a-way-to-run-prosemirror-on-nodejs/2517/8
const jsonDoc = generateJSON(html, tiptapExtensions)
const amender = new JSONDocumentAmender()
const htmlDoc = generateHTML(amender.amendNode(jsonDoc as Node), tiptapExtensions)

// writeFileSync('./debug/test.html', htmlDoc, 'utf8')
// writeFileSync('./debug/test.json', JSON.stringify(jsonDoc), 'utf8')
return {
json: jsonDoc,
html: htmlDoc
Expand Down

0 comments on commit 3b6f71d

Please sign in to comment.