init @tiptap/html package
This commit is contained in:
23
packages/html/example.js
Normal file
23
packages/html/example.js
Normal file
@@ -0,0 +1,23 @@
|
||||
import { generateHtml } from '@tiptap/html'
|
||||
|
||||
import Document from '@tiptap/extension-document'
|
||||
import Paragraph from '@tiptap/extension-paragraph'
|
||||
import Text from '@tiptap/extension-text'
|
||||
|
||||
const html = generateHtml({
|
||||
'type': 'document',
|
||||
'content': [{
|
||||
'type': 'paragraph',
|
||||
'attrs': {
|
||||
'align': 'left'
|
||||
},
|
||||
'content': [{
|
||||
'type': 'text',
|
||||
'text': 'Example Text'
|
||||
}]
|
||||
}]
|
||||
}, [
|
||||
new Document(),
|
||||
new Paragraph(),
|
||||
new Text(),
|
||||
])
|
||||
16
packages/html/getHtmlFromFragment.ts
Normal file
16
packages/html/getHtmlFromFragment.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Node, DOMSerializer } from 'prosemirror-model'
|
||||
import { Schema } from 'prosemirror-model'
|
||||
const jsdom = require('jsdom')
|
||||
const { JSDOM } = jsdom
|
||||
|
||||
export default function getHtmlFromFragment(doc: Node, schema: Schema): string {
|
||||
const fragment = DOMSerializer
|
||||
.fromSchema(schema)
|
||||
.serializeFragment(doc.content)
|
||||
|
||||
const temporaryDocument = new jsdom(`<!DOCTYPE html>`).window.document
|
||||
const container = temporaryDocument.createElement('div')
|
||||
container.appendChild(fragment)
|
||||
|
||||
return container.innerHTML
|
||||
}
|
||||
11
packages/html/index.ts
Normal file
11
packages/html/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import getSchema from '@tiptap/core'
|
||||
import getHtmlFromFragment from './getHtmlFromFragment'
|
||||
import { Node } from 'prosemirror-model'
|
||||
import { Extensions } from '@tiptap/core'
|
||||
|
||||
export default function generateHtml(doc: object, extensions: Extensions): string {
|
||||
const schema = getSchema(extensions)
|
||||
const contentNode = Node.fromJSON(schema, doc)
|
||||
|
||||
return getHtmlFromFragment(contentNode, schema)
|
||||
}
|
||||
14
packages/html/package.json
Normal file
14
packages/html/package.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "@tiptap/html",
|
||||
"version": "1.0.0",
|
||||
"source": "index.ts",
|
||||
"files": [
|
||||
"src",
|
||||
"dist"
|
||||
],
|
||||
"dependencies": {
|
||||
"@tiptap/core": "2.x",
|
||||
"jsdom": "^16.4.0",
|
||||
"prosemirror-model": "^1.11.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user