Custom blocks
Add a block once, with its node, view, slash command, and serializer.
An add-on is one object. defineAddon takes the extensions, the slash category, the chrome, and a serializers map, and stores them on the add-on. It does not register anything globally. createBlockcnEditor installs that whole object on one editor, including the serializers. A second editor on the same page keeps its own. There is no separate list to forget.
createBlockcnEditor (from editor-shell) installs the core blocks: paragraphs, headings, lists, quotes, code, callouts, and toggles. Pass core (the same option as createEditorExtensions) to leave a core block out. Everything else is opt-in. Each add-on factory ships in its own registry item, so you only install what you import:
| Factory | Registry item | Import from |
|---|---|---|
table() | table-toolbar | @/components/editor/tableAddon |
media() | media-blocks | @/components/editor/mediaAddon |
excalidraw() | excalidraw-block | @/components/editor/excalidrawAddon |
ai() | ai-menu | @/components/editor/ai/aiAddon |
columns() | columns | @/components/editor/addons/columnsAddon |
math() | math | @/components/editor/addons/mathAddon |
mentions() | mentions | @/components/editor/addons/mentionsAddon |
toc() | toc | @/components/editor/addons/tocAddon |
comments() | comments | @/components/editor/comments/commentsAddon |
versions() | versions | @/components/editor/versions/versionsAddon |
suggestions() | suggestions | @/components/editor/suggestions/suggestionsAddon |
linkPreview() | link-preview | @/components/editor/linkPreview/linkPreviewAddon |
pages() | pages | @/components/editor/pages/pagesAddon |
emoji(), defineAddon | editor-shell | @/components/editor/preset |
Collaboration is an add-on you build from createCollaborationExtensions (see below). Each add-on factory takes an options object: table({ resizable, cellBackgrounds }), columns({ min, max, resizable }), math({ katexOptions }), toc({ levels }), excalidraw({ theme }), ai({ endpoint, transport, actions, systemPrompt }) (transport wins over endpoint), and mentions({ fetchMentions, renderItem, trigger }). fetchMentions is required.
import { useEditor } from "@/components/editor/useEditor";
import { Editor } from "@/components/editor/Editor";
import { createBlockcnEditor } from "@/components/editor/preset";
import { table } from "@/components/editor/tableAddon";
import { media } from "@/components/editor/mediaAddon";
import { columns } from "@/components/editor/addons/columnsAddon";
import { ai } from "@/components/editor/ai/aiAddon";
import { createUploadFile } from "@/lib/mediaUpload";
const kit = createBlockcnEditor({
addons: [
table(),
media({ uploadFile: createUploadFile({ endpoint: "/api/upload" }) }),
columns(),
ai({ endpoint: "/api/ai" }),
],
});
export function BlockEditor() {
const editor = useEditor({ extensions: kit.extensions, content: "" });
return (
<Editor editor={editor}>
<Editor.Frame>
<Editor.Content />
<Editor.Chrome>
<Editor.SideMenu />
<Editor.FormattingToolbar />
{kit.chrome}
</Editor.Chrome>
</Editor.Frame>
</Editor>
);
}A block of your own
defineBlockcnNode is Node.create plus storage.blockcn. Set hideTextToolbar so the formatting toolbar stays hidden while the block is selected. Pair the node with a view, a slash item, and a serializer, then pass all of it to defineAddon.
import { StickyNote } from "lucide-react";
import { ReactNodeViewRenderer } from "@tiptap/react";
import { NodeViewWrapper, type ReactNodeViewProps } from "@tiptap/react";
import { defineBlockcnNode } from "@/editor-core/schema/blockcnNode";
import { defineAddon, createBlockcnEditor } from "@/components/editor/preset";
function NoteView({ node }: ReactNodeViewProps) {
return (
<NodeViewWrapper className="rounded-md border p-3" data-note="">
{node.textContent}
</NodeViewWrapper>
);
}
const note = defineBlockcnNode({
name: "note",
group: "block",
content: "inline*",
blockcn: { hideTextToolbar: true },
parseHTML: () => [{ tag: "aside[data-note]" }],
renderHTML: () => ["aside", { "data-note": "" }, 0],
addNodeView() {
return ReactNodeViewRenderer(NoteView);
},
});
const noteAddon = defineAddon({
id: "note",
extensions: [note],
slash: {
id: "notes",
title: "Notes",
items: [
{
id: "note",
type: "note",
label: "Note",
description: "A short aside",
keywords: ["note", "aside"],
icon: StickyNote,
category: "notes",
command: (editor, range) => {
editor.chain().focus().deleteRange(range).insertContent({ type: "note" }).run();
},
},
],
},
serializers: {
note: {
html: (node) => `<aside data-note="">${node.content?.map((child) => child.text ?? "").join("") ?? ""}</aside>`,
},
},
});
const kit = createBlockcnEditor({ addons: [noteAddon] });A serializer can also have docx (Word blocks) and paint(node, inline), an async function that returns the HTML the visual HTML and PDF export use when the block has not painted yet (it is below the fold). Without paint, html is used. renderDocument(json, { addons: [noteAddon] }) uses that map for one render. An editor built from kit.extensions reads the same map from editor.storage.blockcnExport. registerSerializer still writes a shared fallback and warns once in development; it is deprecated. Collaboration is the same shape: build an add-on whose extensions are createCollaborationExtensions(session, user) and pass history: false to createBlockcnEditor.