Slash command
Choose a block command by typing a slash at the start of a top-level paragraph.
Type / at the start of a top-level paragraph to open the block menu. Filter the choices by typing the beginning of a label or alias.
Try it
Section titled “Try it”Click the empty paragraph and type /h1. Press Enter, then type a title in the new heading.
Install
Section titled “Install”cargo add gpdoc-plugin-slash-command gpdoc-plugin-headingExample
Section titled “Example”Register a slash command that turns the current paragraph into a heading. Render its label with the picker’s built-in list in the Quickstart editor.
use gpdoc::{EditorConfig, PluginViewExt, StoredCommand, ViewPlugin};use gpdoc_plugin_heading::{Level, Set, heading};use gpdoc_plugin_paragraph::paragraph;use gpdoc_plugin_slash_command::{SlashCommand, SlashMenu, slash_commands, slash_picker};
let entries = [SlashCommand { id: "heading.h1", label: "Heading 1", aliases: &["h1"], command: StoredCommand::new(Set(Level::H1)),}];
let config = EditorConfig::builder() .plugin(paragraph()) .plugin(heading()) .plugin(slash_commands(entries).with_view(heading_menu)) .build() .expect("configure editor");
fn heading_menu() -> ViewPlugin { slash_picker(|menu: SlashMenu, _, _| menu.labeled_list(|entry| entry.label.to_owned()))}Type /h1 in an empty paragraph and press Enter to turn it into a level-one heading.
Compose a slash menu
Section titled “Compose a slash menu”The picker filters the commands, tracks the active row, and handles arrow keys, Enter, and Escape.
Anatomy
Section titled “Anatomy”slash_picker(...) Finds commands matching the typed label or alias└── menu.popover(...) Anchors your list below the caret └── menu.list(6, ...) Keeps the active row within six visible matches └── row.element() Accepts the command on click and keeps editor focusShow a search alias beside each command
Section titled “Show a search alias beside each command”Replace heading_menu in the example. Put the label and first alias inside the row’s click target.
use gpui::prelude::*;
fn heading_menu() -> ViewPlugin { slash_picker(|menu: SlashMenu, _, _| { let list = menu.list(6, |row| { let label = match row.value.aliases.first() { Some(alias) => format!("{} /{alias}", row.value.label), None => row.value.label.to_owned(), }; row.element().child(label) }); menu.popover(list) })}Type /h1 and click the row labeled Heading 1 /h1. Use row.is_active() to identify the row that Enter will accept.
Add menu items from another plugin
Section titled “Add menu items from another plugin”To contribute entries from a plugin, register its catalog with PluginSpec::metadata(SlashCatalog::new(entries)). Register slash_commands([]) once to display the catalogs with the default list, or attach the same custom menu with .with_view(heading_menu).