/// import Document from '@tiptap/extension-document' import Paragraph from '@tiptap/extension-paragraph' import Text from '@tiptap/extension-text' import { TextStyle } from '@tiptap/extension-text-style' import Youtube from '@tiptap/extension-youtube' import { generateHTML, generateJSON } from '@tiptap/html' import StarterKit from '@tiptap/starter-kit' describe('generateHTML', () => { it('generate HTML from JSON without an editor instance', () => { const json = { type: 'doc', content: [ { type: 'paragraph', content: [ { type: 'text', text: 'Example Text', }, ], }, ], } const html = generateHTML(json, [Document, Paragraph, Text]) expect(html).to.eq('

Example Text

') }) it('can convert from & to html', async () => { const extensions = [Document, Paragraph, Text, Youtube] const html = `

Tiptap now supports YouTube embeds! Awesome!

` const json = generateJSON(html, extensions) expect(json).to.deep.equal({ type: 'doc', content: [ { type: 'paragraph', content: [ { type: 'text', text: 'Tiptap now supports YouTube embeds! Awesome!', }, ], }, { type: 'youtube', attrs: { src: 'https://www.youtube.com/watch?v=cqHqLQgVCgY', start: 0, width: 640, height: 480, }, }, ], }) expect(generateHTML(json, extensions)).to.equal( '

Tiptap now supports YouTube embeds! Awesome!

', ) }) it('can convert from & to HTML with a complex schema', async () => { const extensions = [StarterKit, TextStyle] const html = `

Hi there,

this is a basic example of Tiptap. Sure, there are all kind of basic text styles you’d probably expect from a text editor. But wait until you see the lists:

Isn’t that great? And all of that is editable. But wait, there’s more. Let’s try a code block:

body {
  display: none;
}

I know, I know, this is impressive. It’s only the tip of the iceberg though. Give it a try and click a little bit around. Don’t forget to check the other examples too.

Wow, that’s amazing. Good work, boy! 👏
— Mom
` const json = generateJSON(html, extensions) const expected = { type: 'doc', content: [ { type: 'heading', attrs: { level: 2, }, content: [ { type: 'text', text: 'Hi there,', }, ], }, { type: 'paragraph', content: [ { type: 'text', text: 'this is a ', }, { type: 'text', marks: [ { type: 'italic', }, ], text: 'basic', }, { type: 'text', text: ' example of ', }, { type: 'text', marks: [ { type: 'bold', }, ], text: 'Tiptap', }, { type: 'text', text: '. Sure, there are all kind of basic text styles you’d probably expect from a text editor. But wait until you see the lists:', }, ], }, { type: 'bulletList', content: [ { type: 'listItem', content: [ { type: 'paragraph', content: [ { type: 'text', text: 'That’s a bullet list with one …', }, ], }, ], }, { type: 'listItem', content: [ { type: 'paragraph', content: [ { type: 'text', text: '… or two list items.', }, ], }, ], }, ], }, { type: 'paragraph', content: [ { type: 'text', text: 'Isn’t that great? And all of that is editable. But wait, there’s more. Let’s try a code block:', }, ], }, { type: 'codeBlock', attrs: { language: 'css', }, content: [ { type: 'text', text: 'body {\n display: none;\n}', }, ], }, { type: 'paragraph', content: [ { type: 'text', text: 'I know, I know, this is impressive. It’s only the tip of the iceberg though. Give it a try and click a little bit around. Don’t forget to check the other examples too.', }, ], }, { type: 'blockquote', content: [ { type: 'paragraph', content: [ { type: 'text', text: 'Wow, that’s amazing. Good work, boy! 👏 ', }, { type: 'hardBreak', }, { type: 'text', text: '— Mom', }, ], }, ], }, ], } expect(json).to.deep.equal(expected) expect(generateHTML(json, extensions)).to.equal( `

Hi there,

this is a basic example of Tiptap. Sure, there are all kind of basic text styles you’d probably expect from a text editor. But wait until you see the lists:

Isn’t that great? And all of that is editable. But wait, there’s more. Let’s try a code block:

body {
  display: none;
}

I know, I know, this is impressive. It’s only the tip of the iceberg though. Give it a try and click a little bit around. Don’t forget to check the other examples too.

Wow, that’s amazing. Good work, boy! 👏
— Mom

`, ) }) })