A heading extension for TipTap rich text editor providing h1-h6 HTML elements with configurable levels and keyboard shortcuts
npx @tessl/cli install tessl/npm-tiptap--extension-heading@3.4.0The TipTap Heading Extension provides HTML heading functionality (h1-h6) for TipTap rich text editors. It includes configurable heading levels, keyboard shortcuts, markdown-style input rules, and programmatic commands for setting and toggling headings.
npm install @tiptap/extension-headingimport { Heading } from "@tiptap/extension-heading";Default import:
import Heading from "@tiptap/extension-heading";Named imports:
import { Heading, Level, HeadingOptions } from "@tiptap/extension-heading";CommonJS:
const { Heading } = require("@tiptap/extension-heading");import { Editor } from "@tiptap/core";
import { Heading } from "@tiptap/extension-heading";
// Create editor with heading extension
const editor = new Editor({
extensions: [
Heading.configure({
levels: [1, 2, 3, 4, 5, 6], // Allow all heading levels
HTMLAttributes: {
class: 'my-heading',
},
}),
],
});
// Use commands programmatically
editor.commands.setHeading({ level: 1 }); // Set to h1
editor.commands.toggleHeading({ level: 2 }); // Toggle h2
// Keyboard shortcuts work automatically:
// Mod-Alt-1 through Mod-Alt-6 toggle heading levels
// Markdown input rules work automatically:
// Type "# " for h1, "## " for h2, etc.Creates a configurable heading node extension for TipTap editors with support for h1-h6 HTML elements.
import { Node } from "@tiptap/core";
/**
* Main heading extension that provides h1-h6 functionality
*/
const Heading: Node<HeadingOptions>;
/**
* Configuration options for the heading extension
*/
interface HeadingOptions {
/**
* Array of allowed heading levels (1-6)
* @default [1, 2, 3, 4, 5, 6]
*/
levels: Level[];
/**
* HTML attributes applied to all heading elements
* @default {}
*/
HTMLAttributes: Record<string, any>;
}
/**
* Valid heading levels corresponding to h1-h6 HTML elements
*/
type Level = 1 | 2 | 3 | 4 | 5 | 6;Programmatically sets the current selection to a heading with the specified level.
/**
* Sets current selection to a heading node with specified level
* @param attributes Object containing the heading level
* @returns Command execution result
*/
editor.commands.setHeading(attributes: { level: Level }): boolean;Usage Example:
// Set current line/selection to h1
editor.commands.setHeading({ level: 1 });
// Set to h3
editor.commands.setHeading({ level: 3 });Toggles between a heading of the specified level and a paragraph, or changes between heading levels.
/**
* Toggles between heading and paragraph, or changes heading level
* @param attributes Object containing the heading level to toggle
* @returns Command execution result
*/
editor.commands.toggleHeading(attributes: { level: Level }): boolean;Usage Example:
// Toggle h2 - if current node is h2, converts to paragraph
// If current node is paragraph or other heading, converts to h2
editor.commands.toggleHeading({ level: 2 });
// Toggle h1
editor.commands.toggleHeading({ level: 1 });The extension automatically registers keyboard shortcuts for each configured heading level:
Note: Mod key is Cmd on Mac and Ctrl on Windows/Linux.
The extension supports markdown-style input for creating headings:
# (hash + space) to create h1## (two hashes + space) to create h2### (three hashes + space) to create h3###### for h6The input rules create patterns for each configured heading level, where the exact number of hashes corresponds to the heading level (h1=1 hash, h2=2 hashes, etc.).
// Only allow h1, h2, and h3
const editor = new Editor({
extensions: [
Heading.configure({
levels: [1, 2, 3],
}),
],
});// Add CSS classes and other attributes
const editor = new Editor({
extensions: [
Heading.configure({
levels: [1, 2, 3, 4, 5, 6],
HTMLAttributes: {
class: 'prose-heading',
'data-heading': 'true',
},
}),
],
});// Configure for documentation with h1-h4 only
const editor = new Editor({
extensions: [
Heading.configure({
levels: [1, 2, 3, 4],
HTMLAttributes: {
class: 'doc-heading',
},
}),
],
});@tiptap/core as a peer dependency