
There is no reason to use one or the other because of security concerns. Share your experiences with us! We’d like to add more information here. We’re 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. Even if there are some tags or attributes that aren’t allowed (based on your configuration), Tiptap just throws them away quietly. That’s probably the best format to get your initial content into Tiptap, because ProseMirror ensures there is nothing wrong with it. If you’re migrating existing content to Tiptap we would recommend to get your existing output to HTML.
#CONVERT HTML FORM TO JSON HELPER FUNCTION HOW TO#
The below examples shows how to generate JSON from HTML. It’s a helper function which renders HTML without an actual editor instance.īy the way, the other way is possible, too. 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, you’ll probably want to do just that without an actual editor instance. #Option 2: Generate HTML from ProseMirror JSON That’s how you can achieve the exact same rendering as it’s in the editor, without duplicating your CSS and other code. To render the saved content, set the editor to read-only. You can store that in your database (or send it to an API) and restore the document initially like that: new Editor () #Rendering #Option 1: Read-only instance of Tiptap Anyway, if you want to use JSON to store the content we provide a method to retrieve the content as JSON: const json = editor. JSON is probably easier to loop through, for example to look for a mention and it’s more like what Tiptap uses under the hood.

Here is an interactive example, that exports the content as HTML and JSON when the document is changed: #Export #Option 1: JSON And of course, you can pass both formats to the editor to restore your content. You can store your content as a JSON object or as a good old HTML string. Option 2: Generate HTML from ProseMirror JSON.In this blog post, we explored an effective and reusable way of submitting HTML forms to JSON APIs. Informing the user or changing the pageįinally, we will navigate to the page that says "Success" and, for simplicity, show response data in the URL.
#CONVERT HTML FORM TO JSON HELPER FUNCTION CODE#
Since the code is self-explanatory, take a look: Now, the Fetch Service is what we built to submit any request and get the response so that we don't have to keep rewriting its logic. You can checkout the sample function in app.js in the source to see one way to do it. Typically, you'd add headers to your request. It does that by looping over all inputs in the form and appending each key value pair to the JSON object, like so: It simply uses the object FormData to construct a set of key-value pairs in JSON format. This is how it looks like:Īs you might have noticed, we called the function buildJsonFormData. The function will build the JSON body, build the request headers, and use the fetch service to POST the request by the submitted the form. We will add an Id to the form and we will register an event listener to it, like so: Step 2: The form Id and the event listener We will create a simple form that will be submitted to JSON PlaceHolder as an example.



