tauri-plugin-compmd 0.12.0

The Tauri adapter of CompMD's hosting: embeds the bundle @compmd/vite writes, serves it on a scheme of the app's own, and gives the page its origin. In development.
Documentation
# tauri-plugin-compmd

Shows [CompMD](https://compmd.dev/) documents in a [Tauri](https://tauri.app) app. It is the Tauri adapter of CompMD's hosting: what every host does (the configuration, the bundle, the pack store, the serving contract) is shared, in `@compmd/vite` and `compmd-host`, and the plugin does what only Tauri can. It embeds the bundle in the app, serves it on a scheme of its own, gives the page the origin, and keeps the packs the app fetches in its data folder. The design, and why each rule is there, is [docs/tauri-plugin.md](https://github.com/comp-markdown/compmd/blob/main/docs/tauri-plugin.md), on [docs/hosting.md](https://github.com/comp-markdown/compmd/blob/main/docs/hosting.md).

It shares its version with the `@compmd` packages: use `@compmd/vite` and `@compmd/sandbox` of the same version.

**Versions:**
- **0.6.0:** the first version: `init()`, which served frames the crate embedded on `compmd://` and gave the page their base; `Builder` for another scheme or an app's own build of the frames; the `base` command. (`0.0.0` is a placeholder with no code.)
- **0.7.0:** the hosting adapter. The app embeds the bundle its own page's build writes (`build::bundle`, `include_bundle!`, `init(bundle)` in place of `init()` and `Builder`); the scheme is the one the bundle was built for; the page gets the origin, `__COMPMD__ = { origin }`; the scheme serves `compmd-host`'s contract, frames under their hash and packs at `packs/<place>`. The crate's own frames, `frames_dir`, the `base` command and the permissions are gone.
- **0.7.1:** the same as 0.7.0; its homepage and links name the website's new address, [compmd.dev](https://compmd.dev/).
- **0.8.0:** the same adapter as 0.7.1, serving the sandbox's 0.8.0 frames, whose policies let them compile WebAssembly; an app with a policy of its own adds `'wasm-unsafe-eval'` to its `script-src`, as `cspFor` gives it, for code highlighted with Oniguruma on macOS and Linux.
- **0.9.0:** the same adapter as 0.8.0, serving the sandbox's 0.9.0 frames, which ask the app's page to highlight their code and no longer compile WebAssembly; an app's own policy keeps `'wasm-unsafe-eval'` in its `script-src`, as `cspFor` gives it, since its page now highlights every frame's code.
- **0.10.0:** the same adapter as 0.9.0, serving the sandbox's 0.10.0 frames, which speak its protocol's version 3 and take a document's own fonts as `blob:`.
- **0.11.0:** serving the sandbox's 0.11.0 frames, which speak its protocol's version 4 and start only in a sandboxed frame. A Rust app processes documents with the bundle's policy and ambient packs (`packs.policy()`, `packs.ambient()`, with `resolve_with`), so its `deny`, `imports: "listed"` and `refused` hold. Request paths are percent-decoded, and the 404 when there is no bundle carries the contract's refusal headers. Breaking: an embedded bundle that can't be served panics when the app starts, naming why, instead of serving nothing; an app's page and the frames are upgraded together.
- **0.12.0:** serving the sandbox's 0.12.0 frames. The scheme serves `registry/`, the proxy of the registries the bundle's configuration fetches through the host (`fetch: "host"`), as `@compmd/vite`'s dev server does, answered on a thread of its own: a page that processes documents gets registry packs as on its site.

## Use

The page's build writes the bundle into the Rust project, with the scheme it is served on:

```js
// vite.config.js
import { compmd } from "@compmd/vite";
export default { plugins: [compmd({ out: "src-tauri/compmd", source: "compmd://localhost/" })] };
```

The app's `build.rs` embeds it, and its `main` gives it to the plugin:

```toml
# src-tauri/Cargo.toml
[dependencies]
tauri-plugin-compmd = "0.7.0"

[build-dependencies]
tauri-plugin-compmd = { version = "0.7.0", features = ["build"] }
```

```rust
// src-tauri/build.rs
fn main() {
    tauri_plugin_compmd::build::bundle("compmd");
    tauri_build::build();
}

// src-tauri/src/main.rs
tauri::Builder::default()
    .plugin(tauri_plugin_compmd::init(tauri_plugin_compmd::include_bundle!()))
```

The page takes everything from its own bundle, and the origin from the plugin:

```js
import { createSandbox } from "@compmd/sandbox";
import { sandbox } from "virtual:compmd";

const view = createSandbox({ ...sandbox, page: "host" }).open(container, { components: processed.hostComponents });
view.render(processed.expanded);
```

`src-tauri/compmd/` is build output: list it in the app's `.gitignore`. A build that finds no bundle there warns, and the app serves none, which its page reports. Run the page's build before the app's: `vite build`, or the dev server, which writes the bundle when it starts.

**An app that processes documents in Rust** turns on the `processor` feature, and gives the processor the same packs the frames load, with the policy and the ambient packs the bundle's configuration made, so a document is held to what the page's path holds it to (`deny`, `imports: "listed"`, `refused`):

```rust
use tauri_plugin_compmd::CompmdExt;

let packs = app.compmd().packs().expect("a bundle"); // the bundle's packs, then the store's
let (policy, ambient) = (packs.policy()?, packs.ambient());
let processor = Processor::new(path, text).parse(&ParseOptions::default()).resolve_with((), packs, &ambient, &policy);
```

**If the app sets a content security policy** (`app.security.csp` in `tauri.conf.json`; Tauri's template sets none), it adds what `cspFor` from `@compmd/vite` gives, for both forms of the origin:

```js
import { cspFor } from "@compmd/vite/config";
cspFor("compmd://localhost"); cspFor("http://compmd.localhost"); // Windows and Android
```

**Keeping frames from the app's commands:** a frame's code can't call a command, since Tauri gives its invoke key to the app's own page only. A capability doesn't do it: Tauri counts a page on a scheme the app registers as one of its own. Declare the app's commands (`tauri_build::AppManifest`) and grant them in the capability, as the example app does. On WebKit (macOS, Linux) a frame has Tauri's `ipc` message handler, which WebKit gives every frame and no content security policy governs: Tauri drops a message without the key, which the example's check shows by posting one ([the research](https://github.com/comp-markdown/compmd/blob/main/docs/tauri-plugin.md#appendix-webkits-ipc-handler-in-a-frame)). The plugin needs Tauri 2.11.1 or later, which has the fix for [GHSA-7gmj-67g7-phm9](https://github.com/tauri-apps/tauri/security/advisories/GHSA-7gmj-67g7-phm9).

## Building it from this repository

`cargo make tauri-plugin` lints and tests the crate, with its features and without, and measures its coverage. Its tests make their bundles with `compmd-host`'s `Bundle::from_files`, so they need no JavaScript build.

## Status

In development, released with CompMD's packages at the same version. Tested on Linux (WebKitGTK) in CI by the example app, `crates/tauri/examples/tauri-app/`, and by hand on macOS; iOS and Android aren't tested yet.