feat: apply code block background and foreground color

This commit is contained in:
Jan-Bulthuis 2026-07-19 20:28:30 +02:00
parent 7798d556bc
commit 3711e99528

View File

@ -262,24 +262,36 @@ fn style_declarations(style: &Style) -> String {
}
fn generate_css(scopes: &[&str], theme: &Theme) -> String {
let mut rules = Vec::new();
// The theme's editor foreground/background colour the code block itself.
let mut block_style = Vec::new();
if let Some(Color(hex)) = &theme.foreground {
block_style.push(format!("color:{hex}"));
}
if let Some(Color(hex)) = &theme.background {
block_style.push(format!("background:{hex}"));
}
if !block_style.is_empty() {
rules.push(format!(".code-block{{{}}}", block_style.join(";")));
}
let mut groups: HashMap<&Style, Vec<&str>> = HashMap::new();
for &scope in scopes {
if let Some(style) = theme.styles.get(scope) {
groups.entry(style).or_default().push(scope);
}
}
groups
.iter()
.map(|(style, scopes)| {
rules.extend(groups.iter().map(|(style, scopes)| {
let selectors = scopes
.iter()
.map(|s| scope_to_selector(s))
.collect::<Vec<_>>()
.join(",");
format!("{}{{{}}}", selectors, style_declarations(style))
})
.collect::<Vec<_>>()
.join("\n")
}));
rules.join("\n")
}
#[func]