tiptap/docs/api/schema.md

356 lines
10 KiB
Markdown
Raw Normal View History

---
tableOfContents: true
---
2020-04-20 05:50:55 +08:00
# Schema
## Introduction
2021-10-20 04:30:45 +08:00
Unlike many other editors, Tiptap is based on a [schema](https://prosemirror.net/docs/guide/#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.
2020-04-20 05:50:55 +08:00
2020-09-04 18:35:31 +08:00
This schema is *very* strict. You cant use any HTML element or attribute that is not defined in your schema.
2021-10-20 04:30:45 +08:00
Let me give you one example: If you paste something like `This is <strong>important</strong>` into Tiptap, but dont have any extension that handles `strong` tags, youll only see `This is important` without the strong tags.
2020-04-20 20:07:20 +08:00
2020-04-23 21:37:19 +08:00
## How a schema looks like
2020-11-05 21:39:28 +08:00
When youll work with the provided extensions only, you dont have to care that much about the schema. If youre building your own extensions, its probably helpful to understand how the schema works. Lets look at the most simple schema for a typical ProseMirror editor:
2020-04-23 21:37:19 +08:00
```js
2020-11-05 21:39:28 +08:00
// the underlying ProseMirror schema
2020-04-23 21:37:19 +08:00
{
nodes: {
document: {
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.
2020-04-24 15:57:51 +08:00
2021-10-20 04:30:45 +08:00
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:
2020-04-23 21:37:19 +08:00
```js
2021-10-20 04:30:45 +08:00
// the Tiptap schema API
import { Node } from '@tiptap/core'
2020-09-26 04:22:44 +08:00
const Document = Node.create({
name: 'doc',
2020-11-05 21:39:28 +08:00
topNode: true,
content: 'block+',
2020-09-26 04:22:44 +08:00
})
const Paragraph = Node.create({
2020-11-05 21:39:28 +08:00
name: 'paragraph',
group: 'block',
content: 'inline*',
parseHTML() {
return [
{ tag: 'p' },
]
},
2020-11-16 17:03:12 +08:00
renderHTML({ HTMLAttributes }) {
return ['p', HTMLAttributes, 0]
2020-11-05 21:39:28 +08:00
},
})
2020-09-26 04:22:44 +08:00
const Text = Node.create({
2020-11-05 21:39:28 +08:00
name: 'text',
group: 'inline',
})
2020-09-26 04:22:44 +08:00
```
2020-11-05 21:39:28 +08:00
## Nodes and marks
2020-09-26 04:22:44 +08:00
2020-11-05 21:39:28 +08:00
### Differences
Nodes are like blocks of content, for example paragraphs, headings, code blocks, blockquotes and many more.
2020-09-26 04:22:44 +08:00
2020-11-05 21:39:28 +08:00
Marks can be applied to specific parts of a node. Thats the case for **bold**, *italic* or ~~striked~~ text. [Links](#) are marks, too.
2020-10-30 23:13:47 +08:00
2020-11-05 21:39:28 +08:00
### The node schema
2020-10-30 23:13:47 +08:00
#### Content
2020-11-05 23:02:12 +08:00
The content attribute defines exactly what kind of content the node can have. ProseMirror is really strict with that. That means, content which doesnt fit the schema is thrown away. It expects a name or group as a string. Here are a few examples:
```js
Node.create({
// must have one or more blocks
2020-11-05 23:02:12 +08:00
content: 'block+',
// must have zero or more blocks
content: 'block*',
2020-11-05 23:02:12 +08:00
// 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+'
2020-11-05 23:02:12 +08:00
})
```
2020-10-30 23:13:47 +08:00
#### Marks
2020-11-06 00:01:25 +08:00
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:
2020-10-30 23:13:47 +08:00
2020-11-05 23:02:12 +08:00
```js
Node.create({
2020-11-06 00:01:25 +08:00
// allows only the 'bold' mark
2020-11-05 23:02:12 +08:00
marks: 'bold',
2020-11-06 00:01:25 +08:00
// allows only the 'bold' and 'italic' marks
marks: 'bold italic',
2020-11-05 23:02:12 +08:00
// allows all marks
marks: '_',
// disallows all marks
marks: '',
})
```
2020-10-30 23:13:47 +08:00
#### Group
2020-11-06 00:01:25 +08:00
Add this node to a group of extensions, which can be referred to in the [content](#content) attribute of the schema.
2020-10-30 23:13:47 +08:00
2020-11-05 23:02:12 +08:00
```js
Node.create({
2020-11-05 23:02:12 +08:00
// add to 'block' group
group: 'block',
// add to 'inline' group
group: 'inline',
// add to 'block' and 'list' group
group: 'block list',
})
```
2020-10-30 23:13:47 +08:00
#### Inline
2020-11-06 00:01:25 +08:00
Nodes can be rendered inline, too. When setting `inline: true` nodes are rendered in line with the text. Thats 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.
2020-10-30 23:13:47 +08:00
2020-11-05 23:02:12 +08:00
```js
Node.create({
2020-11-06 00:01:25 +08:00
// renders nodes in line with the text, for example
2020-11-05 23:02:12 +08:00
inline: true,
})
```
2021-02-02 21:08:45 +08:00
For some cases where you want features that arent available in marks, for example a node view, try if an inline node would work:
```js
Node.create({
name: 'customInlineNode',
group: 'inline',
inline: true,
content: 'text*',
})
```
2020-10-30 23:13:47 +08:00
#### Atom
2020-11-06 00:01:25 +08:00
Nodes with `atom: true` arent directly editable and should be treated as a single unit. Its not so likely to use that in a editor context, but this is how it would look like:
2020-10-30 23:13:47 +08:00
2020-11-05 23:02:12 +08:00
```js
Node.create({
2020-11-05 23:02:12 +08:00
atom: true,
})
```
2021-01-21 00:58:53 +08:00
One example is the [`Mention`](/api/nodes/mention) extension, which somehow looks like text, but behaves more like a single unit. As this doesnt have editable text content, its empty when you copy such node. Good news though, you can control that. Here is the example from the [`Mention`](/api/nodes/mention) extension:
```js
// Used to convert an atom node to plain text
renderText({ node }) {
return `@${node.attrs.id}`
},
```
2020-10-30 23:13:47 +08:00
#### Selectable
2020-11-12 22:50:24 +08:00
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:
2020-10-30 23:13:47 +08:00
2020-11-05 23:02:12 +08:00
```js
Node.create({
2020-11-05 23:02:12 +08:00
selectable: true,
})
```
2020-10-30 23:13:47 +08:00
#### Draggable
2020-11-06 00:01:25 +08:00
All nodes can be configured to be draggable (by default they arent) with this setting:
2020-10-30 23:13:47 +08:00
2020-11-05 23:02:12 +08:00
```js
Node.create({
2020-11-05 23:02:12 +08:00
draggable: true,
})
```
2020-10-30 23:13:47 +08:00
#### Code
2020-11-06 00:01:25 +08:00
Users expect code to behave very differently. For all kind of nodes containing code, you can set `code: true` to take this into account.
2020-10-30 23:13:47 +08:00
2020-11-05 23:02:12 +08:00
```js
Node.create({
2020-11-05 23:02:12 +08:00
code: true,
})
```
2022-01-04 17:02:24 +08:00
#### Whitespace
Controls way whitespace in this a node is parsed.
```js
Node.create({
whitespace: 'pre',
})
```
2020-10-30 23:13:47 +08:00
#### Defining
2020-11-12 22:50:24 +08:00
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`.
2020-11-16 18:34:24 +08:00
Typically, that applies to [`Blockquote`](/api/nodes/blockquote), [`CodeBlock`](/api/nodes/code-block), [`Heading`](/api/nodes/heading), and [`ListItem`](/api/nodes/list-item).
2020-10-30 23:13:47 +08:00
2020-11-05 23:02:12 +08:00
```js
Node.create({
2020-11-05 23:02:12 +08:00
defining: true,
})
```
2020-10-30 23:13:47 +08:00
#### Isolating
2020-11-06 00:01:25 +08:00
For nodes that should fence the cursor for regular editing operations like backspacing, for example a TableCell, set `isolating: true`.
2020-10-30 23:13:47 +08:00
2020-11-05 23:02:12 +08:00
```js
Node.create({
2020-11-05 23:02:12 +08:00
isolating: true,
})
```
#### Allow gap cursor
2021-02-09 00:39:50 +08:00
The [`Gapcursor`](/api/extensions/gapcursor) extension registers a new schema attribute to control if gap cursors are allowed everywhere in that node.
```js
Node.create({
allowGapCursor: false,
})
```
#### Table roles
2021-02-25 04:08:54 +08:00
The [`Table`](/api/nodes/table) extension registers a new schema attribute to configure which role an Node has. Allowed values are `table`, `row`, `cell`, and `header_cell`.
```js
Node.create({
tableRole: 'cell',
})
```
2020-11-05 21:39:28 +08:00
### The mark schema
2020-10-30 23:13:47 +08:00
#### Inclusive
2020-11-06 00:01:25 +08:00
If you dont want the mark to be active when the cursor is at its end, set inclusive to `false`. For example, thats how its configured for [`Link`](/api/marks/link) marks:
```js
Mark.create({
2020-11-06 00:01:25 +08:00
inclusive: false,
})
```
2020-10-30 23:13:47 +08:00
#### Excludes
2020-11-06 00:01:25 +08:00
By default all nodes 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).
2020-10-30 23:13:47 +08:00
2020-11-06 00:01:25 +08:00
```js
Mark.create({
2020-11-06 00:01:25 +08:00
// must not coexist with the bold mark
excludes: 'bold'
// exclude any other mark
excludes: '_',
})
```
2020-10-30 23:13:47 +08:00
#### Exitable
2022-11-08 04:41:00 +08:00
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.
```js
Mark.create({
// make this mark exitable - default is false
exitable: true,
})
```
2020-10-30 23:13:47 +08:00
#### Group
2020-11-06 00:01:25 +08:00
Add this mark to a group of extensions, which can be referred to in the content attribute of the schema.
```js
Mark.create({
2020-11-06 00:01:25 +08:00
// add this mark to the 'basic' group
group: 'basic',
// add this mark to the 'basic' and the 'foobar' group
2020-11-17 18:19:18 +08:00
group: 'basic foobar',
2020-11-06 00:01:25 +08:00
})
```
#### 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.
```js
Mark.create({
2020-11-06 00:01:25 +08:00
code: true,
})
```
2020-10-30 23:13:47 +08:00
#### Spanning
2020-11-06 00:01:25 +08:00
By default marks can span multiple nodes when rendered as HTML. Set `spanning: false` to indicate that a mark must not span multiple nodes.
```js
Mark.create({
2020-11-06 00:01:25 +08:00
spanning: false,
})
```
2020-11-05 21:39:28 +08:00
## Get the underlying ProseMirror schema
2021-10-20 04:30:45 +08:00
There are a few use cases where you need to work with the underlying schema. Youll need that if youre using the Tiptap collaborative text editing features or if you want to manually render your content as HTML.
2020-11-05 21:39:28 +08:00
### Option 1: With an Editor
If you need this on the client side and need an editor instance anyway, its available through the editor:
```js
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: [
2020-11-16 17:03:12 +08:00
Document,
Paragraph,
Text,
2020-11-05 21:39:28 +08:00
// 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:
```js
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([
2020-11-16 17:03:12 +08:00
Document,
Paragraph,
Text,
2020-11-05 21:39:28 +08:00
// add more extensions here
])
```