# Change Log ## 3.0.0-beta.7 ### Patch Changes - Updated dependencies [d0fda30] - @tiptap/core@3.0.0-beta.7 - @tiptap/pm@3.0.0-beta.7 ## 3.0.0-beta.6 ### Patch Changes - @tiptap/core@3.0.0-beta.6 - @tiptap/pm@3.0.0-beta.6 ## 3.0.0-beta.5 ### Patch Changes - 8c69002: Synced beta with stable features - Updated dependencies [8c69002] - Updated dependencies [62b0877] - @tiptap/core@3.0.0-beta.5 - @tiptap/pm@3.0.0-beta.5 ## 3.0.0-beta.4 ### Patch Changes - Updated dependencies [5e957e5] - Updated dependencies [9f207a6] - @tiptap/core@3.0.0-beta.4 - @tiptap/pm@3.0.0-beta.4 ## 3.0.0-beta.3 ### Patch Changes - 1b4c82b: We are now using pnpm package aliases for versions to enable better version pinning for the monorepository - Updated dependencies [1b4c82b] - @tiptap/core@3.0.0-beta.3 - @tiptap/pm@3.0.0-beta.3 ## 3.0.0-beta.2 ## 3.0.0-beta.1 ## 3.0.0-beta.0 ## 3.0.0-next.8 ## 3.0.0-next.7 ### Patch Changes - 89bd9c7: Enforce type imports so that the bundler ignores TypeScript type imports when generating the index.js file of the dist directory ## 3.0.0-next.6 ### Major Changes - 6a53bb2: # @tiptap/static-renderer The `@tiptap/static-renderer` package provides a way to render a Tiptap/ProseMirror document to any target format, like an HTML string, a React component, or even markdown. It does so, by taking the original JSON of a document (or document partial) and attempts to map this to the output format, by matching against a list of nodes & marks. ## Why Static Render? The main use case for static rendering is to render a Tiptap/ProseMirror document on the server-side, for example in a Next.js or Nuxt.js application. This way, you can render the content of your editor to HTML before sending it to the client, which can improve the performance of your application. Another use case is to render the content of your editor to another format like markdown, which can be useful if you want to send it to a markdown-based API. But what makes it static? The static renderer doesn't require a browser or a DOM to render the content. It's a pure JavaScript function that takes a document (as JSON or Prosemirror Node instance) and returns the target format back. ## Example Render a Tiptap document to an HTML string: ```js import StarterKit from '@tiptap/starter-kit' import { renderToHTMLString } from '@tiptap/static-renderer' renderToHTMLString({ extensions: [StarterKit], // using your extensions // we can map nodes and marks to HTML elements options: { nodeMapping: { // custom node mappings }, markMapping: { // custom mark mappings }, unhandledNode: ({ node }) => { // handle unhandled nodes return `[unknown node ${node.type.name}]` }, unhandledMark: ({ mark }) => { // handle unhandled marks return `[unknown node ${mark.type.name}]` }, }, // the source content to render content: { type: 'doc', content: [ { type: 'paragraph', content: [ { type: 'text', text: 'Hello World!', }, ], }, ], }, }) // returns: '
Hello World!
' ``` Render to a React component: ```js import StarterKit from '@tiptap/starter-kit' import { renderToReactElement } from '@tiptap/static-renderer' renderToReactElement({ extensions: [StarterKit], // using your extensions // we can map nodes and marks to HTML elements options: { nodeMapping: { // custom node mappings }, markMapping: { // custom mark mappings }, unhandledNode: ({ node }) => { // handle unhandled nodes return `[unknown node ${node.type.name}]` }, unhandledMark: ({ mark }) => { // handle unhandled marks return `[unknown node ${mark.type.name}]` }, }, // the source content to render content: { type: 'doc', content: [ { type: 'paragraph', content: [ { type: 'text', text: 'Hello World!', }, ], }, ], }, }) // returns a react node that, when evaluated, would be equivalent to: 'Hello World!
' ``` There are a number of options available to customize the output, like custom node and mark mappings, or handling unhandled nodes and marks. ## API ### `renderToHTMLString` ```ts function renderToHTMLString(options: { extensions: Extension[] content: ProsemirrorNode | JSONContent options?: TiptapHTMLStaticRendererOptions }): string ``` #### `renderToHTMLString` Options - `extensions`: An array of Tiptap extensions that are used to render the content. - `content`: The content to render. Can be a Prosemirror Node instance or a JSON representation of a Prosemirror document. - `options`: An object with additional options. - `options.nodeMapping`: An object that maps Prosemirror nodes to HTML strings. - `options.markMapping`: An object that maps Prosemirror marks to HTML strings. - `options.unhandledNode`: A function that is called when an unhandled node is encountered. - `options.unhandledMark`: A function that is called when an unhandled mark is encountered. ### `renderToReactElement` ```ts function renderToReactElement(options: { extensions: Extension[] content: ProsemirrorNode | JSONContent options?: TiptapReactStaticRendererOptions }): ReactElement ``` #### `renderToReactElement` Options - `extensions`: An array of Tiptap extensions that are used to render the content. - `content`: The content to render. Can be a Prosemirror Node instance or a JSON representation of a Prosemirror document. - `options`: An object with additional options. - `options.nodeMapping`: An object that maps Prosemirror nodes to React components. - `options.markMapping`: An object that maps Prosemirror marks to React components. - `options.unhandledNode`: A function that is called when an unhandled node is encountered. - `options.unhandledMark`: A function that is called when an unhandled mark is encountered. ## How does it work? Each Tiptap node/mark extension can define a `renderHTML` method which is used to generate default mappings of Prosemirror nodes/marks to the target format. These can be overridden by providing custom mappings in the options. One thing to note is that the static renderer doesn't support node views automatically, so you need to provide a mapping for each node type that you want rendered as a node view. Here is an example of how you can render a node view as a React component: ```js import { Node } from '@tiptap/core' import { ReactNodeViewRenderer } from '@tiptap/react' import StarterKit from '@tiptap/starter-kit' import { renderToReactElement } from '@tiptap/static-renderer' // This component does not have a NodeViewContent, so it does not render it's children's rich text content function MyCustomComponentWithoutContent() { const [count, setCount] = React.useState(200) return (Hello World!
' ``` Render to a React component: ```js import StarterKit from '@tiptap/starter-kit' import { renderToReactElement } from '@tiptap/static-renderer' renderToReactElement({ extensions: [StarterKit], // using your extensions // we can map nodes and marks to HTML elements options: { nodeMapping: { // custom node mappings }, markMapping: { // custom mark mappings }, unhandledNode: ({ node }) => { // handle unhandled nodes return `[unknown node ${node.type.name}]` }, unhandledMark: ({ mark }) => { // handle unhandled marks return `[unknown node ${mark.type.name}]` }, }, // the source content to render content: { type: 'doc', content: [ { type: 'paragraph', content: [ { type: 'text', text: 'Hello World!', }, ], }, ], }, }) // returns a react node that, when evaluated, would be equivalent to: 'Hello World!
' ``` There are a number of options available to customize the output, like custom node and mark mappings, or handling unhandled nodes and marks. ## API ### `renderToHTMLString` ```ts function renderToHTMLString(options: { extensions: Extension[] content: ProsemirrorNode | JSONContent options?: TiptapHTMLStaticRendererOptions }): string ``` #### `renderToHTMLString` Options - `extensions`: An array of Tiptap extensions that are used to render the content. - `content`: The content to render. Can be a Prosemirror Node instance or a JSON representation of a Prosemirror document. - `options`: An object with additional options. - `options.nodeMapping`: An object that maps Prosemirror nodes to HTML strings. - `options.markMapping`: An object that maps Prosemirror marks to HTML strings. - `options.unhandledNode`: A function that is called when an unhandled node is encountered. - `options.unhandledMark`: A function that is called when an unhandled mark is encountered. ### `renderToReactElement` ```ts function renderToReactElement(options: { extensions: Extension[] content: ProsemirrorNode | JSONContent options?: TiptapReactStaticRendererOptions }): ReactElement ``` #### `renderToReactElement` Options - `extensions`: An array of Tiptap extensions that are used to render the content. - `content`: The content to render. Can be a Prosemirror Node instance or a JSON representation of a Prosemirror document. - `options`: An object with additional options. - `options.nodeMapping`: An object that maps Prosemirror nodes to React components. - `options.markMapping`: An object that maps Prosemirror marks to React components. - `options.unhandledNode`: A function that is called when an unhandled node is encountered. - `options.unhandledMark`: A function that is called when an unhandled mark is encountered. ## How does it work? Each Tiptap node/mark extension can define a `renderHTML` method which is used to generate default mappings of Prosemirror nodes/marks to the target format. These can be overridden by providing custom mappings in the options. One thing to note is that the static renderer doesn't support node views automatically, so you need to provide a mapping for each node type that you want rendered as a node view. Here is an example of how you can render a node view as a React component: ```js import { Node } from '@tiptap/core' import { ReactNodeViewRenderer } from '@tiptap/react' import StarterKit from '@tiptap/starter-kit' import { renderToReactElement } from '@tiptap/static-renderer' // This component does not have a NodeViewContent, so it does not render it's children's rich text content function MyCustomComponentWithoutContent() { const [count, setCount] = React.useState(200) return (