Repository navigation
feat(rune-kit): sandboxed display math for latex and math fences - #1294
Merged
Merged
Conversation
…ences Add an optional `math` feature to `rune-kit` that renders closed ```` ```latex ```` and ```` ```math ```` Markdown code fences as KaTeX-compatible display math (`anomalyco/opencode#64380`) in pure Rust with zero JS or WASM runtime: - Uses `ratex-parser` + `ratex-layout` + `ratex-types` + `ratex-font` + `ratex-katex-fonts` (`0.1.16`, `default-features = false`, MIT + SIL OFL 1.1) and rasterises `DisplayList` directly via the workspace's existing `skrifa 0.44` + `tiny-skia 0.11` into `rune::Pixels` (no duplicate `skrifa`/`tiny-skia`, no `ratex-svg`/`fontdb`/`ttf-parser`). - Bounded per-thread LRU pixel cache (128 entries / 8 MiB cap, caching both valid equations and parse/cap failures so streaming tokens below a settled fence pay zero reparses). - Hard defense-in-depth caps: 16 KiB raw source, pre-parse nesting depth 32, RaTeX parser depth/expansion/layout validation limits, 4,096 display items, 4096x1024 pt logical bounds, and 4M pixel area cap. - Open streaming fences, empty fences, parse/layout errors, and inputs exceeding caps fall back seamlessly to highlighted code blocks. - Adds `MarkdownStyle::math_scale(f32)` (default 2.0) for crisp 1:1 physical-pixel rendering at any display scale factor.
|
would be nice to have it inline as well e.g. calleum@ea1ba64#diff-455c1483da6eec83a5f7a9e50612e5991db96bbc593462da95a558aa35b4dca8 |
…I thread ratex-math.wasm (built reproducibly from tools/ratex-math-wasm by scripts/build-math-wasm.sh, checked by crates/rune-kit/build.rs) runs in wasmi on a lazy background worker (native) or browser WebAssembly in a terminable Web Worker (wasm32), with host-enforced fuel, 16 MiB memory, 250 ms deadline, output and canvas caps, and a negative cache.
# Conflicts: # crates/rune-kit/src/markdown/render/blocks.rs
Member
Author
|
Farm-built
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Display math for closed
```latexand```mathfences inMarkdown(anomalyco/opencode#64380), behind the opt-inrune-kitfeaturemath. RaTeX runs in a sandboxed WebAssembly module, never in the app's own address space.How it works
ratex-math.wasmis built fromtools/ratex-math-wasm/(guest source, pinned toolchain and lock file) byscripts/build-math-wasm.sh, which writescrates/rune-kit/assets/math/ratex-math.wasm.deflate, the KaTeX font pack andsrc/math/wasm_hash.rs.scripts/build-math-wasm.sh --checkrebuilds from source and compares byte for byte (passes at this head).crates/rune-kit/build.rschecks, whenmathis on, that the committed module, font pack and guest sources match the SHA-256 digests inwasm_hash.rs. A guest edit without a rebuild fails the build.wasmi 1.1on one lazy background thread (rune-math). Web (wasm32): the browser's WebAssembly in a terminable Web Worker; nowasmiin the web bundle.RM01display list (every byte treated as hostile) and draws it withskrifa+tiny-skia.Host-enforced limits
StoreLimitson native,--max-memoryin the module for the browser)worker.terminate()on web)Any trap, fuel or memory exhaustion, deadline or wire error drops the guest instance and shows the fence as highlighted code.
Cost
latex/mathfence keeps its whole-document memo (may_have_math_fence).wasmihost +960,000 B withopt-level = "z"on thewasmi*/wasmparserpackages (saves ~404 KiB againstopt-level = 3); module 809,586 B raw / 298,353 B deflated; KaTeX fonts 513,664 B raw / 267,712 B deflated.Markdown::find; a fence that fell back to code stays searchable.Notices
THIRD_PARTY_NOTICES.md(new) pluscrates/rune-kit/assets/math/{OFL.txt,FONT_NOTICE.txt}. Forrune-desktop/THIRD_PARTY_NOTICES.md: KaTeX fonts (SIL OFL 1.1), RaTeX 0.1.16 (MIT), unicode-normalization 0.1.26 (MIT OR Apache-2.0, Unicode-3.0), wasmi 1.1.0 (MIT OR Apache-2.0).Proof
math,wasm32withmath: clean, norune-kitwarnings.RM01payloads, 4,000 mutated TeX inputs, 50-formula async Markdown + Find): PROOF-FARM run requested atws3/proof-1294-1139; result goes here.