Replies: 1 comment
-
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_name = "Object")]
type CodeToHTMLParam;
#[wasm_bindgen(constructor, js_class = "Object")]
fn new() -> CodeToHTMLParam;
#[wasm_bindgen(method, setter, js_name = "lang")]
fn set_lang(this: &CodeToHTMLParam, lang: &str);
#[wasm_bindgen(method, setter, js_name = "theme")]
fn set_theme(this: &CodeToHTMLParam, theme: &str);
// ...
}
#[wasm_bindgen(module = "https://esm.sh/shikiji@latest")]
extern "C" {
#[wasm_bindgen(js_name = "codeToHtml")]
async fn code_to_html(javascript_code: &str, param: CodeToHTMLParam) -> JsValue;
}
#[wasm_bindgen]
pub async fn run_test() -> JsValue {
let param = CodeToHTMLParam::new();
param.set_lang("javascript");
param.set_theme("vitesse-dark");
code_to_html("const a = 1", param).await
} Then from JS import init, { run_test } from './pkg/bindings.js';
console.log(await run_test()); Output: <pre class="shiki vitesse-dark" style="background-color:#121212;color:#dbd7caee" tabindex="0"><code><span class="line"><span style="color:#CB7676">const</span><span style="color:#BD976A"> a</span><span style="color:#666666"> =</span><span style="color:#4C9A91"> 1</span></span></code></pre> |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
I am working on a project where I need to call a function from a JavaScript CDN library in Rust, specifically the
codeToHtml
function from the shikiji library. I am using the wasm-bindgen crate to create a bridge between Rust and JavaScript. However, I am having trouble importing the function and calling it from my Rust code.Is there a example of calling a function from a external library from rust ?
The closest thing to a example Vanilla trunk example.It shows how to call a function from a relative path and a inline js from not from a js module (cdn package).
Beta Was this translation helpful? Give feedback.
All reactions