# 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](/docs/plugins/behavior/table-of-contents/) entries their depth.

## Try it

[Try the heading editor example](https://gpdoc.dev/docs/plugins/structure/heading/#heading-example)

## Install

```sh
cargo add gpdoc-plugin-heading
```

## Example

Register paragraphs and headings so users can toggle between them. Use this configuration in the [Quickstart editor](/docs/quickstart/#configure-the-editor).

```rust
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

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

### Anatomy

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

### Read the heading level

Replace `heading_view` in the example above.

```rust
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

```rust
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

### `level`

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

```rust
let level = Level::H2;
```

## Commands

### `setHeading`

Apply one heading level to the block at the caret or to all selected text blocks where their parent permits a heading.

```rust
let level = Level::H2;
editor.run(Set(level))?;
```

### `toggleHeading`

If every selected block is a heading of the requested level, convert them to paragraphs. Otherwise, apply the level.

```rust
let level = Level::H2;
editor.run(gpdoc_plugin_heading::Toggle(level))?;
```

## Keyboard shortcuts

| Shortcut                                    | Action                                  |
| ------------------------------------------- | --------------------------------------- |
| **Mod + Alt + 1** through **Mod + Alt + 6** | Toggle the corresponding heading level. |

## Source code

[heading/src/lib.rs](/source/plugins/heading/src/lib.rs.txt)