turbovault-parser 4.0.1

Obsidian Flavored Markdown (OFM) parser
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! JSON Canvas files (`.canvas`), as Obsidian's Canvas core plugin writes
//! them.
//!
//! Follows the JSON Canvas 1.0 spec (<https://jsoncanvas.org/spec/1.0/>): a
//! document has optional `nodes` and `edges` arrays. Every node has an `id`,
//! a `type` (`text`, `file`, `link` or `group`), integer `x`, `y`, `width`
//! and `height`, and an optional `color`; a text node adds `text`, a file
//! node `file` (a vault path) and an optional `subpath` starting with `#`, a
//! link node `url`, and a group an optional `label`, `background` and
//! `backgroundStyle` (`cover`, `ratio` or `repeat`). An edge has an `id`,
//! `fromNode` and `toNode`, and optional `fromSide`/`toSide` (`top`, `right`,
//! `bottom`, `left`), `fromEnd`/`toEnd` (`none` or `arrow`), `color` and
//! `label`. A color is a hex string or a preset `"1"` to `"6"`.
//!
//! Fields this model doesn't name (a plugin's own, or a later spec's) are
//! kept as they are, so reading a canvas and writing it back loses nothing.
//! Coordinates are kept as the JSON numbers they were written as.

use serde::{Deserialize, Serialize};
use serde_json::{Map, Number, Value};

/// A whole canvas document.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Canvas {
    /// The canvas's nodes, in drawing order.
    #[serde(default)]
    pub nodes: Vec<CanvasNode>,
    /// The connections between nodes.
    #[serde(default)]
    pub edges: Vec<CanvasEdge>,
    /// Top-level fields beyond `nodes` and `edges`, kept as written.
    #[serde(flatten)]
    pub extra: Map<String, Value>,
}

/// One node on a canvas. Which of the type-specific fields apply depends on
/// `kind`; see the module docs. Fields are in the order Obsidian writes them.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct CanvasNode {
    /// Unique among the canvas's nodes and edges.
    pub id: String,
    /// `text`, `file`, `link` or `group`.
    #[serde(rename = "type")]
    pub kind: String,
    /// A text node's markdown.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    /// A file node's vault path, with its extension.
    pub file: Option<String>,
    /// A file node's heading or block within the file, starting with `#`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub subpath: Option<String>,
    /// A link node's URL.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    /// A group node's label.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    /// A group node's background image path.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub background: Option<String>,
    #[serde(
        default,
        rename = "backgroundStyle",
        skip_serializing_if = "Option::is_none"
    )]
    /// How a group's background image is laid out: `cover`, `ratio` or
    /// `repeat`.
    pub background_style: Option<String>,
    /// Left edge, in canvas pixels.
    pub x: Number,
    /// Top edge, in canvas pixels.
    pub y: Number,
    /// Width, in canvas pixels.
    pub width: Number,
    /// Height, in canvas pixels.
    pub height: Number,
    /// A preset color `1` to `6`, or a hex color.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub color: Option<String>,
    /// Fields the spec doesn't define, kept as written.
    #[serde(flatten)]
    pub extra: Map<String, Value>,
}

/// One edge between two nodes.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct CanvasEdge {
    /// Unique among the canvas's nodes and edges.
    pub id: String,
    /// The id of the node the edge starts at.
    #[serde(rename = "fromNode")]
    pub from_node: String,
    /// The side it leaves from: `top`, `right`, `bottom` or `left`.
    #[serde(default, rename = "fromSide", skip_serializing_if = "Option::is_none")]
    pub from_side: Option<String>,
    /// The start's shape: `none` or `arrow`.
    #[serde(default, rename = "fromEnd", skip_serializing_if = "Option::is_none")]
    pub from_end: Option<String>,
    /// The id of the node the edge ends at.
    #[serde(rename = "toNode")]
    pub to_node: String,
    /// The side it arrives at: `top`, `right`, `bottom` or `left`.
    #[serde(default, rename = "toSide", skip_serializing_if = "Option::is_none")]
    pub to_side: Option<String>,
    /// The end's shape: `none` or `arrow`.
    #[serde(default, rename = "toEnd", skip_serializing_if = "Option::is_none")]
    pub to_end: Option<String>,
    /// A preset color `1` to `6`, or a hex color.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub color: Option<String>,
    /// Text shown on the edge.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    /// Fields the spec doesn't define, kept as written.
    #[serde(flatten)]
    pub extra: Map<String, Value>,
}

/// A note or file a canvas points at: a file node, or a link written in a
/// text node.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CanvasReference {
    /// A file node: `file` is a vault path, with its extension.
    File {
        /// The file node's id.
        node_id: String,
        /// The vault path it shows.
        path: String,
        /// The heading or block within the file, if any.
        subpath: Option<String>,
    },
    /// A wikilink, embed or Markdown link inside a text node.
    Text {
        /// The text node's id.
        node_id: String,
        /// The link, with its span in the node's text.
        link: turbovault_core::Link,
    },
}

/// Parse a canvas document. An empty file is an empty canvas, as Obsidian
/// treats it.
pub fn parse_canvas(json: &str) -> Result<Canvas, String> {
    if json.trim().is_empty() {
        return Ok(Canvas::default());
    }
    serde_json::from_str(json).map_err(|error| format!("not a JSON Canvas document: {error}"))
}

impl Canvas {
    /// Everything that keeps this document from being valid JSON Canvas
    /// 1.0, one message per problem; empty when it is valid. Checks what the
    /// spec requires: unique ids across nodes and edges, the fields each
    /// node type needs, integer coordinates and sizes, edges that join
    /// existing nodes, and the enumerated values (`type`, sides, ends,
    /// `backgroundStyle`, colors).
    pub fn problems(&self) -> Vec<String> {
        let mut problems = Vec::new();
        let mut ids = std::collections::HashSet::new();
        for node in &self.nodes {
            if node.id.is_empty() {
                problems.push("a node has an empty id".to_string());
            } else if !ids.insert(node.id.as_str()) {
                problems.push(format!("id {:?} is used more than once", node.id));
            }
            let required: &[(&str, bool)] = match node.kind.as_str() {
                "text" => &[("text", node.text.is_some())],
                "file" => &[("file", node.file.is_some())],
                "link" => &[("url", node.url.is_some())],
                "group" => &[],
                other => {
                    problems.push(format!(
                        "node {:?} has type {other:?}; expected text, file, link or group",
                        node.id
                    ));
                    &[]
                }
            };
            for (field, present) in required {
                if !present {
                    problems.push(format!("{} node {:?} has no {field}", node.kind, node.id));
                }
            }
            for (field, value) in [
                ("x", &node.x),
                ("y", &node.y),
                ("width", &node.width),
                ("height", &node.height),
            ] {
                if !(value.is_i64() || value.is_u64()) {
                    problems.push(format!(
                        "node {:?} has a non-integer {field} ({value})",
                        node.id
                    ));
                }
            }
            if let Some(subpath) = &node.subpath
                && !subpath.starts_with('#')
            {
                problems.push(format!(
                    "node {:?} has subpath {subpath:?}, which must start with #",
                    node.id
                ));
            }
            if let Some(style) = &node.background_style
                && !matches!(style.as_str(), "cover" | "ratio" | "repeat")
            {
                problems.push(format!(
                    "node {:?} has backgroundStyle {style:?}; expected cover, ratio or repeat",
                    node.id
                ));
            }
            if let Some(color) = &node.color
                && !is_canvas_color(color)
            {
                problems.push(format!("node {:?} has color {color:?}", node.id));
            }
        }
        for edge in &self.edges {
            if edge.id.is_empty() {
                problems.push("an edge has an empty id".to_string());
            } else if !ids.insert(edge.id.as_str()) {
                problems.push(format!("id {:?} is used more than once", edge.id));
            }
            for (end, node) in [("fromNode", &edge.from_node), ("toNode", &edge.to_node)] {
                if !self.nodes.iter().any(|n| &n.id == node) {
                    problems.push(format!(
                        "edge {:?} has {end} {node:?}, which is no node on the canvas",
                        edge.id
                    ));
                }
            }
            for (field, side) in [("fromSide", &edge.from_side), ("toSide", &edge.to_side)] {
                if let Some(side) = side
                    && !matches!(side.as_str(), "top" | "right" | "bottom" | "left")
                {
                    problems.push(format!(
                        "edge {:?} has {field} {side:?}; expected top, right, bottom or left",
                        edge.id
                    ));
                }
            }
            for (field, end) in [("fromEnd", &edge.from_end), ("toEnd", &edge.to_end)] {
                if let Some(end) = end
                    && !matches!(end.as_str(), "none" | "arrow")
                {
                    problems.push(format!(
                        "edge {:?} has {field} {end:?}; expected none or arrow",
                        edge.id
                    ));
                }
            }
            if let Some(color) = &edge.color
                && !is_canvas_color(color)
            {
                problems.push(format!("edge {:?} has color {color:?}", edge.id));
            }
        }
        problems
    }

    /// The notes and files this canvas points at: each file node, and each
    /// wikilink, embed or internal Markdown link in a text node.
    pub fn references(&self) -> Vec<CanvasReference> {
        let mut references = Vec::new();
        for node in &self.nodes {
            match node.kind.as_str() {
                "file" => {
                    if let Some(path) = &node.file {
                        references.push(CanvasReference::File {
                            node_id: node.id.clone(),
                            path: path.clone(),
                            subpath: node.subpath.clone(),
                        });
                    }
                }
                "text" => {
                    let Some(text) = &node.text else { continue };
                    let mut links = crate::parse_wikilinks(text);
                    links.extend(crate::parse_embeds(text));
                    links.extend(
                        crate::parse_markdown_links(text)
                            .into_iter()
                            .filter(|link| {
                                !matches!(
                                    link.type_,
                                    turbovault_core::LinkType::ExternalLink
                                        | turbovault_core::LinkType::Anchor
                                )
                            }),
                    );
                    references.extend(links.into_iter().map(|link| CanvasReference::Text {
                        node_id: node.id.clone(),
                        link,
                    }));
                }
                _ => {}
            }
        }
        references
    }

    /// The document as Obsidian writes it: tab-indented, one node or edge
    /// per line, so a change shows up in a diff as the lines it touched.
    pub fn to_obsidian_json(&self) -> String {
        fn line<T: Serialize>(item: &T) -> String {
            serde_json::to_string(item).unwrap_or_default()
        }
        let mut out = String::from("{\n");
        let mut sections: Vec<String> = Vec::new();
        for (key, items) in [
            ("nodes", self.nodes.iter().map(line).collect::<Vec<_>>()),
            ("edges", self.edges.iter().map(line).collect::<Vec<_>>()),
        ] {
            if items.is_empty() {
                sections.push(format!("\t\"{key}\":[]"));
            } else {
                sections.push(format!("\t\"{key}\":[\n\t\t{}\n\t]", items.join(",\n\t\t")));
            }
        }
        for (key, value) in &self.extra {
            sections.push(format!("\t{}:{}", line(key), line(value)));
        }
        out.push_str(&sections.join(",\n"));
        out.push_str("\n}");
        out
    }
}

/// A preset (`"1"` to `"6"`) or a `#RGB` / `#RRGGBB` hex color.
fn is_canvas_color(color: &str) -> bool {
    if matches!(color, "1" | "2" | "3" | "4" | "5" | "6") {
        return true;
    }
    color
        .strip_prefix('#')
        .is_some_and(|hex| matches!(hex.len(), 3 | 6) && hex.chars().all(|c| c.is_ascii_hexdigit()))
}

#[cfg(test)]
mod tests {
    use super::*;

    const SAMPLE: &str = r##"{
	"nodes":[
		{"id":"a","type":"text","text":"See [[Plan]] and [x](notes/Idea.md)","x":0,"y":0,"width":250,"height":60},
		{"id":"b","type":"file","file":"notes/Plan.md","subpath":"#Goals","x":300,"y":0,"width":400,"height":400,"color":"4"},
		{"id":"c","type":"link","url":"https://example.com","x":0,"y":500,"width":250,"height":60},
		{"id":"g","type":"group","label":"Ideas","x":-50,"y":-50,"width":900,"height":700,"plugin-field":true}
	],
	"edges":[
		{"id":"e1","fromNode":"a","fromSide":"right","toNode":"b","toSide":"left","label":"plans"}
	]
}"##;

    #[test]
    fn reads_every_node_type_and_keeps_unknown_fields() {
        let canvas = parse_canvas(SAMPLE).unwrap();
        assert_eq!(canvas.nodes.len(), 4);
        assert_eq!(canvas.edges[0].from_node, "a");
        assert_eq!(canvas.nodes[3].extra["plugin-field"], Value::Bool(true));
        assert!(canvas.problems().is_empty(), "{:?}", canvas.problems());
    }

    #[test]
    fn writes_back_what_it_read() {
        let canvas = parse_canvas(SAMPLE).unwrap();
        let written = canvas.to_obsidian_json();
        assert_eq!(written, SAMPLE);
        assert_eq!(parse_canvas(&written).unwrap(), canvas);
    }

    #[test]
    fn an_empty_file_is_an_empty_canvas() {
        assert_eq!(parse_canvas("").unwrap(), Canvas::default());
        assert_eq!(
            Canvas::default().to_obsidian_json(),
            "{\n\t\"nodes\":[],\n\t\"edges\":[]\n}"
        );
    }

    #[test]
    fn references_are_file_nodes_and_links_in_text() {
        let canvas = parse_canvas(SAMPLE).unwrap();
        let references = canvas.references();
        assert!(references.contains(&CanvasReference::File {
            node_id: "b".into(),
            path: "notes/Plan.md".into(),
            subpath: Some("#Goals".into()),
        }));
        let text_targets: Vec<&str> = references
            .iter()
            .filter_map(|r| match r {
                CanvasReference::Text { link, .. } => Some(link.target.as_str()),
                _ => None,
            })
            .collect();
        assert_eq!(text_targets, ["Plan", "notes/Idea.md"]);
    }

    #[test]
    fn problems_name_what_the_spec_requires() {
        let canvas = parse_canvas(
            r#"{"nodes":[
                {"id":"a","type":"text","x":0,"y":0,"width":1.5,"height":1},
                {"id":"a","type":"file","file":"x.md","subpath":"Goals","x":0,"y":0,"width":1,"height":1,"color":"9"},
                {"id":"s","type":"shape","x":0,"y":0,"width":1,"height":1}
            ],"edges":[
                {"id":"e","fromNode":"a","toNode":"missing","toEnd":"circle","fromSide":"middle"}
            ]}"#,
        )
        .unwrap();
        let problems = canvas.problems().join("\n");
        for expected in [
            "text node \"a\" has no text",
            "non-integer width",
            "id \"a\" is used more than once",
            "subpath \"Goals\"",
            "color \"9\"",
            "type \"shape\"",
            "toNode \"missing\"",
            "toEnd \"circle\"",
            "fromSide \"middle\"",
        ] {
            assert!(
                problems.contains(expected),
                "missing {expected:?} in\n{problems}"
            );
        }
    }
}