Skip to content

Code

The Code mark applies inline code formatting to text. It renders as <code> and is an exclusive mark, meaning no other marks (bold, italic, etc.) can overlap with it. Included in StarterKit by default.

Select text and apply inline code with buttons, keyboard shortcut (Cmd+E / Ctrl+E), or type `text` to trigger the input rule.

With the default theme enabled. Click the code toolbar button or use Cmd+E / Ctrl+E.

Click to try it out

Code is included in StarterKit, so it works out of the box. To use it standalone:

import { Editor, Document, Paragraph, Text, Code, defaultIcons } from '@domternal/core';
import '@domternal/theme';
const editorEl = document.getElementById('editor')!;
// Toolbar
const toolbar = document.createElement('div');
toolbar.className = 'dm-toolbar';
toolbar.innerHTML = `<div class="dm-toolbar-group">
<button class="dm-toolbar-button" data-mark="code">${defaultIcons.code}</button>
</div>`;
editorEl.before(toolbar);
// Editor
const editor = new Editor({
element: editorEl,
extensions: [Document, Paragraph, Text, Code],
content: '<p>Use <code>console.log()</code> to debug.</p>',
});
// Toggle code on click
toolbar.addEventListener('click', (e) => {
const btn = (e.target as Element).closest<HTMLButtonElement>('[data-mark]');
if (btn) editor.chain().focus().toggleCode().run();
});

To disable Code in StarterKit:

StarterKit.configure({ code: false })
PropertyValue
ProseMirror namecode
TypeMark
HTML tag<code>
Excludes_ (all other marks)
Spanningfalse

The spanning: false property means the code mark does not span across multiple inline nodes. Each code segment is self-contained.

SourceCondition
<code>Always
OptionTypeDefaultDescription
HTMLAttributesRecord<string, unknown>{}Custom HTML attributes added to the rendered <code> element
import { Code } from '@domternal/core';
const CustomCode = Code.configure({
HTMLAttributes: { class: 'my-code' },
});

Applies inline code formatting to the current selection.

editor.commands.setCode();

Removes inline code formatting from the current selection.

editor.commands.unsetCode();

Toggles inline code formatting on the current selection. Applies code if not active, removes it if active.

editor.commands.toggleCode();
// With chaining
editor.chain().focus().toggleCode().run();
KeyAction
Mod-EToggle inline code (toggleCode)

Mod is Cmd on macOS and Ctrl on Windows/Linux.

InputResult
`text`Wraps text in inline code

Type the closing backtick to trigger the conversion. The backticks are removed and the text between them becomes inline code.

Code registers a button in the toolbar with the name code in group format at priority 160.

ItemTypeCommandIconShortcutActive when
CodebuttontoggleCodecodeMod-ECode mark is active on selection
import { Code } from '@domternal/core';
import type { CodeOptions } from '@domternal/core';
ExportTypeDescription
CodeMark extensionThe inline code mark extension
CodeOptionsTypeScript typeOptions for Code.configure()

@domternal/core - Code.ts