Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(static-renderer): add @tiptap/static-renderer to enable static rendering of content #5528

Merged
merged 34 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
91af037
feat: allowing specifying the content of ReacNodeViewContent via a Re…
nperez0111 Aug 20, 2024
0e11ced
refactor: export `resolveExtensions` function
nperez0111 Aug 23, 2024
3c9a9e5
feat: first version of a static renderer
nperez0111 Aug 23, 2024
3958f52
feat(static-renderer): firm up the API and have it render to HTML Str…
nperez0111 Aug 27, 2024
a5ab9da
chore: minor change
nperez0111 Dec 31, 2024
dbf3682
docs: more examples
nperez0111 Aug 27, 2024
2507e8b
fix: better handling of unhandledNode & unhandledMark
nperez0111 Aug 27, 2024
fcad731
chore: add export
nperez0111 Sep 25, 2024
580db92
feat: add support for providing the current node and parent node to m…
nperez0111 Nov 21, 2024
26da70c
build: setup package to be built by tsup instead
nperez0111 Dec 4, 2024
b35e8d2
build: fix build
nperez0111 Dec 31, 2024
f83b060
chore: make package public
nperez0111 Dec 4, 2024
2719989
feat(static-renderer): add markdown output support
nperez0111 Dec 31, 2024
10b9ffb
fix: access nodeOrMark's type safely across JSON & Prosemirror objects
nperez0111 Dec 18, 2024
d66bab4
chore: widen the type to represent the actual representation
nperez0111 Dec 18, 2024
24c1009
feat(static-renderer): add support for namespacing in DOMOutputSpec
nperez0111 Dec 30, 2024
ec95ac0
feat: move types into core package
nperez0111 Dec 31, 2024
df3d708
test: add tests for static renderer
nperez0111 Dec 31, 2024
33813ae
refactor: minor cleanup
nperez0111 Dec 31, 2024
5ad7ca9
test: update tests
nperez0111 Dec 31, 2024
5653a72
chore: build all packages
nperez0111 Dec 31, 2024
74a54e9
Merge branch 'next' into node-view-content
nperez0111 Jan 6, 2025
09689b1
Merge branch 'next' into node-view-content
nperez0111 Jan 6, 2025
e21f0cd
build: update build
nperez0111 Jan 6, 2025
5d9f6dc
test: add demos and tests to demos
nperez0111 Jan 6, 2025
ef107b2
Merge branch 'next' into node-view-content
nperez0111 Jan 6, 2025
542b01d
chore: fix for react
nperez0111 Jan 6, 2025
ffe5b9a
docs: add demo for static renderer
nperez0111 Jan 6, 2025
addb3da
chore: lint
nperez0111 Jan 6, 2025
5e41d0a
chore: put back file
nperez0111 Jan 6, 2025
c0824a4
fix: export markdown
nperez0111 Jan 6, 2025
1249694
test: update path resolution for static-renderer
nperez0111 Jan 6, 2025
97ad2d1
docs: add a more complex react example
nperez0111 Jan 6, 2025
639e0d7
chore: add changeset of static-renderer docs
nperez0111 Jan 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
273 changes: 273 additions & 0 deletions .changeset/blue-shrimps-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
---
'@tiptap/static-renderer': major
---

# @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: '<p>Hello World!</p>'
```

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: '<p>Hello World!</p>'
```

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 (
<div className='custom-component-without-content' onClick={() => setCount(a => a + 1)}>
{count} This is a react component!
</div>
)
}

const CustomNodeExtensionWithoutContent = Node.create({
name: 'customNodeExtensionWithoutContent',
atom: true,
renderHTML() {
return ['div', { class: 'my-custom-component-without-content' }] as const
},
addNodeView() {
return ReactNodeViewRenderer(MyCustomComponentWithoutContent)
},
})

renderToReactElement({
extensions: [StarterKit, CustomNodeExtensionWithoutContent],
options: {
nodeMapping: {
// render the custom node with the intended node view React component
customNodeExtensionWithoutContent: MyCustomComponentWithoutContent,
},
},
content: {
type: 'doc',
content: [
{
type: 'customNodeExtensionWithoutContent',
},
],
},
})
// returns: <div class="my-custom-component-without-content">200 This is a react component!</div>
```

But what if you want to render the rich text content of the node view? You can do that by providing a `NodeViewContent` component as a child of the node view component:

```js
import { Node } from '@tiptap/core'
import {
NodeViewContent,
ReactNodeViewContentProvider,
ReactNodeViewRenderer
} from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit'
import { renderToReactElement } from '@tiptap/static-renderer'


// This component does have a NodeViewContent, so it will render it's children's rich text content
function MyCustomComponentWithContent() {
return (
<div className="custom-component-with-content">
Custom component with content in React!
<NodeViewContent />
</div>
)
}


const CustomNodeExtensionWithContent = Node.create({
name: 'customNodeExtensionWithContent',
content: 'text*',
group: 'block',
renderHTML() {
return ['div', { class: 'my-custom-component-with-content' }, 0] as const
},
addNodeView() {
return ReactNodeViewRenderer(MyCustomComponentWithContent)
},
})


renderToReactElement({
extensions: [StarterKit, CustomNodeExtensionWithContent],
options: {
nodeMapping: {
customNodeExtensionWithContent: ({ children }) => {
// To pass the content down into the NodeViewContent component, we need to wrap the custom component with the ReactNodeViewContentProvider
return (
<ReactNodeViewContentProvider content={children}>
<MyCustomComponentWithContent />
</ReactNodeViewContentProvider>
)
},
},
},
content: {
type: 'doc',
content: [
{
type: 'customNodeExtensionWithContent',
// rich text content
content: [
{
type: 'text',
text: 'Hello, world!',
},
],
},
],
},
})

// returns: <div class="custom-component-with-content">Custom component with content in React!<div data-node-view-content="" style="white-space:pre-wrap">Hello, world!</div></div>
// Note: The NodeViewContent component is rendered as a div with the attribute data-node-view-content, and the rich text content is rendered inside of it
```
8 changes: 4 additions & 4 deletions .changeset/dirty-colts-shave.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
"@tiptap/extension-table-header": minor
"@tiptap/extension-table-cell": minor
"@tiptap/extension-table-row": minor
"@tiptap/extension-table": minor
'@tiptap/extension-table-header': minor
'@tiptap/extension-table-cell': minor
'@tiptap/extension-table-row': minor
'@tiptap/extension-table': minor
---

This change repackages all of the table extensions to be within the `@tiptap/extension-table` package (other packages are just a re-export of the `@tiptap/extension-table` package). It also adds the `TableKit` export which will allow configuring the entire table with one extension.
2 changes: 1 addition & 1 deletion .changeset/fair-jars-shout.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"@tiptap/extension-text-style": patch
'@tiptap/extension-text-style': patch
---

The text-style extension, now will match elements with a style tag, but not consume them to allow other elements to match [per this comment](https://github.com/ueberdosis/tiptap/discussions/5912#discussioncomment-11716337).
Empty file.
12 changes: 12 additions & 0 deletions demos/src/Examples/StaticRendering/React/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
context('/src/Examples/StaticRendering/React/', () => {
beforeEach(() => {
cy.visit('/src/Examples/StaticRendering/React/')
})

it('should have a working tiptap instance', () => {
cy.get('.tiptap').then(([{ editor }]) => {
// eslint-disable-next-line
expect(editor).to.not.be.null
})
})
})
Loading
Loading