use quick_js::Context;
const PRISM_JS: &str = include_str!("../prism/prism.js");
pub const PRISM_CSS: &str = include_str!("../prism/prism.css");
pub type PrismContext = Context;
pub fn init() -> PrismContext {
let context = Context::new().unwrap();
context.eval(PRISM_JS).unwrap();
context
}
pub fn highlight_internal(
context: &mut PrismContext,
text: &str,
grammar: &str,
language: &str,
) -> Option<String> {
context.set_global("text", text).ok()?;
context.set_global("language", language).ok()?;
context
.eval(&format!("Prism.highlight(text, {}, language)", grammar))
.ok()
.and_then(|v| v.as_str().map(|s| s.to_string()))
}
pub fn highlight(context: &mut PrismContext, text: &str, language: &str) -> Option<String> {
highlight_internal(context, text, format!("Prism.languages.{}", language).as_str(), language)
}