Skip to content
gpdoc
Playground

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.

Terminal window
cargo add gpdoc-plugin-heading

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.

The renderer receives the heading node and its editable contents. Read the level from the node when choosing how to display the heading.

heading_block(...)
└── div() Your wrapper
├── label Level read from the heading node
└── block.content Editable heading text

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.

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));

Use Level::H1 through Level::H6 as the argument for each command. The document stores the level as an integer.

let level = Level::H2;

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))?;

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))?;
Shortcut Action
Mod + Alt + 1 through Mod + Alt + 6 Toggle the corresponding heading level.

heading/src/lib.rs