Skip to main content

wasm/
mdx.rs

1use js_sys::JsString;
2use wasm_bindgen::prelude::*;
3use wasm_bindgen_futures::future_to_promise;
4
5#[wasm_bindgen(js_name = "mdxCompileSync")]
6pub fn mdx_compile_sync(value: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
7    let value: String = value.into();
8    let option: mdxjs::Options = serde_wasm_bindgen::from_value(opts)?;
9
10    mdxjs::compile(value.as_str(), &option)
11        .map(|v| serde_wasm_bindgen::to_value(&v).expect("Should able to convert to JsValue"))
12        .map_err(|v| {
13            serde_wasm_bindgen::to_value(&v.to_string()).expect("Should able to convert to JsValue")
14        })
15}
16
17#[wasm_bindgen(js_name = "mdxCompile")]
18pub fn mdx_compile(value: JsString, opts: JsValue) -> js_sys::Promise {
19    // TODO: This'll be properly scheduled once wasm have standard backed thread
20    // support.
21    future_to_promise(async { mdx_compile_sync(value, opts) })
22}