From abe4816e29c51fe3acace27ab113d0b3cb4d1d25 Mon Sep 17 00:00:00 2001 From: Menci Date: Sun, 18 Feb 2024 02:04:35 +0800 Subject: [PATCH] Upgrade syntect to 5.2.0 --- Cargo.toml | 2 +- README.md | 2 +- src/lib.rs | 29 ++++++++++++++++++----------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f26d4dc..6865839 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ crate-type = ["cdylib"] napi = { version = "2.0.2", optional = true, default-features = false, features = ["napi4"] } napi-derive = { version = "2.0.6", optional = true } wasm-bindgen = { version = "0.2.78", optional = true } -syntect = { version = "4.6", default-features = false } +syntect = { version = "5.2", default-features = false } [build-dependencies] napi-build = { version = "1.2.1", optional = true } diff --git a/README.md b/README.md index b542418..118f3c0 100644 --- a/README.md +++ b/README.md @@ -70,4 +70,4 @@ To use the WASM version with Node.js, please add `--experimental-wasm-modules` t To use the WASM version in browser, you may need to configure your bundler: * *Webpack 5*: enable `experiments.asyncWebAssembly`. -* *Vite*: not currently supported but related works are in progress. +* *Vite*: use [vite-plugin-wasm](https://github.com/Menci/vite-plugin-wasm). diff --git a/src/lib.rs b/src/lib.rs index a20f700..64a24ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,18 +24,25 @@ pub struct GetCssResult { pub fn get_css(theme_data: String, class_name_prefix: String) -> GetCssResult { match ThemeSet::load_from_reader(&mut std::io::Cursor::new(theme_data)) { Ok(theme) => { - let result: String; unsafe { let prefix = Box::into_raw(class_name_prefix.into_boxed_str()); - result = - css_for_theme_with_class_style(&theme, ClassStyle::SpacedPrefixed { prefix: &*prefix }); - Box::from_raw(prefix); + match css_for_theme_with_class_style(&theme, ClassStyle::SpacedPrefixed { prefix: &*prefix }) { + Ok(css) => { + let _ = Box::from_raw(prefix); + return GetCssResult { + css: css, + error: "".to_string(), + }; + }, + Err(error) => { + let _ = Box::from_raw(prefix); + return GetCssResult { + css: "".to_string(), + error: error.to_string(), + }; + }, + } } - - return GetCssResult { - css: result, - error: "".to_string(), - }; } Err(error) => { return GetCssResult { @@ -91,10 +98,10 @@ pub fn highlight(code: String, language: String, prefix: String) -> HighlightRes }, ); for line in LinesWithEndings::from(&code) { - generator.parse_html_for_line_which_includes_newline(line); + let _ = generator.parse_html_for_line_which_includes_newline(line); } result = generator.finalize(); - Box::from_raw(prefix_str); + let _ = Box::from_raw(prefix_str); } return HighlightResult {