'use client';
import * as React from 'react';
import { Plate, usePlateEditor } from 'platejs/react';
import { EditorKit } from '@/components/editor/editor-kit';
import { Editor, EditorContainer } from '@/components/ui/editor';
import { DEMO_VALUES } from './values/demo-values';
export default function Demo({ id }: { id: string }) {
const editor = usePlateEditor({
plugins: EditorKit,
value: DEMO_VALUES[id],
});
return (
<Plate editor={editor}>
<EditorContainer variant="demo">
<Editor />
</EditorContainer>
</Plate>
);
}
Kit Usage
Installation
The fastest way to add code block functionality is with the CodeBlockKit
, which includes pre-configured CodeBlockPlugin
, CodeLinePlugin
, and CodeSyntaxPlugin
with syntax highlighting and Plate UI components.
'use client';
import {
CodeBlockPlugin,
CodeLinePlugin,
CodeSyntaxPlugin,
} from '@platejs/code-block/react';
import { all, createLowlight } from 'lowlight';
import {
CodeBlockElement,
CodeLineElement,
CodeSyntaxLeaf,
} from '@/components/ui/code-block-node';
const lowlight = createLowlight(all);
export const CodeBlockKit = [
CodeBlockPlugin.configure({
node: { component: CodeBlockElement },
options: { lowlight },
shortcuts: { toggle: { keys: 'mod+alt+8' } },
}),
CodeLinePlugin.withComponent(CodeLineElement),
CodeSyntaxPlugin.withComponent(CodeSyntaxLeaf),
];
CodeBlockElement
: Renders code block containers.CodeLineElement
: Renders individual code lines.CodeSyntaxLeaf
: Renders syntax highlighted text.
Add Kit
Add the kit to your plugins:
import { createPlateEditor } from 'platejs/react';
import { CodeBlockKit } from '@/components/editor/plugins/code-block-kit';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
...CodeBlockKit,
],
});
Manual Usage
Installation
pnpm add @platejs/code-block lowlight
Add Plugins
Include the code block plugins in your Plate plugins array when creating the editor.
import { CodeBlockPlugin, CodeLinePlugin, CodeSyntaxPlugin } from '@platejs/code-block/react';
import { createPlateEditor } from 'platejs/react';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
CodeBlockPlugin,
CodeLinePlugin,
CodeSyntaxPlugin,
],
});
Configure Plugins
Configure the plugins with syntax highlighting and custom components.
Basic Setup with All Languages:
import { CodeBlockPlugin, CodeLinePlugin, CodeSyntaxPlugin } from '@platejs/code-block/react';
import { all, createLowlight } from 'lowlight';
import { createPlateEditor } from 'platejs/react';
import { CodeBlockElement, CodeLineElement, CodeSyntaxLeaf } from '@/components/ui/code-block-node';
// Create a lowlight instance with all languages
const lowlight = createLowlight(all);
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
CodeBlockPlugin.configure({
node: { component: CodeBlockElement },
options: { lowlight },
shortcuts: { toggle: { keys: 'mod+alt+8' } },
}),
CodeLinePlugin.withComponent(CodeLineElement),
CodeSyntaxPlugin.withComponent(CodeSyntaxLeaf),
],
});
Custom Language Setup (Optimized Bundle):
For optimized bundle size, you can register only specific languages:
import { createLowlight } from 'lowlight';
import css from 'highlight.js/lib/languages/css';
import js from 'highlight.js/lib/languages/javascript';
import ts from 'highlight.js/lib/languages/typescript';
import html from 'highlight.js/lib/languages/xml';
// Create a lowlight instance
const lowlight = createLowlight();
// Register only the languages you need
lowlight.register('html', html);
lowlight.register('css', css);
lowlight.register('js', js);
lowlight.register('ts', ts);
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
CodeBlockPlugin.configure({
node: { component: CodeBlockElement },
options: {
lowlight,
defaultLanguage: 'js', // Set default language (optional)
},
shortcuts: { toggle: { keys: 'mod+alt+8' } },
}),
CodeLinePlugin.withComponent(CodeLineElement),
CodeSyntaxPlugin.withComponent(CodeSyntaxLeaf),
],
});
node.component
: AssignsCodeBlockElement
to render code block containers.options.lowlight
: Lowlight instance for syntax highlighting.options.defaultLanguage
: Default language when no language is specified.shortcuts.toggle
: Defines a keyboard shortcut to toggle code blocks.withComponent
: Assigns components for code lines and syntax highlighting.
Turn Into Toolbar Button
You can add this item to the Turn Into Toolbar Button to convert blocks into code blocks:
{
icon: <FileCodeIcon />,
label: 'Code',
value: KEYS.codeBlock,
}
Insert Toolbar Button
You can add this item to the Insert Toolbar Button to insert code block elements:
{
icon: <FileCodeIcon />,
label: 'Code',
value: KEYS.codeBlock,
}
Plugins
CodeBlockPlugin
API
isCodeBlockEmpty
isSelectionAtCodeBlockStart
indentCodeLine
Indents the code line if the selection is expanded or there are no non-whitespace characters at left of the cursor. The indentation is 2 spaces by default.
insertCodeBlock
Inserts a code block by setting the node to code line and wrapping it with a code block. If the cursor is not at the block start, it inserts a break before the code block.
insertCodeLine
Inserts a code line starting with the specified indentation depth.
outdentCodeLine
Outdents a code line, removing two whitespace characters if present.
toggleCodeBlock
Toggles the code block in the editor.
unwrapCodeBlock
Unwraps the code block in the editor.
On This Page
FeaturesKit UsageInstallationAdd KitManual UsageInstallationAdd PluginsConfigure PluginsTurn Into Toolbar ButtonInsert Toolbar ButtonPluginsCodeBlockPluginAPIisCodeBlockEmptyisSelectionAtCodeBlockStartindentCodeLineinsertCodeBlockinsertCodeLineoutdentCodeLinetoggleCodeBlockunwrapCodeBlock