11ty-plugin-giallo/giallo-js/src/lib.rs

40 lines
1.3 KiB
Rust
Raw Normal View History

2026-01-23 19:30:29 +00:00
use giallo::{HighlightOptions, HtmlRenderer, Registry, RenderOptions, ThemeVariant};
mod bindings {
2026-01-25 23:35:05 +00:00
//! Generated code derived from the `giallo-js.wit` definition.
2026-01-23 19:30:29 +00:00
2026-01-25 23:35:05 +00:00
wit_bindgen::generate!({ path: "wit/giallo-js.wit" });
2026-01-23 19:30:29 +00:00
use super::GialloJsComponent;
export!(GialloJsComponent);
}
struct GialloJsComponent;
2026-01-25 23:35:05 +00:00
impl bindings::exports::brentsch::giallo_js::syntax_highlighter::Guest for GialloJsComponent {
2026-01-23 19:30:29 +00:00
/// 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()))
}
}