# Ordered list

> Turn paragraphs into a numbered list and set its starting number.

`ordered_list()` wraps paragraphs in a numbered list. Its `start` attribute controls the number of the first item.

## Try it

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

## Install

```sh
cargo add gpdoc-plugin-ordered-list gpdoc-plugin-list-item
```

## Example

Register the numbered list and its item type. 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_list_item::list_item;
use gpdoc_plugin_ordered_list::{ordered_list, ordered_list_body};


let config = EditorConfig::builder()
    .plugin(paragraph())
    .plugin(list_item())
    .plugin(ordered_list().with_view(numbered_view))
    .build()
    .expect("configure editor");


fn numbered_view() -> ViewPlugin {
    ordered_list_body(|block, _, _| block.content)
}
```

Type `1.` followed by a space at the start of a paragraph. The editor creates a numbered item.

## Compose a numbered list

Wrap the whole list with `ordered_list_body`. The [list item view](/docs/plugins/structure/list-item/#compose-a-list-item) supplies each number from the starting counter and the item’s index.

### Anatomy

```text
ordered_list_body(...)
└── div()              Your wrapper
    └── block.content  Editable items with their decimal markers
```

### Wrap the numbered items

Replace `numbered_view` in the example above.

```rust
use gpui::{div, prelude::*};


fn numbered_view() -> ViewPlugin {
    ordered_list_body(|block, _, _| {
        div().child(block.content)
    })
}
```

## Start numbering at four

```rust
use gpdoc::{Editor, Node, Value};


let mut editor = Editor::from_document(&config, vec![Node::textblock("paragraph", "Publish the release")])?;
editor.run(gpdoc_plugin_ordered_list::Toggle { start: Some(4), ..Default::default() })?;


assert_eq!(editor.document().blocks()[0].attrs["start"], Value::from(4));
```

## Settings

Register `gpdoc_plugin_ordered_list::ordered_list()` with `paragraph()` and `list_item()`. Pass the following attributes when creating a list.

### `start`

The first number in the list. Defaults to integer `1`. Pass `4` to label the first two items 4 and 5.

### `type`

Optional string on the list node, default `null`. The typed-marker rule continues a preceding list when its type is `null` or `"1"` and the typed number is next in sequence.

## Commands

### `toggleOrderedList`

Wrap the current or selected paragraphs. Inside an ordered list, lift the selected items. Inside a compatible list of another type, convert it to an ordered list.

```rust
editor.run(gpdoc_plugin_ordered_list::Toggle { start: Some(4), ..Default::default() })?;
```

### `wrapInOrderedList`

Wrap each selected paragraph in a list item. A preceding ordered list absorbs the new items and supplies their numbering.

```rust
editor.run(gpdoc_plugin_ordered_list::Wrap::default())?;
```

## Keyboard shortcuts

| Shortcut            | Command             |
| ------------------- | ------------------- |
| **Mod + Shift + 7** | `toggleOrderedList` |

At the start of a paragraph, type a number, a period, and a space. After a list containing items 4 and 5, typing `6.` followed by a space continues that list. Typing `1.` followed by a space starts a separate list.

## Source code

[ordered-list/src/lib.rs](/source/plugins/ordered-list/src/lib.rs.txt)