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
//! # `canvist_headless`
//!
//! The canvist document model and editing engine with no UI, for servers,
//! scripts, and tests. It runs anywhere Rust runs, including WebAssembly
//! without a DOM, and is what the TypeScript and Dart server packages wrap.
//!
//! - An [`Engine`] holds a set of extensions: their schema, their metadata,
//! and their typed handles. Build one per process; it is `Send + Sync`.
//! - A [`Doc`] is one document with an editor around it. Load it from JSON,
//! HTML, Markdown, or plain text ([`Format`]), set a selection, run
//! commands, and export it again. No layout is involved: selections are
//! explicit, addressed by model [`Point`](canvist_model::Point)s or by
//! language-neutral [`Position`]s. A `Doc` is `Send`.
//! - Typed handles make unregistered extensions a compile error: an engine
//! built without bold has no `bold` handle to call. Bindings use
//! [`Doc::run_named`] and [`Doc::query`], whose names come from
//! [`Engine::metadata`].
//! - [`Snapshot`]s capture content and selection as JSON.
//! - With the `collab` feature, a `Doc` is a Yjs peer: it joins from Yjs
//! state, merges updates, produces updates and y-sync messages, and checks
//! the merged state against its schema.
//! - With the `journal` feature, a `Doc` records every commit that changes
//! it (`Doc::record_commits`), for hosts that keep a layout of a document
//! other code changes too.
//! - With the `ssr` feature, a [`Renderer`] produces the HTML and SVG the
//! canvas editor would paint, using `canvist_layout` and `parley`. The
//! default build pulls in neither.
//! - With the `scene` feature, a [`SceneEngine`] loads canvas scenes of
//! shapes, frames, images, vector paths, and text frames into
//! [`SceneDoc`]s, which run canvas commands and export SVG and PNG with
//! the renderer the canvas draws with. With `collab` too, a scene is a Yjs
//! peer.
//!
//! Every binding runs the same language-neutral fixtures in `conformance/`
//! at the repository root, so a document transformed in Rust, TypeScript, or
//! Dart comes out the same.
//!
//! # Converting Markdown to JSON
//!
//! ```
//! use canvist_headless::Engine;
//! use canvist_headless::Format;
//!
//! let engine = Engine::starter_kit();
//! let doc = engine.parse(Format::Markdown, "# Plan\n\n- [x] Model\n- [ ] Server")?;
//! let json = doc.to_json();
//!
//! assert_eq!(json["content"][0]["type"], "heading");
//! assert_eq!(json["content"][1]["type"], "taskList");
//! assert_eq!(doc.export(Format::Text), "Plan\nModel\nServer");
//! # Ok::<(), canvist_headless::HeadlessError>(())
//! ```
//!
//! # A scripted transform
//!
//! Commands run through the typed handles the engine returns, against an
//! explicit selection. This one turns every paragraph that starts with
//! `TODO:` into an unchecked task:
//!
//! ```
//! use canvist_headless::Engine;
//! use canvist_headless::Format;
//! use canvist_headless::Position;
//!
//! let engine = Engine::starter_kit();
//! let kit = engine.handles();
//! let mut doc = engine.parse(Format::Markdown, "Intro\n\nTODO: write docs\n\nTODO: ship")?;
//!
//! let todos: Vec<u32> = doc
//! .document()
//! .children(None)
//! .iter()
//! .enumerate()
//! .filter(|&(_, &id)| doc.document().inline(id).is_some_and(|text| text.as_str().starts_with("TODO: ")))
//! .map(|(index, _)| index as u32)
//! .collect();
//!
//! // Later blocks keep their indexes because turning a paragraph into a task
//! // list keeps one top-level block per paragraph.
//! for index in todos {
//! let start = Position::new([index], 0);
//! doc.select_range(&start, &Position::new([index], 6))?;
//! doc.delete_selection()?;
//! doc.run(&kit.task_list.toggle())?;
//! }
//!
//! assert_eq!(doc.export(Format::Markdown), "Intro\n\n- [ ] write docs\n- [ ] ship");
//! # Ok::<(), canvist_headless::HeadlessError>(())
//! ```
//!
//! # Merging Yjs updates on a server
//!
//! See [`Doc::receive_sync`] (feature `collab`) for a y-websocket endpoint,
//! and `examples/` for runnable programs.
pub use canvist_collab;
pub use canvist_core;
pub use canvist_extensions;
pub use canvist_layout;
pub use canvist_model;
pub use canvist_scene;
pub use SyncReply;
pub use Validation;
pub use join_message;
pub use Doc;
pub use Engine;
pub use ErrorCode;
pub use HeadlessError;
pub use Format;
pub use Position;
pub use ExportOptions;
pub use SceneDoc;
pub use SceneEngine;
pub use Snapshot;
pub use Renderer;