Heading
Turn text blocks into headings with levels 1 through 6.
heading() adds headings with a numeric level attribute. Use headings for section titles and to give Table of contents entries their depth.
Try it
Section titled “Try it”Install
Section titled “Install”cargo add gpdoc-plugin-headingExample
Section titled “Example”Register paragraphs and headings so users can toggle between them. Use this configuration in the Quickstart editor.
use gpdoc::{EditorConfig, PluginViewExt, ViewPlugin};use gpdoc_plugin_paragraph::paragraph;use gpdoc_plugin_heading::{heading, heading_block};
let config = EditorConfig::builder() .plugin(paragraph()) .plugin(heading().with_view(heading_view)) .build() .expect("configure editor");
fn heading_view() -> ViewPlugin { heading_block(|block, _, _| block.content)}Press Mod + Alt + 2 in a paragraph to turn it into a level-two heading.
Compose a heading
Section titled “Compose a heading”The renderer receives the heading node and its editable contents. Read the level from the node when choosing how to display the heading.
Anatomy
Section titled “Anatomy”heading_block(...)└── div() Your wrapper ├── label Level read from the heading node └── block.content Editable heading textRead the heading level
Section titled “Read the heading level”Replace heading_view in the example above.
use gpui::{div, prelude::*};
fn heading_view() -> ViewPlugin { heading_block(|block, _, _| { let level = block.node.attrs.get("level").and_then(|value| value.as_u64()).unwrap_or(1); div().flex().child(format!("H{level}")).child(block.content) })}Use block.node.attrs to read the level. This wrapper displays the level beside the editable heading.
Convert a paragraph to a level 2 heading
Section titled “Convert a paragraph to a level 2 heading”use gpdoc::{Editor, Node, Value};use gpdoc_plugin_heading::{Level, Set};
let mut editor = Editor::from_document(&config, vec![Node::textblock("paragraph", "Release notes")])?;editor.run(Set(Level::H2))?;assert_eq!(editor.document().blocks()[0].attrs["level"], Value::from(2));Settings
Section titled “Settings”Use Level::H1 through Level::H6 as the argument for each command. The document stores the level as an integer.
let level = Level::H2;Commands
Section titled “Commands”setHeading
Section titled “setHeading”Apply one heading level to the block at the caret or to all selected text blocks where their parent permits a heading.
let level = Level::H2;editor.run(Set(level))?;toggleHeading
Section titled “toggleHeading”If every selected block is a heading of the requested level, convert them to paragraphs. Otherwise, apply the level.
let level = Level::H2;editor.run(gpdoc_plugin_heading::Toggle(level))?;Keyboard shortcuts
Section titled “Keyboard shortcuts”| Shortcut | Action |
|---|---|
| Mod + Alt + 1 through Mod + Alt + 6 | Toggle the corresponding heading level. |