Code

PreviousNext

Add inline code formatting.

Code applies the code leaf mark to inline text. Use Code Block for fenced or multiline code; this page is only for inline code spans.

Loading…

Features

  • KEYS.code leaf mark.
  • Hard selection affinity.
  • HTML deserialization from code tags and Consolas font styles.
  • <code> rendering by default.
  • Registry CodeLeaf for styled inline code.
  • Optional Markdown-style input rule through CodeRules.

Kit Usage

Add Basic Marks

BasicMarksKit includes CodePlugin, CodeLeaf, the backtick input rule, and a mod+e shortcut.

'use client';
 
import {
  BoldRules,
  CodeRules,
  HighlightRules,
  ItalicRules,
  MarkComboRules,
  StrikethroughRules,
  SubscriptRules,
  SuperscriptRules,
  UnderlineRules,
} from '@platejs/basic-nodes';
import {
  BoldPlugin,
  CodePlugin,
  HighlightPlugin,
  ItalicPlugin,
  KbdPlugin,
  StrikethroughPlugin,
  SubscriptPlugin,
  SuperscriptPlugin,
  UnderlinePlugin,
} from '@platejs/basic-nodes/react';
 
import { CodeLeaf } from '@/components/ui/code-node';
import { HighlightLeaf } from '@/components/ui/highlight-node';
import { KbdLeaf } from '@/components/ui/kbd-node';
 
export const BasicMarksKit = [
  BoldPlugin.configure({
    inputRules: [
      BoldRules.markdown({ variant: '*' }),
      BoldRules.markdown({ variant: '_' }),
      MarkComboRules.markdown({ variant: 'boldItalic' }),
      MarkComboRules.markdown({ variant: 'boldUnderline' }),
      MarkComboRules.markdown({ variant: 'boldItalicUnderline' }),
      MarkComboRules.markdown({ variant: 'italicUnderline' }),
    ],
  }),
  ItalicPlugin.configure({
    inputRules: [
      ItalicRules.markdown({ variant: '*' }),
      ItalicRules.markdown({ variant: '_' }),
    ],
  }),
  UnderlinePlugin.configure({
    inputRules: [UnderlineRules.markdown()],
  }),
  CodePlugin.configure({
    inputRules: [CodeRules.markdown()],
    node: { component: CodeLeaf },
    shortcuts: { toggle: { keys: 'mod+e' } },
  }),
  StrikethroughPlugin.configure({
    inputRules: [StrikethroughRules.markdown()],
    shortcuts: { toggle: { keys: 'mod+shift+x' } },
  }),
  SubscriptPlugin.configure({
    inputRules: [SubscriptRules.markdown()],
    shortcuts: { toggle: { keys: 'mod+comma' } },
  }),
  SuperscriptPlugin.configure({
    inputRules: [SuperscriptRules.markdown()],
    shortcuts: { toggle: { keys: 'mod+period' } },
  }),
  HighlightPlugin.configure({
    inputRules: [
      HighlightRules.markdown({ variant: '==' }),
      HighlightRules.markdown({ variant: '≡' }),
    ],
    node: { component: HighlightLeaf },
    shortcuts: { toggle: { keys: 'mod+shift+h' } },
  }),
  KbdPlugin.withComponent(KbdLeaf),
];
'use client';
 
import {
  BoldRules,
  CodeRules,
  HighlightRules,
  ItalicRules,
  MarkComboRules,
  StrikethroughRules,
  SubscriptRules,
  SuperscriptRules,
  UnderlineRules,
} from '@platejs/basic-nodes';
import {
  BoldPlugin,
  CodePlugin,
  HighlightPlugin,
  ItalicPlugin,
  KbdPlugin,
  StrikethroughPlugin,
  SubscriptPlugin,
  SuperscriptPlugin,
  UnderlinePlugin,
} from '@platejs/basic-nodes/react';
 
import { CodeLeaf } from '@/components/ui/code-node';
import { HighlightLeaf } from '@/components/ui/highlight-node';
import { KbdLeaf } from '@/components/ui/kbd-node';
 
export const BasicMarksKit = [
  BoldPlugin.configure({
    inputRules: [
      BoldRules.markdown({ variant: '*' }),
      BoldRules.markdown({ variant: '_' }),
      MarkComboRules.markdown({ variant: 'boldItalic' }),
      MarkComboRules.markdown({ variant: 'boldUnderline' }),
      MarkComboRules.markdown({ variant: 'boldItalicUnderline' }),
      MarkComboRules.markdown({ variant: 'italicUnderline' }),
    ],
  }),
  ItalicPlugin.configure({
    inputRules: [
      ItalicRules.markdown({ variant: '*' }),
      ItalicRules.markdown({ variant: '_' }),
    ],
  }),
  UnderlinePlugin.configure({
    inputRules: [UnderlineRules.markdown()],
  }),
  CodePlugin.configure({
    inputRules: [CodeRules.markdown()],
    node: { component: CodeLeaf },
    shortcuts: { toggle: { keys: 'mod+e' } },
  }),
  StrikethroughPlugin.configure({
    inputRules: [StrikethroughRules.markdown()],
    shortcuts: { toggle: { keys: 'mod+shift+x' } },
  }),
  SubscriptPlugin.configure({
    inputRules: [SubscriptRules.markdown()],
    shortcuts: { toggle: { keys: 'mod+comma' } },
  }),
  SuperscriptPlugin.configure({
    inputRules: [SuperscriptRules.markdown()],
    shortcuts: { toggle: { keys: 'mod+period' } },
  }),
  HighlightPlugin.configure({
    inputRules: [
      HighlightRules.markdown({ variant: '==' }),
      HighlightRules.markdown({ variant: '≡' }),
    ],
    node: { component: HighlightLeaf },
    shortcuts: { toggle: { keys: 'mod+shift+h' } },
  }),
  KbdPlugin.withComponent(KbdLeaf),
];
import { createPlateEditor } from 'platejs/react';
 
import { BasicMarksKit } from '@/components/editor/plugins/basic-marks-kit';
 
export const editor = createPlateEditor({
  plugins: [...BasicMarksKit],
});
import { createPlateEditor } from 'platejs/react';
 
import { BasicMarksKit } from '@/components/editor/plugins/basic-marks-kit';
 
export const editor = createPlateEditor({
  plugins: [...BasicMarksKit],
});

Add A Toolbar Button

Use MarkToolbarButton with KEYS.code.

import { Code2Icon } from 'lucide-react';
import { KEYS } from 'platejs';
 
import { MarkToolbarButton } from '@/components/ui/mark-toolbar-button';
 
export function CodeToolbarButton() {
  return (
    <MarkToolbarButton nodeType={KEYS.code} tooltip="Code (⌘+E)">
      <Code2Icon />
    </MarkToolbarButton>
  );
}
import { Code2Icon } from 'lucide-react';
import { KEYS } from 'platejs';
 
import { MarkToolbarButton } from '@/components/ui/mark-toolbar-button';
 
export function CodeToolbarButton() {
  return (
    <MarkToolbarButton nodeType={KEYS.code} tooltip="Code (⌘+E)">
      <Code2Icon />
    </MarkToolbarButton>
  );
}

Manual Usage

Install the mark package.

pnpm add @platejs/basic-nodes
pnpm add @platejs/basic-nodes

Add CodePlugin directly when you want the default <code> render.

import { CodePlugin } from '@platejs/basic-nodes/react';
import { createPlateEditor } from 'platejs/react';
 
export const editor = createPlateEditor({
  plugins: [CodePlugin],
});
import { CodePlugin } from '@platejs/basic-nodes/react';
import { createPlateEditor } from 'platejs/react';
 
export const editor = createPlateEditor({
  plugins: [CodePlugin],
});

Configure the registry leaf, input rule, and shortcut when you want the same behavior as the kit.

import { CodeRules } from '@platejs/basic-nodes';
import { CodePlugin } from '@platejs/basic-nodes/react';
 
import { CodeLeaf } from '@/components/ui/code-node';
 
export const codePlugin = CodePlugin.configure({
  inputRules: [CodeRules.markdown()],
  node: { component: CodeLeaf },
  shortcuts: { toggle: { keys: 'mod+e' } },
});
import { CodeRules } from '@platejs/basic-nodes';
import { CodePlugin } from '@platejs/basic-nodes/react';
 
import { CodeLeaf } from '@/components/ui/code-node';
 
export const codePlugin = CodePlugin.configure({
  inputRules: [CodeRules.markdown()],
  node: { component: CodeLeaf },
  shortcuts: { toggle: { keys: 'mod+e' } },
});

Ownership

SurfaceOwnerWhat It Does
BaseCodePlugin@platejs/basic-nodesHeadless inline code mark, HTML parser, render tag, selection rule, and toggle transform.
CodePlugin@platejs/basic-nodes/reactReact wrapper for the headless code mark.
CodeRules.markdown@platejs/basic-nodesOptional backtick input rule.
CodeLeafRegistry UIStyled inline code leaf using PlateLeaf.
CodeLeafStaticRegistry UIStatic rendering version using SlateLeaf.
BasicMarksKitRegistryAdds CodePlugin, CodeLeaf, CodeRules.markdown(), and mod+e.
MarkToolbarButtonRegistry UIReads active mark state and calls the mark toggle hook.

The package owns inline code semantics. The registry owns visual styling and toolbar placement.

Behavior

BehaviorSource
Mark keyKEYS.code
Leaf behaviornode.isLeaf: true
Toggle transformeditor.tf.code.toggle() calls editor.tf.toggleMark(type).
Selection affinityhard
HTML tagscode
HTML stylesfont-family: Consolas
HTML guardSkips code inside pre and paragraph-level Consolas blocks.
Render outputcode
Kit shortcutmod+e

API Reference

APIPackageUse
BaseCodePlugin@platejs/basic-nodesHeadless inline code plugin.
CodePlugin@platejs/basic-nodes/reactReact inline code plugin.
CodeRules.markdown()@platejs/basic-nodesCreates the backtick mark input rule.
tf.code.toggle()@platejs/basic-nodesToggles the inline code mark at the selection.
CodeLeafRegistry UIStyled client inline code leaf.
CodeLeafStaticRegistry UIStyled static inline code leaf.