tiptap/tests/cypress/integration/static-renderer/json-string.spec.ts
Nick Perez 6a53bb2699
Some checks failed
build / build (20) (push) Has been cancelled
Publish / Release (20) (push) Has been cancelled
build / test (20, map[name:Demos/Commands spec:./demos/src/Commands/**/*.spec.{js,ts}]) (push) Has been cancelled
build / test (20, map[name:Demos/Examples spec:./demos/src/Examples/**/*.spec.{js,ts}]) (push) Has been cancelled
build / test (20, map[name:Demos/Experiments spec:./demos/src/Experiments/**/*.spec.{js,ts}]) (push) Has been cancelled
build / test (20, map[name:Demos/Extensions spec:./demos/src/Extensions/**/*.spec.{js,ts}]) (push) Has been cancelled
build / test (20, map[name:Demos/GuideContent spec:./demos/src/GuideContent/**/*.spec.{js,ts}]) (push) Has been cancelled
build / test (20, map[name:Demos/GuideGettingStarted spec:./demos/src/GuideGettingStarted/**/*.spec.{js,ts}]) (push) Has been cancelled
build / test (20, map[name:Demos/Marks spec:./demos/src/Marks/**/*.spec.{js,ts}]) (push) Has been cancelled
build / test (20, map[name:Demos/Nodes spec:./demos/src/Nodes/**/*.spec.{js,ts}]) (push) Has been cancelled
build / test (20, map[name:Integration spec:./tests/cypress/integration/**/*.spec.{js,ts}]) (push) Has been cancelled
build / release (20) (push) Has been cancelled
feat(static-renderer): add @tiptap/static-renderer to enable static rendering of content (#5528)
2025-01-06 14:00:38 +01:00

297 lines
7.3 KiB
TypeScript

/// <reference types="cypress" />
import { TextType } from '@tiptap/core'
import Bold from '@tiptap/extension-bold'
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import { Mark, Node } from '@tiptap/pm/model'
import { renderJSONContentToString, serializeChildrenToHTMLString } from '@tiptap/static-renderer/json/html-string'
import { renderToHTMLString } from '@tiptap/static-renderer/pm/html-string'
describe('static render json to string (no prosemirror)', () => {
it('generate an HTML string from JSON without an editor instance', () => {
const json = {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Example Text',
},
],
},
],
attrs: {},
}
const html = renderJSONContentToString({
nodeMapping: {
doc: ({ children }) => {
return `<doc>${serializeChildrenToHTMLString(children)}</doc>`
},
paragraph: ({ children }) => {
return `<p>${serializeChildrenToHTMLString(children)}</p>`
},
text: ({ node }) => {
return (node as unknown as TextType).text
},
},
markMapping: {},
})({ content: json })
expect(html).to.eq('<doc><p>Example Text</p></doc>')
})
it('supports mapping nodes & marks', () => {
const json = {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Example Text',
marks: [
{
type: 'bold',
attrs: {},
},
],
},
],
},
],
attrs: {},
}
const html = renderJSONContentToString({
nodeMapping: {
doc: ({ children }) => {
return `<doc>${serializeChildrenToHTMLString(children)}</doc>`
},
paragraph: ({ children }) => {
return `<p>${serializeChildrenToHTMLString(children)}</p>`
},
text: ({ node }) => {
return (node as unknown as TextType).text
},
},
markMapping: {
bold: ({ children }) => {
return `<strong>${serializeChildrenToHTMLString(children)}</strong>`
},
},
})({ content: json })
expect(html).to.eq('<doc><p><strong>Example Text</strong></p></doc>')
})
it('gives access to the original JSON node or mark', () => {
const json = {
type: 'doc',
content: [
{
type: 'heading',
attrs: {
level: 2,
},
content: [
{
type: 'text',
text: 'Example Text',
marks: [
{
type: 'bold',
attrs: {},
},
],
},
],
},
],
attrs: {},
}
const html = renderJSONContentToString({
nodeMapping: {
doc: ({ node, children }) => {
expect(node).to.deep.eq(json)
return `<doc>${serializeChildrenToHTMLString(children)}</doc>`
},
heading: ({ node, children }) => {
expect(node).to.deep.eq({
type: 'heading',
attrs: {
level: 2,
},
content: [
{
type: 'text',
text: 'Example Text',
marks: [
{
type: 'bold',
attrs: {},
},
],
},
],
})
return `<h${node.attrs.level}>${serializeChildrenToHTMLString(children)}</h${node.attrs.level}>`
},
text: ({ node }) => {
expect(node).to.deep.eq({
type: 'text',
text: 'Example Text',
marks: [
{
type: 'bold',
attrs: {},
},
],
})
return (node as unknown as TextType).text
},
},
markMapping: {
bold: ({ children, mark }) => {
expect(mark).to.deep.eq({
type: 'bold',
attrs: {},
})
return `<strong>${serializeChildrenToHTMLString(children)}</strong>`
},
},
})({ content: json })
expect(html).to.eq('<doc><h2><strong>Example Text</strong></h2></doc>')
})
})
describe('static render json to string (with prosemirror)', () => {
it('generates an HTML string from JSON without an editor instance', () => {
const json = {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Example Text',
marks: [
{
type: 'bold',
attrs: {},
},
],
},
],
},
],
attrs: {},
}
const html = renderToHTMLString({
content: json,
extensions: [Document, Paragraph, Text, Bold],
})
expect(html).to.eq('<p><strong>Example Text</strong></p>')
})
it('supports custom mapping for nodes & marks', () => {
const json = {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Example Text',
marks: [
{
type: 'bold',
attrs: {},
},
],
},
],
},
],
attrs: {},
}
const html = renderToHTMLString({
content: json,
extensions: [Document, Paragraph, Text, Bold],
options: {
nodeMapping: {
doc: ({ children }) => {
return `<doc>${serializeChildrenToHTMLString(children)}</doc>`
},
},
markMapping: {
bold: ({ children }) => {
return `<b>${serializeChildrenToHTMLString(children)}</b>`
},
},
},
})
expect(html).to.eq('<doc><p><b>Example Text</b></p></doc>')
})
it('gives access to a prosemirror node or mark instance', () => {
const json = {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Example Text',
marks: [
{
type: 'bold',
attrs: {},
},
],
},
],
},
],
attrs: {},
}
const html = renderToHTMLString({
content: json,
extensions: [Document, Paragraph, Text, Bold],
options: {
nodeMapping: {
doc: ({ children, node }) => {
expect(node.type.name).to.eq('doc')
expect(node).to.be.instanceOf(Node)
return `<doc>${serializeChildrenToHTMLString(children)}</doc>`
},
},
markMapping: {
bold: ({ children, mark }) => {
expect(mark.type.name).to.eq('bold')
expect(mark).to.be.instanceOf(Mark)
return `<b>${serializeChildrenToHTMLString(children)}</b>`
},
},
},
})
expect(html).to.eq('<doc><p><b>Example Text</b></p></doc>')
})
})