Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion crates/mdbook-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>,
}

impl Default for Code {
fn default() -> Code {
Code {
default_hidelines: true,
hidelines: HashMap::<String, String>::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)]
Expand Down
34 changes: 33 additions & 1 deletion crates/mdbook-html/src/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
"<code class=\"{}\">{}</code>",
classes,
Expand Down Expand Up @@ -1311,6 +1311,38 @@ mod tests {
}
}

#[test]
fn no_default_hide_lines() {
let inputs = [
(
"<pre class=\"playground\"><code class=\"language-rust\">\n# #![allow(unused)]\n#fn main() {\nx()\n#}</code></pre>",
"<pre class=\"playground\"><code class=\"language-rust\">\n# #![allow(unused)]\n#fn main() {\nx()\n#}</code></pre>",
),
(
"<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n # bar\n\";</code></pre>",
"<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n # bar\n\";</code></pre>",
),
(
"<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n ## bar\n\";</code></pre>",
"<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n ## bar\n\";</code></pre>",
),
(
"<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n # bar\n#\n\";</code></pre>",
"<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n # bar\n#\n\";</code></pre>",
),
(
"<code class=\"language-rust ignore\">let s = \"foo\n # bar\n\";</code>",
"<code class=\"language-rust ignore\">let s = \"foo\n # bar\n\";</code>",
),
];
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"));
Expand Down
Loading