What You’ll Build
You’ll create a rich text editor with:- Basic text editing
- Bold and italic formatting
- Headings
- Keyboard shortcuts
- A simple toolbar
Prerequisites
Before starting, make sure you have:- ProseKit installed in your project (Installation guide)
- Basic knowledge of your chosen framework (React, Vue, etc.)
- A text editor and development server running
Step 1: Create a Minimal Editor
Let’s start with the absolute minimum to get an editor working.We’re importing
prosekit/basic/style.css which contains essential styles. Without this, the editor won’t work correctly.Every ProseKit editor needs at least two node types:
doc (the document root) and text (for text content):The
union function combines multiple extensions into one. Think of it like composing LEGO blocks - each extension adds specific functionality.Step 2: Add Text Formatting
Let’s add bold and italic formatting capabilities.
Now your editor supports bold and italic! Each extension (
defineBold() and defineItalic()) includes:
- The mark specification (how it’s stored and rendered)
- Commands to toggle the formatting
- Keyboard shortcuts (Cmd+B for bold, Cmd+I for italic)
- Input rules (e.g.,
**text**becomes bold)
**hello** and the text will automatically become bold!
Step 3: Add Headings
Let’s add support for headings (h1, h2, h3, etc.).
Now you can create headings by typing
# followed by a space for h1, ## for h2, and so on!
Step 4: Add Essential Features
A useful editor needs undo/redo, keyboard shortcuts, and history. Let’s add these:function defineExtension() {
return union(
// Nodes
defineDoc(),
defineText(),
defineParagraph(),
defineHeading(),
// Marks
defineBold(),
defineItalic(),
// Core features
defineBaseKeymap(), // Essential keyboard shortcuts
defineBaseCommands(), // Basic commands
defineHistory(), // Undo/redo support
)
}
- Press Cmd+Z to undo (Ctrl+Z on Windows)
- Press Cmd+Shift+Z to redo
- Use standard text editing shortcuts (arrows, backspace, etc.)
Step 5: Add a Simple Toolbar
Let’s create a toolbar with buttons for bold, italic, and headings. Now you have a toolbar with buttons to format your text!Step 6: Add Typography Styles
Let’s make the editor content look better with ProseKit’s typography styles. Add this import at the top of your file:Complete Code
Here’s your complete editor with all the features we’ve added:What You’ve Learned
In this tutorial, you learned:- Extensions are composable: Use
union()to combine multiple extensions - Node types: Every editor needs at least
doc,text, and a block node likeparagraph - Marks: Formatting like bold and italic are called “marks”
- Commands: Each extension provides commands to modify the document
- Hooks: Use framework-specific hooks like
useEditor()to access the editor instance - The ProseKit component: Provides context for all child components
Using defineBasicExtension
In this tutorial, we built an extension from scratch to understand the fundamentals. In practice, you’ll often usedefineBasicExtension() which includes all of these features and more:
- Everything we built above
- Plus: links, lists, blockquotes, code blocks, tables, images, and more
- Plus: input rules, keyboard shortcuts, and automatic transformations
Next Steps
Now that you understand the basics, explore these topics:Extensions Guide
Learn about all available extensions and how to customize them
Styling
Make your editor beautiful with custom styles
Saving Content
Learn how to save and load editor content
API Reference
Explore the complete API documentation