Tiptap Schemas
Unlike many other editors, Tiptap is based on a schema that defines how your content is structured. That enables you to define the kind of nodes that may occur in the document, its attributes and the way they can be nested.
This schema is very strict. You can’t use any HTML element or attribute that is not defined in your schema.
Let me give you one example: If you paste something like This is <strong>important</strong>
into Tiptap, but don’t have any extension that handles strong
tags, you’ll only see This is important
– without the strong tags.
If you want to know when this happens, you can listen to the contentError
event after enabling the enableContentCheck
option.
How a schema looks like
When you’ll work with the provided extensions only, you don’t have to care that much about the schema. If you’re building your own extensions, it’s probably helpful to understand how the schema works. Let’s look at the most simple schema for a typical ProseMirror editor:
// the underlying ProseMirror schema
{
nodes: {
doc: {
content: 'block+',
},
paragraph: {
content: 'inline*',
group: 'block',
parseDOM: [{ tag: 'p' }],
toDOM: () => ['p', 0],
},
text: {
group: 'inline',
},
},
}
We register three nodes here. doc
, paragraph
and text
. doc
is the root node which allows one or more block nodes as children (content: 'block+'
). Since paragraph
is in the group of block nodes (group: 'block'
) our document can only contain paragraphs. Our paragraphs allow zero or more inline nodes as children (content: 'inline*'
) so there can only be text
in it. parseDOM
defines how a node can be parsed from pasted HTML. toDOM
defines how it will be rendered in the DOM.
In Tiptap every node, mark and extension is living in its own file. This allows us to split the logic. Under the hood the whole schema will be merged together:
// the Tiptap schema API
import { Node } from '@tiptap/core'
const Document = Node.create({
name: 'doc',
topNode: true,
content: 'block+',
})
const Paragraph = Node.create({
name: 'paragraph',
group: 'block',
content: 'inline*',
parseHTML() {
return [{ tag: 'p' }]
},
renderHTML({ HTMLAttributes }) {
return ['p', HTMLAttributes, 0]
},
})
const Text = Node.create({
name: 'text',
group: 'inline',
})
Nodes and marks
Differences
Nodes are like blocks of content, for example paragraphs, headings, code blocks, blockquotes and many more.
Marks can be applied to specific parts of a node. That’s the case for bold, italic or striked text. Links are marks, too.
The node schema
Content
The content attribute defines exactly what kind of content the node can have. ProseMirror is really strict with that. That means, content which doesn’t fit the schema is thrown away. It expects a name or group as a string. Here are a few examples:
Node.create({
// must have one or more blocks
content: 'block+',
// must have zero or more blocks
content: 'block*',
// allows all kinds of 'inline' content (text or hard breaks)
content: 'inline*',
// must not have anything else than 'text'
content: 'text*',
// can have one or more paragraphs, or lists (if lists are used)
content: '(paragraph|list?)+',
// must have exact one heading at the top, and one or more blocks below
content: 'heading block+',
})
Marks
You can define which marks are allowed inside of a node with the marks
setting of the schema. Add a one or more names or groups of marks, allow all or disallow all marks like this:
Node.create({
// allows only the 'bold' mark
marks: 'bold',
// allows only the 'bold' and 'italic' marks
marks: 'bold italic',
// allows all marks
marks: '_',
// disallows all marks
marks: '',
})
Group
Add this node to a group of extensions, which can be referred to in the content attribute of the schema.
Node.create({
// add to 'block' group
group: 'block',
// add to 'inline' group
group: 'inline',
// add to 'block' and 'list' group
group: 'block list',
})
Inline
Nodes can be rendered inline, too. When setting inline: true
nodes are rendered in line with the text. That’s the case for mentions. The result is more like a mark, but with the functionality of a node. One difference is the resulting JSON document. Multiple marks are applied at once, inline nodes would result in a nested structure.
Node.create({
// renders nodes in line with the text, for example
inline: true,
})
For some cases where you want features that aren’t available in marks, for example a node view, try if an inline node would work:
Node.create({
name: 'customInlineNode',
group: 'inline',
inline: true,
content: 'text*',
})
Inline nodes can be tricky to select, especially at line edges. A quick fix: add a zero-width space right after the element using CSS:
.customInlineNode::after {
content: "\200B";
}
Atom
Nodes with atom: true
aren’t directly editable and should be treated as a single unit. It’s not so likely to use that in a editor context, but this is how it would look like:
Node.create({
atom: true,
})
One example is the Mention
extension, which somehow looks like text, but behaves more like a single unit. As this doesn’t have editable text content, it’s empty when you copy such node. Good news though, you can control that. Here is the example from the Mention
extension:
// Used to convert an atom node to plain text
renderText({ node }) {
return `@${node.attrs.id}`
},
Selectable
Besides the already visible text selection, there is an invisible node selection. If you want to make your nodes selectable, you can configure it like this:
Node.create({
selectable: true,
})
Draggable
All nodes can be configured to be draggable (by default they aren’t) with this setting:
Node.create({
draggable: true,
})
Code
Users expect code to behave very differently. For all kind of nodes containing code, you can set code: true
to take this into account.
Node.create({
code: true,
})
Whitespace
Controls way whitespace in this a node is parsed.
Node.create({
whitespace: 'pre',
})
Defining
Nodes get dropped when their entire content is replaced (for example, when pasting new content) by default. If a node should be kept for such replace operations, configure them as defining
.
Typically, that applies to Blockquote
, CodeBlock
, Heading
, and ListItem
.
Node.create({
defining: true,
})
Isolating
For nodes that should fence the cursor for regular editing operations like backspacing, for example a TableCell, set isolating: true
.
Node.create({
isolating: true,
})
Allow gap cursor
The Gapcursor
extension registers a new schema attribute to control if gap cursors are allowed everywhere in that node.
Node.create({
allowGapCursor: false,
})
Table roles
The Table
extension registers a new schema attribute to configure which role an Node has. Allowed values are table
, row
, cell
, and header_cell
.
Node.create({
tableRole: 'cell',
})
The mark schema
Inclusive
If you don’t want the mark to be active when the cursor is at its end, set inclusive to false
. For example, that’s how it’s configured for Link
marks:
Mark.create({
inclusive: false,
})
Excludes
By default all marks can be applied at the same time. With the excludes attribute you can define which marks must not coexist with the mark. For example, the inline code mark excludes any other mark (bold, italic, and all others).
Mark.create({
// must not coexist with the bold mark
excludes: 'bold'
// exclude any other mark
excludes: '_',
})
Exitable
By default a mark will "trap" the cursor, meaning the cursor can't get out of the mark except by moving the cursor left to right into text without a mark. If this is set to true, the mark will be exitable when the mark is at the end of a node. This is handy for example using code marks.
Mark.create({
// make this mark exitable - default is false
exitable: true,
})
Group
Add this mark to a group of extensions, which can be referred to in the content attribute of the schema.
Mark.create({
// add this mark to the 'basic' group
group: 'basic',
// add this mark to the 'basic' and the 'foobar' group
group: 'basic foobar',
})
Code
Users expect code to behave very differently. For all kind of marks containing code, you can set code: true
to take this into account.
Mark.create({
code: true,
})
Spanning
By default marks can span multiple nodes when rendered as HTML. Set spanning: false
to indicate that a mark must not span multiple nodes.
Mark.create({
spanning: false,
})
Get the underlying ProseMirror schema
There are a few use cases where you need to work with the underlying schema. You’ll need that if you’re using the Tiptap collaborative text editing features or if you want to manually render your content as HTML.
Option 1: With an Editor
If you need this on the client side and need an editor instance anyway, it’s available through the editor:
import { Editor } from '@tiptap/core'
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
const editor = new Editor({
extensions: [
Document,
Paragraph,
Text,
// add more extensions here
])
})
const schema = editor.schema
Option 2: Without an Editor
If you just want to have the schema without initializing an actual editor, you can use the getSchema
helper function. It needs an array of available extensions and conveniently generates a ProseMirror schema for you:
import { getSchema } from '@tiptap/core'
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
const schema = getSchema([
Document,
Paragraph,
Text,
// add more extensions here
])
Invalid Schema Handling
To track and respond to content errors, Tiptap supports checking that the content provided matches the schema derived from the registered extensions.
To use this, set the enableContentCheck
option to true
, which activates checking the content and emitting contentError
events.
These events can be listened to with the onContentError
callback.
By default, this flag is set to false
to maintain compatibility with previous versions.
Note
The content checking that Tiptap runs is 100% accurate on JSON content types. But, if you provide your content as HTML, we have done our best to try to alert on missing nodes but marks can be missed in certain situations, therefore, falling back to the default behavior of stripping that unrecognized content by default.
contentError event
The contentError
event is emitted when the initial content
provided during editor setup is incompatible with the schema.
As part of the error context, you are provided with a disableCollaboration
function. Invoking this function reinitializes the editor without the collaboration extension, ensuring that any removed content is not synchronized with other users.
This event can be handled either directly as an option through onContentError
like:
new Editor({
enableContentCheck: true,
content: invalidContent,
onContentError({ editor, error, disableCollaboration }) {
// your handler here
},
...options,
})
Or, by attaching a listener to the contentError
event on the editor instance.
const editor = new Editor({
enableContentCheck: true,
content: invalidContent,
...options,
})
editor.on('contentError', ({ editor, error, disableCollaboration }) => {
// your handler here
})
For more implementation examples, refer to the events section.
Recommended Handling
How you handle schema errors will be specific to your application and requirements but, here are our suggestions:
Without collaborative editing
Depending on your use case, the default behavior of stripping unknown content keeps your content in a known valid state for future editing.
With collaborative editing
Depending on your use case, you may want to set the enableContentCheck
flag and listen to contentError
events. When this event is received, you may want to respond similarly to this example:
onContentError({ editor, error, disableCollaboration }) {
// Removes the collaboration extension.
disableCollaboration()
// Since the content is invalid, we don't want to emit an update
// Preventing synchronization with other editors or to a server
const emitUpdate = false
// Disable the editor to prevent further user input
editor.setEditable(false, emitUpdate)
// Maybe show a notification to the user that they need to refresh the app
}