tiptap_rusty_parser/
builder.rs1use crate::node::{Mark, Node};
4use serde_json::{Map, Value};
5
6impl Node {
7 #[inline]
17 pub fn element(node_type: impl Into<String>) -> Node {
18 Node {
19 node_type: Some(node_type.into()),
20 ..Default::default()
21 }
22 }
23
24 #[inline]
26 pub fn text(text: impl Into<String>) -> Node {
27 Node {
28 node_type: Some("text".into()),
29 text: Some(text.into()),
30 ..Default::default()
31 }
32 }
33
34 pub fn text_with_marks(text: impl Into<String>, marks: impl IntoIterator<Item = Mark>) -> Node {
36 Node {
37 node_type: Some("text".into()),
38 text: Some(text.into()),
39 marks: Some(marks.into_iter().collect()),
40 ..Default::default()
41 }
42 }
43
44 pub fn with_attr(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
48 self.attrs
49 .get_or_insert_with(Map::new)
50 .insert(key.into(), value.into());
51 self
52 }
53
54 pub fn with_child(mut self, node: Node) -> Self {
56 self.push_child(node);
57 self
58 }
59
60 pub fn with_children(mut self, nodes: impl IntoIterator<Item = Node>) -> Self {
62 self.children_mut().extend(nodes);
63 self
64 }
65
66 pub fn with_text(self, text: impl Into<String>) -> Self {
68 self.with_child(Node::text(text))
69 }
70
71 pub fn with_mark(mut self, mark: Mark) -> Self {
73 self.marks.get_or_insert_with(Vec::new).push(mark);
74 self
75 }
76}
77
78pub fn doc(children: impl IntoIterator<Item = Node>) -> Node {
86 Node {
87 node_type: Some("doc".into()),
88 content: Some(children.into_iter().collect()),
89 ..Default::default()
90 }
91}