@eventuras/scribo-core
The clean core of Scribo: a small set of extensions, on Lexical's extension API, to build an editor from. @eventuras/scribo-markdown, the Markdown editor, is a preset over it.
The core holds nothing that belongs to one app and no syntax highlighter. It imports only Lexical: no React, which is in @eventuras/scribo-react. Lexical is pinned to exactly the version Payload CMS uses (0.50.0 today).
pnpm add @eventuras/scribo-core
@lexical/markdown is needed only for ./markdown. It is an optional peer dependency: install it, at the same Lexical version, if you import that entry.
The contract
An extension for Scribo is a Lexical extension, made with Lexical's own defineExtension. The core re-exports that API (defineExtension, configExtension, declarePeerDependency, buildEditorFromExtensions, …) and adds two shared extensions that other extensions contribute to:
ToolbarExtensioncollects toolbar items.MarkdownExtension(in./markdown) collects Markdown transformers.
An extension contributes as a peer dependency, by name, with configuration that the shared extension merges. If the editor is built without the shared extension, the contribution is dropped, so an extension does not force a toolbar or Markdown on an app.
import {declarePeerDependency,defineExtension,MARKDOWN_EXTENSION_NAME,toolbarItem,toolbarItems,} from "@eventuras/scribo-core";import { COMMAND_PRIORITY_EDITOR, createCommand } from "lexical";// A type-only import: this file does not load @lexical/markdown.import type { MarkdownExtension } from "@eventuras/scribo-core/markdown";export const INSERT_THING_COMMAND = createCommand<void>("INSERT_THING_COMMAND");export const ThingExtension = defineExtension({name: "my-app/Thing",nodes: [ThingNode],peerDependencies: [toolbarItems(toolbarItem({id: "thing",group: "insert",label: "Thing",icon: "thing",command: INSERT_THING_COMMAND,}),),declarePeerDependency<typeof MarkdownExtension>(MARKDOWN_EXTENSION_NAME, {transformers: [THING_TRANSFORMER],}),],register: (editor) =>editor.registerCommand(INSERT_THING_COMMAND,$insertThing,COMMAND_PRIORITY_EDITOR,),});
Contributions are merged in the order the extensions are built, which is the order of the dependencies they are listed in.
Entries
| Entry | Holds |
|---|---|
@eventuras/scribo-core | The contract, BaseExtension (paragraphs, bold, italic, strikethrough, inline code, undo), ToolbarExtension, exportJSON / importJSON. |
@eventuras/scribo-core/markdown | MarkdownExtension, importMarkdown / exportMarkdown and their $ forms. |
@eventuras/scribo-core/extensions/* | Opt-in entries over Lexical's own packages: heading, quote, list, link, code. Each adds its command and toolbar items. |
An editor without a DOM, for a server or a test, is buildEditorFromExtensions(extension).
Toolbar
A toolbar item is data. It names a command and holds no reference to the editor:
interface ToolbarItem<Payload> {id: string;group: string; // "history", "block", "format", "insert", or your ownorder?: number;label: string;icon?: string;command: LexicalCommand<Payload>;payload?: Payload;$isActive?: () => boolean;$isEnabled?: () => boolean;}
getToolbarItems(editor) returns the items sorted by group, then order, then the order they were contributed in. readToolbarState(editor) adds whether each is active and enabled, and runToolbarItem(editor, item) dispatches its command. Configure the toolbar with configExtension(ToolbarExtension, { hidden: ["strikethrough"], groups: [...] }).
In React, <Toolbar /> from @eventuras/scribo-react renders plain buttons with scribo- class names and no CSS. For your own markup, its useToolbar() returns the items with their live state and a run function.
Markdown
MarkdownExtension uses the transformers that extensions contribute, followed by Lexical's stock transformers for the text formats and for each of the opt-in entries the editor has loaded. So an editor with only BaseExtension and HeadingExtension reads # Title as a heading and leaves > quote as text. Contributed transformers come first and can override the stock ones.
Markdown shortcuts while typing are on by default (shortcuts: false turns them off).
Code blocks and highlighters
@eventuras/scribo-core/extensions/code gives code blocks on @lexical/code-core, with the ``` fence in Markdown. Without a highlighter they are plain, and there is no language to pick.
A highlighter is an object from another package:
interface CodeHighlighter {name: string;/** The Lexical extension that colours the code. Loaded only when highlighting is asked for. */extension?: AnyLexicalExtensionArgument;/** The languages for a picker, as [value, label] pairs. */languages: ReadonlyArray<readonly [value: string, label: string]>;normalizeLanguage?: (language: string) => string;/** The specifier of a stylesheet to import, when the highlighter ships one. */stylesheet?: string;}
import { codeBlocks } from "@eventuras/scribo-core/extensions/code";dependencies: [...codeBlocks(), // plain...codeBlocks({ highlighter }), // a language picker, plain code...codeBlocks({ highlighter, highlight: true }), // coloured code];
@eventuras/scribo-code-prism is the Prism highlighter. Another one, such as Shiki over @lexical/code-shiki, is the same few lines.
Not here yet
A JSON ScriboEditor component, the document envelope with migrations, a checklist entry and collaborative editing are planned for later steps.
License
MIT