39 lines
1.3 KiB
Rust
39 lines
1.3 KiB
Rust
use giallo::{HighlightOptions, HtmlRenderer, Registry, RenderOptions, ThemeVariant};
|
|
|
|
mod bindings {
|
|
//! Generated code derived from the `giallo-js.wit` definition.
|
|
|
|
wit_bindgen::generate!({ path: "wit/giallo-js.wit" });
|
|
|
|
use super::GialloJsComponent;
|
|
export!(GialloJsComponent);
|
|
}
|
|
|
|
struct GialloJsComponent;
|
|
|
|
impl bindings::exports::brentsch::giallo_js::syntax_highlighter::Guest for GialloJsComponent {
|
|
/// Rudimentary Wasm adapter around the basic
|
|
/// [Giallo usage example](https://github.com/getzola/giallo/blob/a86755836dc4387056432f63f0b37b2c04b52711/examples/basic.rs).
|
|
fn render_highlighted(
|
|
code: String,
|
|
language: String,
|
|
theme_light: String,
|
|
theme_dark: String,
|
|
) -> Result<String, String> {
|
|
let mut registry = Registry::builtin().map_err(|err| err.to_string())?;
|
|
registry.link_grammars();
|
|
|
|
let options = HighlightOptions::new(
|
|
language,
|
|
ThemeVariant::Dual {
|
|
light: &theme_light,
|
|
dark: &theme_dark,
|
|
},
|
|
);
|
|
let highlighted = registry
|
|
.highlight(&code, &options)
|
|
.map_err(|err| err.to_string())?;
|
|
|
|
Ok(HtmlRenderer::default().render(&highlighted, &RenderOptions::default()))
|
|
}
|
|
}
|