Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Add Syntax Highlighting To Strings Of Code In Rust

rust
//! ```cargo
//! [dependencies]
//! syntect = "5.1.0"
//! ```

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

fn main() {
    let input = "console.log(`here`)"; 
    let extension = "rust";
    let output = highlight_code(input, extension);
    dbg!(output);
}

fn highlight_code(input: &str, code_type: &str) -> String {
    let ss = SyntaxSet::load_defaults_newlines();
    let syntax = ss.find_syntax_by_token(code_type).unwrap();
    let ts = ThemeSet::load_defaults();
    let theme = &ts.themes["base16-ocean.dark"];
    let html = highlighted_html_for_string(input, &ss, syntax, theme).unwrap();
    html
}
results start