Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: WASM

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions-rust-lang/setup-rust-toolchain@v1.17.0
with:
target: wasm32-unknown-unknown
- uses: jetli/wasm-pack-action@v0.4.0
- run: cargo test
working-directory: bindings/wasm
- run: wasm-pack build bindings/wasm --target nodejs
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ __pycache__/
dist/
*.egg-info/

# WASM bindings
pkg/


# Added by cargo
#
Expand Down
220 changes: 220 additions & 0 deletions bindings/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions bindings/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "txm-wasm"
version = "0.1.4"
edition = "2024"
description = "WebAssembly bindings for TXM, a terminal math rendering engine."
license = "MIT OR Apache-2.0"
repository = "https://github.com/thatmagicalcat/txm"

[lib]
crate-type = ["cdylib", "rlib"]

[workspace]

[dependencies]
wasm-bindgen = "0.2"
libtxm = { package = "txm", path = "../..", default-features = false }
24 changes: 24 additions & 0 deletions bindings/wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div align="center">
<h1>TXM.wasm</h1>
<p>WebAssembly / JavaScript bindings for TXM.</p>
</div>

### Build

```
rustup target add wasm32-unknown-unknown
cargo install wasm-pack
cd bindings/wasm
wasm-pack build --target nodejs
```

### Example (Node.js)

```js
const { render } = require('./pkg');
console.log(render('E = mc^2'));
console.log(render('\\int_0^\\infty e^{-x^2}\\,dx = \\frac{\\sqrt{\\pi}}{2}'));
```

## License
- Apache License, Version 2.0 / MIT license
39 changes: 39 additions & 0 deletions bindings/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use libtxm;
use wasm_bindgen::prelude::*;

/// Render a LaTeX math expression to Unicode art (one newline-separated row per line).
#[wasm_bindgen]
pub fn render(input: &str) -> Result<String, JsError> {
libtxm::render(input).map_err(|e| JsError::new(&e.0))
}

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

#[test]
fn render_basic() {
let out = libtxm::render("E = mc^2").unwrap();
assert!(out.contains('m'));
assert!(out.contains('c'));
}

#[test]
fn render_integral() {
let out = libtxm::render(r"\int_0^\infty e^{-x^2}\,dx").unwrap();
assert!(out.contains('∞'));
assert!(out.contains('x'));
}

#[test]
fn render_fraction() {
let out = libtxm::render(r"\frac{1}{2}").unwrap();
assert!(out.contains('1'));
assert!(out.contains('2'));
}

#[test]
fn render_error_on_unclosed_brace() {
assert!(libtxm::render(r"\frac{1").is_err());
}
}
Loading