Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Highlight Code In Rust With syntect

This is how I'm highlighting HTML strings in Rust which I use to build my site.

  • look at this and combine it: id: 2ujekgik

Code

use minijinja::Error;
use syntect::highlighting::ThemeSet;
use syntect::html::highlighted_html_for_string;
use syntect::parsing::SyntaxSet;

pub fn highlight_html(name: String) -> Result<String, Error> {
    let ss = SyntaxSet::load_defaults_newlines();
    let ts = ThemeSet::load_defaults();
    let theme = &ts.themes["base16-ocean.dark"];
    let html = highlighted_html_for_string(name.as_str(), &ss, &ss.syntaxes()[1], theme).unwrap();
    Ok(html)
}

References