Skip to content

Commit

Permalink
Add configuration option to disable the default hidelines feature for…
Browse files Browse the repository at this point in the history
… rust
  • Loading branch information
zgtm committed Jan 3, 2025
1 parent 0bf6751 commit 6d7839b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,13 +697,24 @@ 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")]
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")]
Expand Down
34 changes: 33 additions & 1 deletion src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,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 @@ -1283,6 +1283,38 @@ mod tests {
map.insert("python".to_string(), "~".to_string());
map
},
default_hidelines: true,
},
);
assert_eq!(&*got, *should_be);
}
}

#[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>",),
];
for (src, should_be) in &inputs {
let got = hide_lines(
src,
&Code {
hidelines: HashMap::<String, String>::default(),
default_hidelines: false,
},
);
assert_eq!(&*got, *should_be);
Expand Down

0 comments on commit 6d7839b

Please sign in to comment.