Inline code mark extension for the Tiptap rich text editor framework
npx @tessl/cli install tessl/npm-tiptap--extension-code@3.4.0The @tiptap/extension-code package provides an inline code mark extension for the Tiptap rich text editor framework. It enables users to mark text as inline code using backticks (`code`) with keyboard shortcuts (Mod-e), automatically parses inline code blocks from input and paste operations, and renders them as HTML <code> elements.
npm install @tiptap/extension-codeimport { Code } from "@tiptap/extension-code";For default import:
import Code from "@tiptap/extension-code";For importing specific components:
import { Code, CodeOptions, inputRegex, pasteRegex } from "@tiptap/extension-code";CommonJS:
const { Code } = require("@tiptap/extension-code");import { Editor } from "@tiptap/core";
import { Code } from "@tiptap/extension-code";
// Initialize editor with Code extension
const editor = new Editor({
extensions: [
Code.configure({
HTMLAttributes: {
class: "my-custom-code",
},
}),
],
content: "<p>Here is some <code>inline code</code> in the text.</p>",
});
// Programmatically toggle code mark
editor.commands.toggleCode();
// Set code mark on selected text
editor.commands.setCode();
// Remove code mark from selected text
editor.commands.unsetCode();The extension integrates with Tiptap's mark system and provides:
Mark class to create inline code marksMain extension class that creates inline code marks in the Tiptap editor.
/**
* This extension allows you to mark text as inline code.
* @see https://tiptap.dev/api/marks/code
*/
declare const Code: Mark<CodeOptions>;Configure the Code extension with custom options.
interface CodeOptions {
/**
* The HTML attributes applied to the code element.
* @default {}
* @example { class: 'foo' }
*/
HTMLAttributes: Record<string, any>;
}Usage Example:
import { Editor } from "@tiptap/core";
import { Code } from "@tiptap/extension-code";
const editor = new Editor({
extensions: [
Code.configure({
HTMLAttributes: {
class: "inline-code",
spellcheck: "false",
},
}),
],
});The Code extension adds three commands to the editor's command interface.
declare module '@tiptap/core' {
interface Commands<ReturnType> {
code: {
/**
* Set a code mark
*/
setCode: () => ReturnType;
/**
* Toggle inline code
*/
toggleCode: () => ReturnType;
/**
* Unset a code mark
*/
unsetCode: () => ReturnType;
};
}
}Usage Examples:
// Toggle code formatting (most common usage)
editor.commands.toggleCode();
// Apply code formatting to selected text
editor.commands.setCode();
// Remove code formatting from selected text
editor.commands.unsetCode();
// Check if current selection has code formatting
const isActive = editor.isActive('code');Built-in keyboard shortcuts for quick code formatting.
Automatic parsing of backtick syntax while typing, powered by regular expressions.
/**
* Regular expression to match inline code blocks enclosed in backticks.
* Matches an opening backtick, followed by any text that doesn't include
* a backtick (captured for marking), followed by a closing backtick.
*/
declare const inputRegex: RegExp;Pattern: /(^|[^])([^]+)(?!)$/`
Usage: Type `text` and it will automatically be converted to inline code formatting.
Recognition of backtick syntax when pasting content from external sources.
/**
* Regular expression to match inline code while pasting content.
*/
declare const pasteRegex: RegExp;Pattern: /(^|[^])([^]+)(?!)/g`
Usage: Paste text containing `code` syntax and it will be automatically formatted as inline code.
The extension handles HTML parsing and rendering for <code> elements.
HTML Parsing: Recognizes existing <code> elements when loading content
HTML Rendering: Outputs semantic <code> HTML elements with customizable attributes
<!-- Input HTML -->
<p>This is <code>inline code</code> text.</p>
<!-- Rendered with custom attributes -->
<p>This is <code class="my-custom-code">inline code</code> text.</p>The Code extension has specific mark properties that control its behavior:
'_' - Excludes other marks with underscore notationtrue - Identifies this as a code mark for editor logictrue - Allows cursor to exit the mark boundariesinterface CodeOptions {
HTMLAttributes: Record<string, any>;
}
// Regular expression constants
declare const inputRegex: RegExp;
declare const pasteRegex: RegExp;
// Extension class
declare const Code: Mark<CodeOptions>;