satteri_plugin_api/
commands.rs1use satteri_ast::mdast::MdastNodeType;
2
3#[derive(Debug, Clone)]
6pub enum Command {
7 Replace { node_id: u32, new_node: NewNode },
9 Remove { node_id: u32 },
11 InsertBefore { node_id: u32, new_node: NewNode },
13 InsertAfter { node_id: u32, new_node: NewNode },
15 Wrap { node_id: u32, parent_node: NewNode },
17 PrependChild { node_id: u32, child_node: NewNode },
19 AppendChild { node_id: u32, child_node: NewNode },
21 SetData {
23 node_id: u32,
24 key: String,
25 value: crate::data::DataValue,
26 },
27}
28
29#[derive(Debug, Clone)]
32pub enum NewNode {
33 Raw(String),
35 Built(BuiltNode),
37}
38
39#[derive(Debug, Clone)]
41pub struct BuiltNode {
42 pub node_type: MdastNodeType,
43 pub children: Vec<NewNode>,
44 pub data_bytes: Vec<u8>,
46 pub position: Option<crate::typed_nodes::NodePosition>,
48}
49
50pub struct NodeBuilder {
52 node_type: MdastNodeType,
53 children: Vec<NewNode>,
54 data_bytes: Vec<u8>,
55}
56
57impl NodeBuilder {
58 pub fn new(node_type: MdastNodeType) -> Self {
59 Self {
60 node_type,
61 children: Vec::new(),
62 data_bytes: Vec::new(),
63 }
64 }
65
66 pub fn child(mut self, child: NewNode) -> Self {
68 self.children.push(child);
69 self
70 }
71
72 pub fn children(mut self, children: impl IntoIterator<Item = NewNode>) -> Self {
74 self.children.extend(children);
75 self
76 }
77
78 pub fn data_bytes(mut self, bytes: Vec<u8>) -> Self {
80 self.data_bytes = bytes;
81 self
82 }
83
84 pub fn build(self) -> NewNode {
86 NewNode::Built(BuiltNode {
87 node_type: self.node_type,
88 children: self.children,
89 data_bytes: self.data_bytes,
90 position: None,
91 })
92 }
93}
94
95impl NodeBuilder {
97 pub fn heading(depth: u8) -> Self {
98 use satteri_ast::mdast::codec::encode_heading_data;
99 Self::new(MdastNodeType::Heading).data_bytes(encode_heading_data(depth))
100 }
101
102 pub fn paragraph() -> Self {
103 Self::new(MdastNodeType::Paragraph)
104 }
105
106 pub fn text(value_offset: u32, value_len: u32) -> Self {
107 use satteri_arena::{StringRef, encode_string_ref_data};
108 let string_ref = StringRef {
109 offset: value_offset,
110 len: value_len,
111 };
112 Self::new(MdastNodeType::Text).data_bytes(encode_string_ref_data(string_ref))
113 }
114
115 pub fn raw(markdown: impl Into<String>) -> NewNode {
118 NewNode::Raw(markdown.into())
119 }
120}