-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
base: next
Are you sure you want to change the base?
Conversation
|
✅ Deploy Preview for tiptap-embed ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
2162826
to
64dae06
Compare
const schema = getSchema([StarterKit]) | ||
|
||
console.log( | ||
renderToStaticMarkup( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a demo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Awesome idea @nperez0111
@tiptap/static-renderer
to enable static rendering of content
Just as a note, if we render to something like jsx-dom then we can render directly to HTMLElements with pure JS & prosemirror |
@nperez0111 I am super excited about this PR (finally took a look at it over the weekend). I tried building it locally today to test it out, and it failed because there was no Edit: Ah, its the same error currently failing the build CI step. You are probably already aware of this then! |
Yep, this isn't completely prime-time yet. Needs a lot more tests to ensure things actually work |
474ff43
to
21fa3aa
Compare
@nperez0111 I'll remove my review - let me check if I should review in the future. |
5645687
to
bc7ad50
Compare
@tiptap/core
@tiptap/extension-blockquote
@tiptap/extension-bubble-menu
@tiptap/extension-bold
@tiptap/extension-bullet-list
@tiptap/extension-character-count
@tiptap/extension-code
@tiptap/extension-code-block
@tiptap/extension-code-block-lowlight
@tiptap/extension-collaboration
@tiptap/extension-collaboration-cursor
@tiptap/extension-color
@tiptap/extension-document
@tiptap/extension-dropcursor
@tiptap/extension-floating-menu
@tiptap/extension-focus
@tiptap/extension-font-family
@tiptap/extension-gapcursor
@tiptap/extension-hard-break
@tiptap/extension-heading
@tiptap/extension-highlight
@tiptap/extension-history
@tiptap/extension-horizontal-rule
@tiptap/extension-image
@tiptap/extension-italic
@tiptap/extension-link
@tiptap/extension-list-item
@tiptap/extension-list-keymap
@tiptap/extension-mention
@tiptap/extension-ordered-list
@tiptap/extension-paragraph
@tiptap/extension-placeholder
@tiptap/extension-strike
@tiptap/extension-subscript
@tiptap/extension-superscript
@tiptap/extension-table-cell
@tiptap/extension-table
@tiptap/extension-table-header
@tiptap/extension-table-row
@tiptap/extension-task-item
@tiptap/extension-task-list
@tiptap/extension-text
@tiptap/extension-text-align
@tiptap/extension-text-style
@tiptap/extension-underline
@tiptap/extension-typography
@tiptap/extension-youtube
@tiptap/html
@tiptap/pm
@tiptap/react
@tiptap/starter-kit
@tiptap/static-renderer
@tiptap/suggestion
@tiptap/vue-2
@tiptap/vue-3
commit: |
…ing & React Elements
479d895
to
d42fb8e
Compare
I think there is a bug when using namespaces: this should work: renderHTML({ HTMLAttributes, node }) {
const { iconValue, title } = node.attrs
return [
[
"div",
{
"data-type": ALERT_ICON_NODE,
},
[
"http://www.w3.org/2000/svg svg",
{
width: 24,
height: 24,
"stroke-width": 2,
stroke: "currentColor",
"stroke-linecap": "round",
fill: "none",
},
["title", {}, iconValue],
[
"use",
{ "http://www.w3.org/1999/xlink xlink:href": `#${iconValue}` },
],
],
],
] as any
} Specifically, the |
Welp, didn't know about that feature, I'll have to get back to you on this |
@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.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:
Render to a React component:
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
renderToHTMLString
Optionsextensions
: 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
renderToReactElement
Optionsextensions
: 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: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: