Create your first plugin
Create a Rust plugin that adds an editable caption block and test it without opening a window.
Define a caption block and open it in a headless editor. The examples use gpdoc-writing as the plugin crate name.
Add the dependencies
Section titled “Add the dependencies”Add the plugin toolkit to your crate. The caption test uses the paragraph plugin as a development dependency.
cargo add gpdoc_plugincargo add --dev gpdoc gpdoc-plugin-paragraphDefine a caption node
Section titled “Define a caption node”Add the caption function to src/lib.rs.
use gpdoc_plugin::prelude::{NodeSpec, Plugin};
pub fn caption() -> Plugin { Plugin::new("caption", |p| { p.node(NodeSpec::block("caption").group("block").content("inline*")); })}The block group lets a caption appear wherever the document accepts a block. The inline* content expression lets the caption contain text and other inline nodes, or be empty.
Create tests/caption.rs and run cargo test. The paragraph plugin supplies the config’s default text block.
use gpdoc::{Editor, EditorConfig, Error, Node};use gpdoc_plugin_paragraph::paragraph;use gpdoc_writing::caption;
#[test]fn open_a_caption() -> Result<(), Error> { let config = EditorConfig::builder() .plugin(paragraph()) .plugin(caption()) .build()?; let editor = Editor::from_document(&config, vec![Node::textblock("caption", "Figure 1")])?; let block = &editor.document().blocks()[0]; assert_eq!(block.kind, "caption"); assert_eq!(block.child(0).projected_text(), "Figure 1"); Ok(())}The passing test confirms that the config accepts a caption at the document root and retains its text. The plugin can define its node without opening a window or depending on GPUI.
To add formatting and caption attributes, continue with Nodes, marks, and attributes. To implement an editing action, start with Commands and shortcuts.