hljs: binding: catch hljs errors when specifying language
the language may not exist. do not crash the entire program due to this
This commit is contained in:
parent
f519152465
commit
311b9a0d26
|
@ -120,7 +120,7 @@ impl Post {
|
||||||
// library updates. Updaing this value invalidates all
|
// library updates. Updaing this value invalidates all
|
||||||
// existing cache and they will be recompiled when someone
|
// existing cache and they will be recompiled when someone
|
||||||
// visits.
|
// visits.
|
||||||
const CACHE_VERSION: &'static str = "0015";
|
const CACHE_VERSION: &'static str = "0017";
|
||||||
|
|
||||||
// The prefix path used for caching remote images
|
// The prefix path used for caching remote images
|
||||||
pub const IMG_CACHE_PREFIX: &'static str = "/imgcache/";
|
pub const IMG_CACHE_PREFIX: &'static str = "/imgcache/";
|
||||||
|
|
|
@ -12,6 +12,11 @@ pub fn highlight_auto(code: &str) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn highlight(lang: &str, code: &str) -> String {
|
pub fn highlight(lang: &str, code: &str) -> String {
|
||||||
Reflect::get(&hljs_highlight(lang, code), &"value".into())
|
match hljs_highlight(lang, code) {
|
||||||
.unwrap().as_string().unwrap()
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -2,6 +2,6 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#[wasm_bindgen(js_name = "highlightAuto")]
|
#[wasm_bindgen(js_name = "highlightAuto")]
|
||||||
fn hljs_highlight_auto(code: &str) -> JsValue;
|
fn hljs_highlight_auto(code: &str) -> JsValue;
|
||||||
#[wasm_bindgen(js_name = "highlight")]
|
#[wasm_bindgen(catch, js_name = "highlight")]
|
||||||
fn hljs_highlight(lang: &str, code: &str) -> JsValue;
|
fn hljs_highlight(lang: &str, code: &str) -> Result<JsValue, JsValue>;
|
||||||
}
|
}
|
Loading…
Reference in a new issue