|
| 1 | +--- |
| 2 | +title: Configure custom nodes and marks |
| 3 | +meta: |
| 4 | + title: Custom nodes/marks | Tiptap Content AI |
| 5 | + description: Learn how to configure the AI Agent extension so that it can generate and understand custom nodes and marks. |
| 6 | + category: Content AI |
| 7 | +--- |
| 8 | + |
| 9 | +import { CodeDemo } from '@/components/CodeDemo' |
| 10 | + |
| 11 | +The AI Agent extension understands the content of the document and the possible elements (nodes and marks) that can be in it. |
| 12 | + |
| 13 | +For example, the AI Agent extension detects the [Table](/editor/extensions/nodes/table) extension and knows how to generate tables. If the Table extension is not enabled and the user asks it to generate a table, the AI Agent will respond that the document doesn't support this type of element. |
| 14 | + |
| 15 | +This is automatically done for all built-in Tiptap extensions. However, if you have an editor with [custom nodes and marks](/editor/extensions/custom-extensions), you need to configure the AI Agent extension so that it can generate and understand them. |
| 16 | + |
| 17 | +<CodeDemo |
| 18 | + isPro |
| 19 | + isLarge |
| 20 | + path="/Extensions/AiAgentCustomElements" |
| 21 | + src="https://develop--tiptap-pro.netlify.app/preview/Extensions/AiAgentCustomElements" |
| 22 | +/> |
| 23 | + |
| 24 | +## Configure custom nodes |
| 25 | + |
| 26 | +To configure custom nodes for the AI Agent, you need to follow these steps: |
| 27 | + |
| 28 | +1. Define your custom Tiptap node extension |
| 29 | +2. Configure the AI Agent with schema awareness information |
| 30 | + |
| 31 | +Let's walk through each step with a practical example. |
| 32 | + |
| 33 | +### Step 1: Define a custom Tiptap node extension |
| 34 | + |
| 35 | +First, create your custom node extension. Here's an example of a custom "Alert" node: |
| 36 | + |
| 37 | +```ts |
| 38 | +import { Node, mergeAttributes } from '@tiptap/core' |
| 39 | + |
| 40 | +export const AlertNode = Node.create({ |
| 41 | + name: 'alert', |
| 42 | + |
| 43 | + addOptions() { |
| 44 | + return { |
| 45 | + HTMLAttributes: {}, |
| 46 | + } |
| 47 | + }, |
| 48 | + |
| 49 | + addAttributes() { |
| 50 | + return { |
| 51 | + type: { |
| 52 | + default: 'info', |
| 53 | + parseHTML: (element) => element.getAttribute('data-type'), |
| 54 | + renderHTML: (attributes) => { |
| 55 | + if (!attributes.type) { |
| 56 | + return {} |
| 57 | + } |
| 58 | + return { |
| 59 | + 'data-type': attributes.type, |
| 60 | + } |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | + }, |
| 65 | + |
| 66 | + parseHTML() { |
| 67 | + return [ |
| 68 | + { |
| 69 | + tag: 'div[data-alert]', |
| 70 | + }, |
| 71 | + ] |
| 72 | + }, |
| 73 | + |
| 74 | + renderHTML({ HTMLAttributes }) { |
| 75 | + return [ |
| 76 | + 'div', |
| 77 | + mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, { |
| 78 | + 'data-alert': '', |
| 79 | + }), |
| 80 | + 0, |
| 81 | + ] |
| 82 | + }, |
| 83 | +}) |
| 84 | +``` |
| 85 | + |
| 86 | +Add the extension to your editor: |
| 87 | + |
| 88 | +```ts |
| 89 | +import { Editor } from '@tiptap/core' |
| 90 | +import { AlertNode } from './AlertNode' |
| 91 | + |
| 92 | +const editor = new Editor({ |
| 93 | + extensions: [ |
| 94 | + // ... other extensions |
| 95 | + AlertNode, |
| 96 | + ], |
| 97 | +}) |
| 98 | +``` |
| 99 | + |
| 100 | +### Step 2: Configure the AI Agent |
| 101 | + |
| 102 | +Now configure the AI Agent extension with schema awareness information about your custom node: |
| 103 | + |
| 104 | +```ts |
| 105 | +const provider = new AiAgentProvider({ |
| 106 | + schemaAwarenessCustomElements: [ |
| 107 | + { |
| 108 | + extensionName: 'alert', |
| 109 | + tag: 'div[data-alert]', |
| 110 | + name: 'Alert Box', |
| 111 | + description: |
| 112 | + 'A highlighted box used to display important information, warnings, or tips to the user', |
| 113 | + // Describe the HTML attributes of the node as it is rendered in HTML |
| 114 | + attributes: [ |
| 115 | + { |
| 116 | + name: 'data-alert', |
| 117 | + // Specify the "value" property if the attribute always has that value |
| 118 | + value: '', |
| 119 | + description: 'Indicates that this is an alert box', |
| 120 | + }, |
| 121 | + { |
| 122 | + name: 'data-type', |
| 123 | + description: 'The type of alert: info, warning, error, or success', |
| 124 | + }, |
| 125 | + ], |
| 126 | + }, |
| 127 | + ], |
| 128 | +}) |
| 129 | +``` |
| 130 | + |
| 131 | +## Multiple custom elements |
| 132 | + |
| 133 | +You can configure multiple custom nodes and marks at once. Here's an example with both an Alert node and a custom highlight marks: |
| 134 | + |
| 135 | +```ts |
| 136 | +const provider = new AiAgentProvider({ |
| 137 | + schemaAwarenessCustomElements: [ |
| 138 | + { |
| 139 | + extensionName: 'alert', |
| 140 | + tag: 'div[data-alert]', |
| 141 | + name: 'Alert Box', |
| 142 | + description: 'A highlighted box used to display important information, warnings, or tips', |
| 143 | + attributes: [ |
| 144 | + { |
| 145 | + name: 'data-alert', |
| 146 | + value: '', |
| 147 | + description: 'Indicates that this is an alert box', |
| 148 | + } |
| 149 | + { |
| 150 | + name: 'data-type', |
| 151 | + description: 'The type of alert: info, warning, error, or success', |
| 152 | + }, |
| 153 | + ], |
| 154 | + }, |
| 155 | + // Custom marks are also configured in the same way |
| 156 | + { |
| 157 | + extensionName: 'customHighlight', |
| 158 | + tag: 'span', |
| 159 | + name: 'Custom Highlight', |
| 160 | + description: 'Highlights text with a special background', |
| 161 | + attributes: [ |
| 162 | + { |
| 163 | + name: 'data-custom-highlight', |
| 164 | + value: '', |
| 165 | + description: 'Indicates that this is a custom highlight', |
| 166 | + } |
| 167 | + ], |
| 168 | + }, |
| 169 | + ], |
| 170 | +}) |
| 171 | +``` |
| 172 | + |
| 173 | +## Best practices |
| 174 | + |
| 175 | +When configuring custom nodes and marks for the AI Agent extension: |
| 176 | + |
| 177 | +1. **Use descriptive names**: Choose clear, descriptive names that help the AI model understand what the element represents. |
| 178 | + |
| 179 | +2. **Provide detailed descriptions**: Include comprehensive descriptions that explain both what the element is and how it's displayed or used. |
| 180 | + |
| 181 | +3. **Document all attributes**: List all possible HTML attributes with their purposes and expected values. |
| 182 | + |
| 183 | +4. **Use consistent naming**: Match the `extensionName` with your actual Tiptap extension name. |
| 184 | + |
| 185 | +5. **Specify HTML structure**: Ensure `tag` and `atributes` match exactly how your extension renders HTML. |
| 186 | + |
| 187 | +## API Reference |
| 188 | + |
| 189 | +The `schemaAwarenessCustomElements` option accepts an array of `SchemaAwarenessItem` objects with the following properties: |
| 190 | + |
| 191 | +| Property | Type | Required | Description | |
| 192 | +| --------------- | -------------------------------- | -------- | --------------------------------------------------------- | |
| 193 | +| `extensionName` | `string` | Yes | The name of the Tiptap extension (must match exactly) | |
| 194 | +| `tag` | `string` | Yes | The HTML tag or selector that represents this element | |
| 195 | +| `name` | `string` | Yes | A human-readable name for the element | |
| 196 | +| `description` | `string \| null` | No | Explanation of what the element is and how it's displayed | |
| 197 | +| `attributes` | `SchemaAwarenessItemAttribute[]` | No | Array of possible HTML attributes for this element | |
| 198 | + |
| 199 | +### SchemaAwarenessItemAttribute |
| 200 | + |
| 201 | +Each attribute object in the `attributes` array has the following properties: |
| 202 | + |
| 203 | +| Property | Type | Required | Description | |
| 204 | +| ------------- | ---------------- | -------- | ---------------------------------------------------------- | |
| 205 | +| `name` | `string` | Yes | The name of the attribute in the HTML code | |
| 206 | +| `value` | `string` | No | If specified, the attribute always has this exact value | |
| 207 | +| `description` | `string \| null` | No | Explanation of the attribute's purpose and expected values | |
0 commit comments