adoctra-wasm exposes the parser — not the converters — to JavaScript. It is built for editors: parsing is error-tolerant, every block carries byte spans, and a token stream drives syntax highlighting.
Build
rustup target add wasm32-unknown-unknown
wasm-pack build crates/adoctra-wasm --target web # or --target bundler
API
parse_json(text)-
{ document, diagnostics, tokens }; diagnostics and tokens carry UTF-8 byte spans ({ source_id, start, end }). tokens_json(text)-
highlighting tokens only.
ResourceWorkspace-
a virtual file system for includes and images:
set(name, text),set_binary(name, bytes),remove(name),load(target, relative_to),parse_json(source_id),tokens_json(source_id).
import init, { parse_json, ResourceWorkspace } from "./pkg/adoctra_wasm.js";
await init();
const { document, diagnostics, tokens } = JSON.parse(parse_json("= Title\n\nText"));
const ws = new ResourceWorkspace();
ws.set("/main.adoc", "= Book\n\ninclude::chapter.adoc[]");
ws.set("/chapter.adoc", "== Chapter");
const main = ws.load("/main.adoc");
const book = JSON.parse(ws.parse_json(main));
Malformed input never throws: the result is a partial document plus diagnostics, so the editor can keep showing an outline and highlighting while the user types.
Diagrams
The browser cannot run diagram tools, so diagrams are rendered through Kroki or a PlantUML server. Parsing is synchronous, network requests are not: a parse lists the requests it still needs, the host performs them and parses again.
const ws = new DiagramWorkspace("https://kroki.io/", "kroki_io");
let result = JSON.parse(ws.parse_json(text, 0));
for (const r of result.diagrams.requests) {
const response = await fetch(r.url, r.body === undefined ? {} :
{ method: "POST", body: r.body, headers: { "Content-Type": r.content_type } });
if (response.ok) ws.set_response(r.key, new Uint8Array(await response.arrayBuffer()));
else ws.set_error(r.key, `${response.status} "${response.statusText}"`);
}
result = JSON.parse(ws.parse_json(text, 0)); // images in result.diagrams.images
To read diagram files from a ResourceWorkspace, call ws.enable_diagrams(url, type) and pass responses with set_diagram_response/set_diagram_error. PlantUML diagrams in the browser need preprocess=false.