Skip to content

Commit 0ee565a

Browse files
authored
Merge pull request #2530 from max-heller/rust-hidelines
fix: make line hiding in Rust code blocks consistent with `rustdoc`
2 parents 9e4854f + d325e82 commit 0ee565a

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

guide/src/format/mdbook.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
There is a feature in mdBook that lets you hide code lines by prepending them with a specific prefix.
66

7-
For the Rust language, you can use the `#` character as a prefix which will hide lines [like you would with Rustdoc][rustdoc-hide].
7+
For the Rust language, you can prefix lines with `# ` (`#` followed by a space) to hide them [like you would with Rustdoc][rustdoc-hide].
8+
This prefix can be escaped with `##` to prevent the hiding of a line that should begin with the literal string `# ` (see [Rustdoc's docs][rustdoc-hide] for more details)
89

910
[rustdoc-hide]: https://doc.rust-lang.org/stable/rustdoc/write-documentation/documentation-tests.html#hiding-portions-of-the-example
1011

src/renderer/html_handlebars/hbs_renderer.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ fn add_playground_pre(
931931
// we need to inject our own main
932932
let (attrs, code) = partition_source(code);
933933

934-
format!("# #![allow(unused)]\n{attrs}#fn main() {{\n{code}#}}").into()
934+
format!("# #![allow(unused)]\n{attrs}# fn main() {{\n{code}# }}").into()
935935
};
936936
content
937937
}
@@ -1003,12 +1003,9 @@ fn hide_lines_rust(content: &str) -> String {
10031003
result += &caps[3];
10041004
result += newline;
10051005
continue;
1006-
} else if &caps[2] != "!" && &caps[2] != "[" {
1006+
} else if matches!(&caps[2], "" | " ") {
10071007
result += "<span class=\"boring\">";
10081008
result += &caps[1];
1009-
if &caps[2] != " " {
1010-
result += &caps[2];
1011-
}
10121009
result += &caps[3];
10131010
result += newline;
10141011
result += "</span>";
@@ -1134,7 +1131,7 @@ mod tests {
11341131
fn add_playground() {
11351132
let inputs = [
11361133
("<code class=\"language-rust\">x()</code>",
1137-
"<pre class=\"playground\"><code class=\"language-rust\"># #![allow(unused)]\n#fn main() {\nx()\n#}</code></pre>"),
1134+
"<pre class=\"playground\"><code class=\"language-rust\"># #![allow(unused)]\n# fn main() {\nx()\n# }</code></pre>"),
11381135
("<code class=\"language-rust\">fn main() {}</code>",
11391136
"<pre class=\"playground\"><code class=\"language-rust\">fn main() {}</code></pre>"),
11401137
("<code class=\"language-rust editable\">let s = \"foo\n # bar\n\";</code>",
@@ -1164,7 +1161,7 @@ mod tests {
11641161
fn add_playground_edition2015() {
11651162
let inputs = [
11661163
("<code class=\"language-rust\">x()</code>",
1167-
"<pre class=\"playground\"><code class=\"language-rust edition2015\"># #![allow(unused)]\n#fn main() {\nx()\n#}</code></pre>"),
1164+
"<pre class=\"playground\"><code class=\"language-rust edition2015\"># #![allow(unused)]\n# fn main() {\nx()\n# }</code></pre>"),
11681165
("<code class=\"language-rust\">fn main() {}</code>",
11691166
"<pre class=\"playground\"><code class=\"language-rust edition2015\">fn main() {}</code></pre>"),
11701167
("<code class=\"language-rust edition2015\">fn main() {}</code>",
@@ -1188,7 +1185,7 @@ mod tests {
11881185
fn add_playground_edition2018() {
11891186
let inputs = [
11901187
("<code class=\"language-rust\">x()</code>",
1191-
"<pre class=\"playground\"><code class=\"language-rust edition2018\"># #![allow(unused)]\n#fn main() {\nx()\n#}</code></pre>"),
1188+
"<pre class=\"playground\"><code class=\"language-rust edition2018\"># #![allow(unused)]\n# fn main() {\nx()\n# }</code></pre>"),
11921189
("<code class=\"language-rust\">fn main() {}</code>",
11931190
"<pre class=\"playground\"><code class=\"language-rust edition2018\">fn main() {}</code></pre>"),
11941191
("<code class=\"language-rust edition2015\">fn main() {}</code>",
@@ -1212,7 +1209,7 @@ mod tests {
12121209
fn add_playground_edition2021() {
12131210
let inputs = [
12141211
("<code class=\"language-rust\">x()</code>",
1215-
"<pre class=\"playground\"><code class=\"language-rust edition2021\"># #![allow(unused)]\n#fn main() {\nx()\n#}</code></pre>"),
1212+
"<pre class=\"playground\"><code class=\"language-rust edition2021\"># #![allow(unused)]\n# fn main() {\nx()\n# }</code></pre>"),
12161213
("<code class=\"language-rust\">fn main() {}</code>",
12171214
"<pre class=\"playground\"><code class=\"language-rust edition2021\">fn main() {}</code></pre>"),
12181215
("<code class=\"language-rust edition2015\">fn main() {}</code>",
@@ -1237,8 +1234,12 @@ mod tests {
12371234
fn hide_lines_language_rust() {
12381235
let inputs = [
12391236
(
1240-
"<pre class=\"playground\"><code class=\"language-rust\">\n# #![allow(unused)]\n#fn main() {\nx()\n#}</code></pre>",
1237+
"<pre class=\"playground\"><code class=\"language-rust\">\n# #![allow(unused)]\n# fn main() {\nx()\n# }</code></pre>",
12411238
"<pre class=\"playground\"><code class=\"language-rust\">\n<span class=\"boring\">#![allow(unused)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}</span></code></pre>",),
1239+
// # must be followed by a space for a line to be hidden
1240+
(
1241+
"<pre class=\"playground\"><code class=\"language-rust\">\n#fn main() {\nx()\n#}</code></pre>",
1242+
"<pre class=\"playground\"><code class=\"language-rust\">\n#fn main() {\nx()\n#}</code></pre>",),
12421243
(
12431244
"<pre class=\"playground\"><code class=\"language-rust\">fn main() {}</code></pre>",
12441245
"<pre class=\"playground\"><code class=\"language-rust\">fn main() {}</code></pre>",),

0 commit comments

Comments
 (0)