The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Add Syntax Highlighting To Strings Of Code In Rust

TODO: Pull subtitle into page object
Code
//! ```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
[_active_nvim_run:14] output = "<pre style=\"background-color:#2b303b;\">\n<span style=\"color:#c0c5ce;\">console.</span><span style=\"color:#96b5b4;\">log</span><span style=\"color:#c0c5ce;\">(`here`)</span></pre>\n"

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Add Syntax Highlighting To Strings Of Code In Rust

-- code
-- 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/

[_active_nvim_run:14] output = "<pre style=\"background-color:#2b303b;\">\n<span style=\"color:#c0c5ce;\">console.</span><span style=\"color:#96b5b4;\">log</span><span style=\"color:#c0c5ce;\">(`here`)</span></pre>\n"

-- /results


-- categories
-- Rust 

-- metadata
-- date: 2023-08-30 21:18:53
-- id: 2ujekgik
-- site: aws
-- type: rust 
-- status: scratch