Skip to content
gpdoc
Playground

Trailing node

Keep an editable block at the end of a document after edits.

Trailing node appends a block when an edit leaves a different block type at the end of the document. An empty paragraph after a media block or heading gives the writer a place to continue typing.

Run this from a Quickstart project.

Terminal window
cargo add gpdoc-plugin-trailing-node

For this example, add gpdoc-plugin-heading to the Quickstart project. Turning the final paragraph into a heading leaves a new empty paragraph after it.

use gpdoc::{Editor, EditorConfig, Node};
use gpdoc_plugin_heading::heading;
use gpdoc_plugin_paragraph::paragraph;
use gpdoc_plugin_trailing_node::trailing_node;
let config = EditorConfig::builder()
.plugin(paragraph())
.plugin(heading())
.plugin(trailing_node())
.build()
.expect("configure editor");
let mut editor = Editor::from_document(&config, vec![Node::textblock("paragraph", "Release notes")])?;
editor.run(gpdoc_plugin_heading::Set(gpdoc_plugin_heading::Level::H1))?;
assert_eq!(
editor.document().blocks().last(),
Some(&Node::textblock("paragraph", "")),
);

The default constructor uses the document schema’s default block. To choose a specific block, pass its registered name as Some(String) to trailing_node_with. The schema must be able to fill its default content and attributes.

use gpdoc_plugin_trailing_node::trailing_node_with;
let plugin = trailing_node_with(Some("paragraph".into()), vec![]);

The second argument, not_after: Vec<String>, defaults to an empty list. List types that may end the document as they are. A final block of the configured trailing type also skips insertion.

let plugin = trailing_node_with(None, vec!["heading".into()]);

Opening existing content preserves its final block. The plugin checks the end of the document after an edit.

use gpdoc::Editor;
let initial = Editor::from_document(&config, vec![Node::textblock("heading", "Release notes")])?;
assert_eq!(initial.document().blocks().len(), 1);

The triggering edit and the appended paragraph share one history entry. After the heading conversion above, one undo restores the original paragraph.

editor.undo()?;
assert_eq!(editor.document().blocks(), [Node::textblock("paragraph", "Release notes")]);

trailing-node/src/lib.rs