Overview
Thecode-block extension adds support for code block nodes with optional syntax highlighting. Code blocks preserve whitespace and disable rich text formatting, making them ideal for displaying code snippets.
Installation
Usage
Basic Setup
With Syntax Highlighting (Shiki)
API Reference
defineCodeBlock()
Defines a code block node with commands, keymap, input rules, and node specification. Returns:CodeBlockExtension
This extension includes:
- Node specification with language attribute
- Commands for manipulating code blocks
- Keyboard shortcuts
- Input rules and enter rules for Markdown-style syntax
defineCodeBlockSpec()
Defines only the code block node specification. Returns:CodeBlockSpecExtension
defineCodeBlockCommands()
Defines code block-related commands. Returns:CodeBlockCommandsExtension
defineCodeBlockKeymap()
Defines keyboard shortcuts for code block operations. Returns:PlainExtension
defineCodeBlockInputRule()
Defines input rules for Markdown-style code block syntax (triple backticks). Returns:PlainExtension
defineCodeBlockEnterRule()
Defines enter key rules for creating code blocks. Returns:PlainExtension
defineCodeBlockShiki(options?)
Adds syntax highlighting support using Shiki. Parameters:options?: CodeBlockHighlightOptions
PlainExtension
defineCodeBlockHighlight(options)
Custom syntax highlighting with a custom parser. Parameters:options: CodeBlockHighlightOptions
PlainExtension
Node Specification
The code block node is defined with the following properties:- name:
codeBlock - content:
text*(plain text only) - group:
block - code:
true(disables rich text formatting) - defining:
true(prevents wrapping) - marks:
''(no marks allowed) - attrs:
language: string (default: ”)
- parseDOM: Parses
<pre>and<code>tags - toDOM: Renders as
<pre><code>structure
Types
CodeBlockAttrs
CodeBlockHighlightOptions
Commands
setCodeBlock(attrs?)
Converts the current block to a code block. Parameters:attrs?: CodeBlockAttrs- Optional language attribute
insertCodeBlock(attrs?)
Inserts a new code block node. Parameters:attrs?: CodeBlockAttrs- Optional language attribute
toggleCodeBlock(attrs?)
Toggles between a code block and the default block type. Parameters:attrs?: CodeBlockAttrs- Optional language attribute
setCodeBlockAttrs(attrs)
Updates the attributes of the current code block (e.g., change the language). Parameters:attrs: CodeBlockAttrs- New attributes
Keyboard Shortcuts
| Shortcut | Action |
|---|---|
Enter | Exit code block when ending with two newlines |
Input Rules
The extension supports Markdown-style code block syntax:Triple Backtick (Space)
Triple Backtick (Enter)
Examples
Basic Setup
With Shiki Syntax Highlighting
Language Selector
Toggle Button
Insert with Default Language
Custom Syntax Highlighter
Shiki Integration
ProseKit includes built-in support for Shiki syntax highlighting:Available Languages and Themes
Styling
Code blocks render with the following HTML structure:HTML Structure
Input HTML:Behavior
Exiting Code Blocks
Press Enter twice (creating two newlines) at the end of a code block to exit and create a new paragraph below.Whitespace Preservation
All whitespace, including tabs and multiple spaces, is preserved in code blocks.No Rich Text Formatting
Marks (bold, italic, etc.) are disabled inside code blocks to maintain plain text formatting.Related Extensions
- Text - Required for text content
- Paragraph - Default block type
- Inline Code - For inline code snippets