Skip to content

Commit bc1e3cc

Browse files
committed
Add configuration option to disable the default hidelines feature for rust
1 parent 0722d81 commit bc1e3cc

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

crates/mdbook-core/src/config.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,13 +561,23 @@ impl Default for Playground {
561561

562562
/// Configuration for tweaking how the HTML renderer handles code blocks.
563563
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
564-
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
565-
#[non_exhaustive]
564+
#[serde(default, rename_all = "kebab-case")]
566565
pub struct Code {
566+
/// Enable or disable the default line hiding with '#' for rust. Default: `true`.
567+
pub default_hidelines: bool,
567568
/// A prefix string to hide lines per language (one or more chars).
568569
pub hidelines: HashMap<String, String>,
569570
}
570571

572+
impl Default for Code {
573+
fn default() -> Code {
574+
Code {
575+
default_hidelines: true,
576+
hidelines: HashMap::<String, String>::default(),
577+
}
578+
}
579+
}
580+
571581
/// Configuration of the search functionality of the HTML renderer.
572582
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
573583
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]

crates/mdbook-html/src/html_handlebars/hbs_renderer.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ fn hide_lines(html: &str, code_config: &Code) -> String {
901901
let classes = &caps[2];
902902
let code = &caps[3];
903903

904-
if classes.contains("language-rust") {
904+
if classes.contains("language-rust") && code_config.default_hidelines {
905905
format!(
906906
"<code class=\"{}\">{}</code>",
907907
classes,
@@ -1311,6 +1311,35 @@ mod tests {
13111311
}
13121312
}
13131313

1314+
#[test]
1315+
fn no_default_hide_lines() {
1316+
let inputs = [
1317+
(
1318+
"<pre class=\"playground\"><code class=\"language-rust\">\n# #![allow(unused)]\n#fn main() {\nx()\n#}</code></pre>",
1319+
"<pre class=\"playground\"><code class=\"language-rust\">\n# #![allow(unused)]\n#fn main() {\nx()\n#}</code></pre>",),
1320+
(
1321+
"<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n # bar\n\";</code></pre>",
1322+
"<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n # bar\n\";</code></pre>",),
1323+
(
1324+
"<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n ## bar\n\";</code></pre>",
1325+
"<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n ## bar\n\";</code></pre>",),
1326+
(
1327+
"<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n # bar\n#\n\";</code></pre>",
1328+
"<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n # bar\n#\n\";</code></pre>",),
1329+
(
1330+
"<code class=\"language-rust ignore\">let s = \"foo\n # bar\n\";</code>",
1331+
"<code class=\"language-rust ignore\">let s = \"foo\n # bar\n\";</code>",),
1332+
];
1333+
let code = Code {
1334+
hidelines: HashMap::<String, String>::default(),
1335+
default_hidelines: false,
1336+
};
1337+
for (src, should_be) in &inputs {
1338+
let got = hide_lines(src, &code);
1339+
assert_eq!(&*got, *should_be);
1340+
}
1341+
}
1342+
13141343
#[test]
13151344
fn test_json_direction() {
13161345
assert_eq!(json!(TextDirection::RightToLeft), json!("rtl"));

0 commit comments

Comments
 (0)