Rich Text Extensions
The rich text field offers a fixed set of features, like headings, links and tables. An extension adds one of your own, like a notice box. The editor is Tiptap, so an extension is a Tiptap extension. This page shows how to add one, from the schema to the button in the toolbar.
An extension has two parts:
- The schema says what the content looks like: the node or mark, its attributes and what it may contain. The server checks content with it, and your site renders with it.
- The browser part adds it to the editor, with buttons for the toolbar.
Add an extension
Define the schema
Install @tiptap/core, and define the node in an ordinary module:
import { Node } from "@tiptap/core";
export const Notice = Node.create({
name: "notice",
group: "block",
content: "block+",
defining: true,
parseHTML: () => [{ tag: "aside[data-notice]" }],
renderHTML: () => ["aside", { "data-notice": "", role: "note" }, 0],
});
The schema runs on the server, so it MUST work without a browser DOM. renderHTML is the markup on your site.
Write the browser part
The browser part lists the Tiptap extensions for the editor, and the buttons it adds to the toolbar. Put it in a .client.ts module:
import type { RichtextBrowserExtension } from "tenoncms/richtext";
import { Notice } from "./notice";
export const notice: RichtextBrowserExtension = {
extensions: [Notice],
controls: [
{
label: { en: "Notice", de: "Hinweis" },
active: (editor) => editor.isActive("notice"),
run: (editor) => editor.chain().focus().toggleWrap("notice").run(),
},
],
};
A control has a label and run(editor). active(editor) highlights the button, and disabled(editor) turns it off.
Register it
Add it to richtext.extensions in defineCms(). The key is the feature name:
import { Notice } from "./cms/richtext/notice";
import { notice } from "./cms/richtext/notice.client";
export default defineCms({
// ...
richtext: {
extensions: {
notice: { schema: Notice, client: notice },
},
},
});
Turn it on in a field
List the feature name in allowed, next to the built-in features:
body: { type: "richtext", allowed: ["bold", "italic", "link", "notice"] },
Render it on your site
Pass the schema to the Richtext component, keyed by the feature name:
<Richtext content={post.body} extensions={{ notice: [Notice] }} />
See Your own nodes for every framework.
Rules
- The schema and the browser part MUST use the same names, attributes and content rules. Content the editor writes must pass the server's check.
- Node views and other DOM code belong only in the browser part.
- Built-in feature names, like
boldorlink, are taken. A name used twice stops the server at startup. - Publishing checks the content against the schema. Drafts are checked less strictly.
- When an extension is gone, its content stays stored as it is. The editor shows it, but editors cannot change it until you add the extension again.
In a plugin
A plugin sets the same richtext.extensions in definePlugin(). Its client is a clientRef() to a package export:
import { clientRef } from "tenoncms/admin";
richtext: {
extensions: {
notice: { schema: Notice, client: clientRef("my-plugin/admin#notice") },
},
},
The bundled example plugin adds a notice like this. See Plugins.
Related resources
- Rich text field: the built-in features and
allowed. - Components: render rich text on your site.
- Tiptap docs: write nodes and marks.