diff --git a/crates/mdbook-core/src/config.rs b/crates/mdbook-core/src/config.rs index 771204822b..988b34fd5b 100644 --- a/crates/mdbook-core/src/config.rs +++ b/crates/mdbook-core/src/config.rs @@ -560,14 +560,25 @@ impl Default for Playground { } /// Configuration for tweaking how the HTML renderer handles code blocks. -#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "kebab-case", deny_unknown_fields)] #[non_exhaustive] pub struct Code { + /// Enable or disable the default line hiding with '#' for rust. Default: `true`. + pub default_hidelines: bool, /// A prefix string to hide lines per language (one or more chars). pub hidelines: HashMap, } +impl Default for Code { + fn default() -> Code { + Code { + default_hidelines: true, + hidelines: HashMap::::default(), + } + } +} + /// Configuration of the search functionality of the HTML renderer. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "kebab-case", deny_unknown_fields)] diff --git a/crates/mdbook-html/src/html_handlebars/hbs_renderer.rs b/crates/mdbook-html/src/html_handlebars/hbs_renderer.rs index 73b2a141de..87db5da3a2 100644 --- a/crates/mdbook-html/src/html_handlebars/hbs_renderer.rs +++ b/crates/mdbook-html/src/html_handlebars/hbs_renderer.rs @@ -901,7 +901,7 @@ fn hide_lines(html: &str, code_config: &Code) -> String { let classes = &caps[2]; let code = &caps[3]; - if classes.contains("language-rust") { + if classes.contains("language-rust") && code_config.default_hidelines { format!( "{}", classes, @@ -1311,6 +1311,38 @@ mod tests { } } + #[test] + fn no_default_hide_lines() { + let inputs = [ + ( + "
\n# #![allow(unused)]\n#fn main() {\nx()\n#}
", + "
\n# #![allow(unused)]\n#fn main() {\nx()\n#}
", + ), + ( + "
let s = \"foo\n # bar\n\";
", + "
let s = \"foo\n # bar\n\";
", + ), + ( + "
let s = \"foo\n ## bar\n\";
", + "
let s = \"foo\n ## bar\n\";
", + ), + ( + "
let s = \"foo\n # bar\n#\n\";
", + "
let s = \"foo\n # bar\n#\n\";
", + ), + ( + "let s = \"foo\n # bar\n\";", + "let s = \"foo\n # bar\n\";", + ), + ]; + let mut code = Code::default(); + code.default_hidelines = false; + for (src, should_be) in &inputs { + let got = hide_lines(src, &code); + assert_eq!(&*got, *should_be); + } + } + #[test] fn test_json_direction() { assert_eq!(json!(TextDirection::RightToLeft), json!("rtl"));