Writing an extension

An extension for Scribo is a Lexical extension, made with defineExtension. The core re-exports Lexical's extension API, so everything here is imported from @eventuras/scribo-core and lexical.

The core's page states the contract and shows the outline of an extension with a node and a Markdown transformer. This guide walks through a smaller one from start to finish: a command that inserts today's date, and a toolbar item for it.

It assumes the editor from Getting started. Because the code below imports from lexical directly, add it as a dependency of your own, at the version Scribo pins:

sh
pnpm add lexical@0.50.0

The extension

ts
import { defineExtension, toolbarItem, toolbarItems } from "@eventuras/scribo-core";
import { $getSelection, $isRangeSelection, COMMAND_PRIORITY_EDITOR, createCommand } from "lexical";
export const INSERT_DATE_COMMAND = createCommand<void>("INSERT_DATE_COMMAND");
/** Today's date where the user is, as `2026-12-04`. */
function today(): string {
const now = new Date();
const pad = (part: number) => String(part).padStart(2, "0");
return `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`;
}
export const DateExtension = defineExtension({
name: "my-app/Date",
peerDependencies: [
toolbarItems(
toolbarItem({ id: "date", group: "insert", label: "Date", command: INSERT_DATE_COMMAND }),
),
],
register: (editor) =>
editor.registerCommand(
INSERT_DATE_COMMAND,
() => {
const selection = $getSelection();
if (!$isRangeSelection(selection)) return false;
selection.insertText(today());
return true;
},
COMMAND_PRIORITY_EDITOR,
),
});

Three things to note:

  • The name is how other extensions refer to this one. Prefix it with something of your own.
  • The toolbar item is data. It names a command and holds no reference to an editor. toolbarItems(...) contributes it to the core's ToolbarExtension as a peer dependency, by name. In an editor built without ToolbarExtension the contribution is dropped, and the command still works.
  • register runs when the editor is made, and returns what undoes it. registerCommand returns such a function, so it is returned as it is.

Using it

Add it to the dependencies of an editor:

ts
import { BaseExtension, ToolbarExtension, defineExtension } from "@eventuras/scribo-core";
const editor = defineExtension({
name: "my-editor",
dependencies: [BaseExtension, ToolbarExtension, DateExtension],
});

Toolbar from @eventuras/scribo-react now shows a Date button in the insert group. Items are sorted by group, then by their order, then by the order they were contributed in, which is the order of dependencies.

The chords package does the same. The Chord, Verse, Chorus and Bridge buttons in its song editor are items it contributes, in the core's own toolbar:

The song editor: the core's toolbar with the chords extension's items in it

An app can hide an item, or set the order of the groups, without touching the extension: configExtension(ToolbarExtension, { hidden: ["date"] }) in the editor's dependencies, in place of ToolbarExtension.

Testing it without a browser

buildEditorFromExtensions gives an editor with no DOM, which is enough to test a command. With Vitest:

ts
import {
BaseExtension,
ToolbarExtension,
buildEditorFromExtensions,
defineExtension,
getToolbarItems,
runToolbarItem,
} from "@eventuras/scribo-core";
import { $getRoot } from "lexical";
import { expect, test } from "vitest";
test("the toolbar item inserts the date", () => {
const editor = buildEditorFromExtensions(
defineExtension({
name: "test-editor",
dependencies: [BaseExtension, ToolbarExtension, DateExtension],
}),
);
editor.update(() => $getRoot().selectEnd(), { discrete: true });
const item = getToolbarItems(editor).find((entry) => entry.id === "date");
expect(item && runToolbarItem(editor, item)).toBe(true);
expect(editor.read(() => $getRoot().getTextContent())).toMatch(/^\d{4}-\d{2}-\d{2}$/);
});

runToolbarItem dispatches the item's command, as a click on the button does, and refuses an item that is disabled.

Going further

  • A node of your own goes in the extension's nodes. See the outline in the contract.
  • Markdown for that node is a transformer, contributed to MarkdownExtension in the same way as a toolbar item. markdownTransformers(...) from @eventuras/scribo-core/markdown is the counterpart of toolbarItems(...). It loads @lexical/markdown; the contract's outline shows the form with declarePeerDependency and a type-only import, for an extension that must not.
  • $isActive and $isEnabled on a toolbar item say when its button is pressed or disabled. They are listed under Toolbar.
  • A React component for a node can be set from a second extension, so the node itself stays free of React. The chords package does this, and explains how.