Custom block
A node, an add-on, and its HTML serializer.
defineBlockcnNode stores storage.blockcn, so the text toolbar can hide while the node is selected. defineAddon keeps the node and its serializer on one object. createBlockcnEditor installs that object on this editor only.
This page's code block is examples/recipes/custom-block.tsx. pnpm --dir docs typecheck compiles that file and fails if this block drifts.
import type { ReactNode } from "react";
import { createBlockcnEditor, defineAddon } from "@/components/editor/preset";
import { defineBlockcnNode } from "@/editor-core/schema/blockcnNode";
export const Note = defineBlockcnNode({
name: "note",
group: "block",
content: "block+",
defining: true,
blockcn: { hideTextToolbar: true },
parseHTML: () => [{ tag: "aside[data-note]" }],
renderHTML: () => ["aside", { "data-note": "" }, 0],
});
export const note = defineAddon<ReactNode>({
id: "note",
extensions: [Note],
serializers: {
note: { html: (node, ctx) => `<aside>${ctx.renderBlocks(node.content)}</aside>` },
},
});
export const kit = createBlockcnEditor({ addons: [note] });