use super::emitter::FADE;
use super::inline::{sgr_fg, RESET};
#[cfg(not(feature = "markdown-syntect"))]
pub(super) fn highlight(_lang: &str, body: &str) -> Vec<String> {
body.split('\n')
.map(|l| format!("{}{l}{RESET}", sgr_fg(FADE)))
.collect()
}
#[cfg(feature = "markdown-syntect")]
pub(super) fn highlight(lang: &str, body: &str) -> Vec<String> {
use std::sync::OnceLock;
use syntect::easy::HighlightLines;
use syntect::highlighting::ThemeSet;
use syntect::parsing::SyntaxSet;
use syntect::util::as_24_bit_terminal_escaped;
static ASSETS: OnceLock<(SyntaxSet, ThemeSet)> = OnceLock::new();
let (ss, ts) = ASSETS.get_or_init(|| {
(
SyntaxSet::load_defaults_newlines(),
ThemeSet::load_defaults(),
)
});
let syntax = ss
.find_syntax_by_token(lang)
.unwrap_or_else(|| ss.find_syntax_plain_text());
let theme = &ts.themes["base16-ocean.dark"];
let mut h = HighlightLines::new(syntax, theme);
body.split('\n')
.map(|line| {
let with_nl = format!("{line}\n");
match h.highlight_line(&with_nl, ss) {
Ok(ranges) => {
let escaped = as_24_bit_terminal_escaped(&ranges, false);
format!("{}{RESET}", escaped.trim_end_matches('\n'))
}
Err(_) => format!("{}{line}{RESET}", sgr_fg(FADE)),
}
})
.collect()
}