Skip to main content
The CodeBlock extension allows you to create code blocks for displaying code snippets. It supports language specification, syntax highlighting, and special keyboard handling for code editing.

Installation

Usage

The CodeBlock extension is included in the StarterKit by default:
To use it standalone:

Configuration

languageClassPrefix

Prefix for language classes applied to code tags.
string | null
default:"'language-'"
Adds a prefix to language classes that are applied to code tags.

exitOnTripleEnter

Defines whether the node should be exited on triple enter.
boolean
default:"true"
Exit the code block when pressing Enter three times.

exitOnArrowDown

Defines whether the node should be exited on arrow down when at the end.
boolean
default:"true"
Exit the code block when pressing arrow down at the end if there is no node after it.

defaultLanguage

The default language for code blocks.
string | null
default:"null"
Set a default language for all code blocks.

enableTabIndentation

Enables tab key for indentation in code blocks.
boolean
default:"false"
When enabled, pressing Tab will insert spaces for indentation instead of changing focus.

tabSize

The number of spaces to use for tab indentation.
number
default:"4"
Number of spaces to insert when pressing Tab (only when enableTabIndentation is true).

HTMLAttributes

Custom HTML attributes to add to the rendered code block element.
Record<string, any>
default:"{}"
Custom HTML attributes that should be added to the rendered HTML tag.

Commands

setCodeBlock

Converts the current node to a code block.

toggleCodeBlock

Toggles a code block. If the current node is already a code block, it converts it to a paragraph.

Keyboard Shortcuts

  • Mod-Alt-C: Toggle code block
  • Backspace: Remove code block when empty or at start
  • Tab: Insert indentation (when enableTabIndentation is true)
  • Shift-Tab: Remove indentation (when enableTabIndentation is true)
  • Enter: Exit code block on triple enter (when exitOnTripleEnter is true)
  • ArrowDown: Exit code block when at end (when exitOnArrowDown is true)

Input Rules

The CodeBlock extension supports Markdown-style input rules:
  • Type ``` followed by a space to create a code block
  • Type ``` followed by a language name and space to create a code block with that language
  • Type ~~~ followed by a space as an alternative syntax

Features

VS Code Paste Support

The extension automatically detects code pasted from VS Code and creates a code block with the correct language.

Tab Indentation

When enableTabIndentation is enabled, you can use Tab and Shift-Tab to indent and unindent code:
  • Tab on a selection: Indents all selected lines
  • Shift-Tab on a selection: Unindents all selected lines
  • Tab without selection: Inserts indentation at cursor
  • Shift-Tab without selection: Removes indentation at cursor

Source Code

View the source code on GitHub: packages/extension-code-block/src/code-block.ts