paprika/src/hljs.rs
Peter Cai 311b9a0d26
hljs: binding: catch hljs errors when specifying language
the language may not exist. do not crash the entire program due to this
2020-04-12 17:01:45 +08:00

22 lines
No EOL
753 B
Rust

// Simple bindings for Highlight.js
// We don't have something equivalent in Rust
// and I don't really want to run these on client
use js_sys::Reflect;
use wasm_bindgen::prelude::*;
include!(concat!(env!("OUT_DIR"), "/load_hljs.rs"));
pub fn highlight_auto(code: &str) -> String {
Reflect::get(&hljs_highlight_auto(code), &"value".into())
.unwrap().as_string().unwrap()
}
pub fn highlight(lang: &str, code: &str) -> String {
match hljs_highlight(lang, code) {
Ok(res) => Reflect::get(&res, &"value".into())
.unwrap().as_string().unwrap(),
// This can throw error if `lang` is not supported
// or not imported by build.rs (and thus config.json)
Err(_) => code.to_owned()
}
}