tiptap/docs/guide/output.md

137 lines
6.0 KiB
Markdown
Raw Normal View History

---
tableOfContents: true
---
2021-04-07 05:36:07 +08:00
# Output
## Introduction
You can store your content as a JSON object or as a good old HTML string. Both work fine. And of course, you can pass both formats to the editor to restore your content. Here is an interactive example, that exports the content as HTML and JSON when the document is changed:
2020-08-12 18:16:58 +08:00
2020-11-30 22:58:05 +08:00
## Export
2020-08-12 18:16:58 +08:00
2020-11-30 22:58:05 +08:00
### Option 1: JSON
2021-10-20 04:30:45 +08:00
JSON is probably easier to loop through, for example to look for a mention and its more like what Tiptap uses under the hood. Anyway, if you want to use JSON to store the content we provide a method to retrieve the content as JSON:
2020-08-12 18:16:58 +08:00
2020-09-16 23:01:47 +08:00
```js
const json = editor.getJSON()
2020-09-16 23:01:47 +08:00
```
You can store that in your database (or send it to an API) and restore the document initially:
2020-09-16 23:01:47 +08:00
```js
new Editor({
content: {
2021-03-15 18:06:09 +08:00
"type": "doc",
2020-09-16 23:01:47 +08:00
"content": [
2020-11-30 22:58:05 +08:00
// …
2020-09-16 23:01:47 +08:00
]
},
})
```
Or if you need to wait for something, you can do it later through the editor instance:
```js
2021-03-15 18:06:09 +08:00
editor.commands.setContent({
"type": "doc",
2020-09-16 23:01:47 +08:00
"content": [
2020-11-30 22:58:05 +08:00
// …
2020-09-16 23:01:47 +08:00
]
})
```
2020-11-30 22:58:05 +08:00
Here is an interactive example where you can see that in action:
2021-10-19 00:01:47 +08:00
https://embed.tiptap.dev/preview/GuideContent/ExportJSON?hideSource
2020-11-30 22:58:05 +08:00
### Option 2: HTML
2023-10-16 13:17:24 +08:00
HTML can be easily rendered in other places, for example in emails and its widely used, so its probably easier to switch the editor at some point. Anyway, every editor instance provides a method to get HTML from the current document:
2020-09-16 23:01:47 +08:00
```js
const html = editor.getHTML()
2020-09-16 23:01:47 +08:00
```
This can then be used to restore the document initially:
```js
new Editor({
content: `<p>Example Text</p>`,
})
```
Or if you want to restore the content later (e. g. after an API call has finished), you can do that too:
```js
2020-11-13 18:52:02 +08:00
editor.commands.setContent(`<p>Example Text</p>`)
2020-09-16 23:01:47 +08:00
```
2020-11-30 22:58:05 +08:00
Use this interactive example to fiddle around:
2021-10-19 00:01:47 +08:00
https://embed.tiptap.dev/preview/GuideContent/ExportHTML?hideSource
2020-11-30 22:58:05 +08:00
2021-02-02 22:01:37 +08:00
### Option 3: Y.js
2021-04-16 03:48:19 +08:00
Our editor has top notch support for Y.js, which is amazing to add features like [realtime collaboration, offline editing, or syncing between devices](/guide/collaborative-editing).
2021-02-02 22:01:37 +08:00
Internally, Y.js stores a history of all changes. That can be in the browser, on a server, synced with other connected clients, or on a USB stick. But, its important to know that Y.js needs those stored changes. A simple JSON document is not enough to merge changes.
Sure, you can import existing JSON documents to get started and get a JSON out of Y.js, but thats more like an import/export format. It wont be your single source. Thats important to consider when adding Y.js for one of the mentioned use cases.
That said, its amazing and were about to provide an amazing backend, that makes all that a breeze.
2020-11-30 22:58:05 +08:00
### Not an option: Markdown
2022-11-08 04:41:00 +08:00
Unfortunately, **Tiptap doesnt support Markdown as an input or output format**. We considered to add support for it, but those are the reasons why we decided to not do it:
2020-08-12 18:16:58 +08:00
2020-11-17 22:38:46 +08:00
* Both, HTML and JSON, can have deeply nested structures, Markdown is flat.
* Markdown standards vary.
2021-10-20 04:30:45 +08:00
* Tiptaps strength is customization, that doesnt work very well with Markdown.
2021-04-16 03:48:19 +08:00
* There are enough packages to convert HTML to Markdown and vice-versa.
2020-08-12 18:16:58 +08:00
2020-09-22 22:03:31 +08:00
You should really consider to work with HTML or JSON to store your content, they are perfectly fine for most use cases.
2021-10-20 04:30:45 +08:00
If you still think you need Markdown, ProseMirror has an [example on how to deal with Markdown](https://prosemirror.net/examples/markdown/), [Nextcloud Text](https://github.com/nextcloud/text) uses Tiptap 1 to work with Markdown. Maybe you can learn from them. Or if you are looking for a really good Markdown editor, try [CodeMirror](https://codemirror.net/).
2020-09-22 22:03:31 +08:00
2021-10-20 04:30:45 +08:00
That said, Tiptap does support [Markdown shortcuts](/examples/markdown-shortcuts) to format your content. Also youre free to let your content look like Markdown, for example add a `#` before an `<h1>` with CSS.
2020-11-05 21:39:28 +08:00
2020-12-15 06:29:57 +08:00
## Listening for changes
If you want to continuously store the updated content while people write, you can [hook into events](/api/events). Here is an example how that could look like:
```js
const editor = new Editor({
// intial content
content: `<p>Example Content</p>`,
// triggered on every change
2021-10-26 05:09:31 +08:00
onUpdate: ({ editor }) => {
const json = editor.getJSON()
2020-12-15 06:29:57 +08:00
// send the content to an API here
},
})
```
2020-11-30 22:58:05 +08:00
## Rendering
2021-10-20 04:30:45 +08:00
### Option 1: Read-only instance of Tiptap
2020-11-30 22:58:05 +08:00
To render the saved content, set the editor to read-only. Thats how you can achieve the exact same rendering as its in the editor, without duplicating your CSS and other code.
2021-10-19 00:01:47 +08:00
https://embed.tiptap.dev/preview/GuideContent/ReadOnly
2020-11-30 22:58:05 +08:00
### Option 2: Generate HTML from ProseMirror JSON
If you need to render the content on the server side, for example to generate the HTML for a blog post, which has been written in Tiptap, youll probably want to do just that without an actual editor instance.
2020-11-05 21:39:28 +08:00
2020-11-30 22:58:05 +08:00
Thats what the `generateHTML()` is for. Its a helper function which renders HTML without an actual editor instance.
2020-11-05 21:39:28 +08:00
2021-10-19 00:01:47 +08:00
https://embed.tiptap.dev/preview/GuideContent/GenerateHTML
By the way, the other way is possible, too. The below examples shows how to generate JSON from HTML.
2021-10-19 00:01:47 +08:00
https://embed.tiptap.dev/preview/GuideContent/GenerateJSON
2021-01-21 19:35:06 +08:00
## Migration
2021-10-20 04:30:45 +08:00
If youre migrating existing content to Tiptap we would recommend to get your existing output to HTML. Thats probably the best format to get your initial content into Tiptap, because ProseMirror ensures there is nothing wrong with it. Even if there are some tags or attributes that arent allowed (based on your configuration), Tiptap just throws them away quietly.
2021-01-21 19:35:06 +08:00
Were about to go through a few cases to help with that, for example we provide a PHP package to convert HTML to a compatible JSON structure: [ueberdosis/prosemirror-to-html](https://github.com/ueberdosis/html-to-prosemirror).
2021-02-09 00:39:50 +08:00
[Share your experiences with us!](mailto:humans@tiptap.dev) Wed like to add more information here.
2021-02-02 22:01:37 +08:00
## Security
2021-10-20 04:30:45 +08:00
There is no reason to use one or the other because of security concerns. If someone wants to send malicious content to your server, it doesnt matter if its JSON or HTML. It doesnt even matter if youre using Tiptap or not. You should always validate user input.