blockcn

Publishing an add-on

Copy the banner starter, then publish it as a registry item.

templates/addon is a starter you copy. It is not part of the demo, and it is not a first-party registry item. It adds a banner node, a slash item, a formatting-toolbar button, an HTML serializer, a test, and a registry entry.

Copy templates/addon/src/node.ts and templates/addon/src/addon.tsx into your app (the registry entry's target paths are src/banner/). Imports use @/, the same alias as the editor. pnpm --dir editor typecheck compiles the starter and fails if a source block on this page drifts.

A copied add-on can keep its own labels. If you add the block to this repository, put the label in editor/src/editor-core/i18n/messages.ts and read it with useMessages(). The button uses FormattingToolbar.Button, so it picks up the toolbar's keyboard row and focus ring.

Node

Source: templates/addon/src/node.ts

import { defineBlockcnNode } from "@/editor-core/schema/blockcnNode";

/** A highlighted block. `block+` so it can hold paragraphs and other blocks. */
export const Banner = defineBlockcnNode({
  name: "banner",
  group: "block",
  content: "block+",
  defining: true,
  parseHTML: () => [{ tag: "aside[data-banner]" }],
  renderHTML: () => ["aside", { "data-banner": "" }, 0],
});

Slash item, toolbar button, and serializer

The category id is banner, so the item does not merge into Advanced. defineAddon stores the extension, the slash category, and the serializer on the add-on. createBlockcnEditor installs that object on one editor.

Source: templates/addon/src/addon.tsx

import type { ReactNode } from "react";
import type { Editor } from "@tiptap/core";
import { Megaphone } from "lucide-react";
import { FormattingToolbar } from "@/components/editor/FormattingToolbar";
import { useEditorContext } from "@/components/editor/editorContext";
import { createBlockcnEditor, defineAddon } from "@/components/editor/preset";
import { SLASH_CATEGORY_ADVANCED, SLASH_CATEGORY_ORDER, type SlashCategory } from "@/editor-core/slash/items";
import { Banner } from "./node";

const BANNER_CATEGORY = "banner";

function insertBanner(editor: Editor, range?: { from: number; to: number }) {
  const chain = editor.chain().focus();
  if (range) chain.deleteRange(range);
  chain.insertContent({ type: "banner", content: [{ type: "paragraph" }] }).run();
}

/** One slash category. Its id is new, so it does not merge into Advanced. */
export const bannerSlash: SlashCategory = {
  id: BANNER_CATEGORY,
  title: "Banner",
  order: SLASH_CATEGORY_ORDER[SLASH_CATEGORY_ADVANCED] + 1,
  items: [
    {
      id: "banner",
      type: "banner",
      label: "Banner",
      description: "A highlighted block",
      keywords: ["banner", "callout", "highlight"],
      icon: Megaphone,
      category: BANNER_CATEGORY,
      command: (editor, range) => insertBanner(editor, range),
    },
  ],
};

/** Place this inside `FormattingToolbar` so arrow keys reach it. */
export function BannerButton() {
  const editor = useEditorContext();
  return <FormattingToolbar.Button icon={Megaphone} label="Banner" onClick={() => insertBanner(editor)} />;
}

export const banner = defineAddon<ReactNode>({
  id: "banner",
  extensions: [Banner],
  slash: bannerSlash,
  serializers: {
    banner: {
      html: (node, ctx) => `<aside data-banner="">${ctx.renderBlocks(node.content)}</aside>`,
    },
  },
});

export const kit = createBlockcnEditor({ addons: [banner] });

Place BannerButton inside FormattingToolbar. children replaces the default row. See Toolbar button.

Source: templates/addon/src/usage.tsx

import type { Editor } from "@tiptap/core";
import { FormattingToolbar } from "@/components/editor/FormattingToolbar";
import { BannerButton } from "./addon";

/** `children` replaces the default row, so keep the buttons you still want. */
export function BannerToolbar({ editor }: { editor: Editor }) {
  return (
    <FormattingToolbar editor={editor}>
      <FormattingToolbar.Bold />
      <FormattingToolbar.Italic />
      <BannerButton />
    </FormattingToolbar>
  );
}

Register the add-on the same way as the built-in ones. See Custom blocks.

const editor = useEditor({ extensions: kit.extensions, content: "" });

Test

The range patch is there because jsdom has no layout. The slash item inserts a banner, the serializer writes an aside, the toolbar button inserts a banner and takes part in the arrow-key row, and the registry entry points at files that exist.

Source: templates/addon/src/addon.test.tsx

import { useEffect } from "react";

// jsdom's Range has no layout APIs. ProseMirror calls them while the bubble menu measures its anchor.
const rect = () => new DOMRect(0, 0, 1, 1);
Range.prototype.getBoundingClientRect = rect;
Range.prototype.getClientRects = () => [rect()] as unknown as DOMRectList;

import { readFileSync } from "node:fs";
import path from "node:path";
import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { Editor } from "@tiptap/core";
import { EditorContent, useEditor } from "@tiptap/react";
import { afterEach, expect, it } from "vitest";
import { FormattingToolbar } from "@/components/editor/FormattingToolbar";
import { renderDocument } from "@/lib/export/serialize";
import { BannerButton, banner, bannerSlash, kit } from "./addon";

afterEach(() => {
  cleanup();
});

it("inserts a banner from the slash item", () => {
  const editor = new Editor({ extensions: kit.extensions, content: "<p></p>" });
  const item = bannerSlash.items.find((entry) => entry.id === "banner");
  expect(item).toBeDefined();
  item!.command(editor, { from: 1, to: 1 });
  expect(JSON.stringify(editor.getJSON())).toContain('"banner"');
  editor.destroy();
});

it("serializes a banner to an aside", () => {
  const html = renderDocument(
    {
      type: "doc",
      content: [
        {
          type: "banner",
          content: [{ type: "paragraph", content: [{ type: "text", text: "Hello" }] }],
        },
      ],
    },
    { addons: [banner] },
  );
  expect(html).toContain('data-banner=""');
  expect(html).toContain("Hello");
});

it("the toolbar button inserts a banner and joins the row", async () => {
  let editor: Editor | undefined;
  function Host() {
    const instance = useEditor({ extensions: kit.extensions, content: "<p>Hello</p>" });
    useEffect(() => {
      if (instance) editor = instance;
    }, [instance]);
    if (!instance) return null;
    return (
      <>
        <EditorContent editor={instance} />
        <FormattingToolbar editor={instance} shouldShow={() => true}>
          <FormattingToolbar.Bold />
          <BannerButton />
        </FormattingToolbar>
      </>
    );
  }
  const view = render(<Host />);
  await waitFor(() => expect(editor).toBeDefined());
  editor!.commands.focus();
  const bold = await screen.findByRole("button", { name: "Bold" });
  const button = screen.getByRole("button", { name: "Banner" });
  await waitFor(() => expect(bold.tabIndex).toBe(0));
  expect(button.tabIndex).toBe(-1);
  fireEvent.keyDown(bold, { key: "ArrowRight" });
  expect(document.activeElement).toBe(button);
  fireEvent.click(button);
  await waitFor(() => expect(JSON.stringify(editor!.getJSON())).toContain('"banner"'));
  view.unmount();
  editor!.destroy();
});

it("the registry entry points at files that exist", () => {
  const root = path.resolve(import.meta.dirname, "..");
  const entry = JSON.parse(readFileSync(path.join(root, "registry.json"), "utf8")) as {
    name: string;
    files: { path: string }[];
    registryDependencies: string[];
  };
  expect(entry.name).toBe("banner");
  expect(entry.registryDependencies).toContain("https://blockcn.com/r/editor-shell.json");
  for (const file of entry.files) readFileSync(path.join(root, file.path), "utf8");
});

Registry entry

shadcn add copies files and the items in registryDependencies. This starter depends on editor-shell. Publish the JSON at a URL you control, then install it with npx shadcn@latest add <url>. The first-party items and their commands are on Registry.

Source: templates/addon/registry.json

{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "banner",
  "type": "registry:component",
  "title": "Banner",
  "description": "A starter add-on: a banner node, a slash item, a toolbar button, and an HTML serializer.",
  "files": [
    {
      "path": "src/node.ts",
      "type": "registry:lib",
      "target": "src/banner/node.ts"
    },
    {
      "path": "src/addon.tsx",
      "type": "registry:component",
      "target": "src/banner/addon.tsx"
    }
  ],
  "registryDependencies": [
    "https://blockcn.com/r/editor-shell.json"
  ],
  "dependencies": [
    "@tiptap/core@3.29.0",
    "@tiptap/react@3.29.0",
    "lucide-react@^1.26.0"
  ],
  "meta": {
    "version": "0.1.0"
  }
}

On this page