Merge pull request #2530 from max-heller/rust-hidelines

fix: make line hiding in Rust code blocks consistent with `rustdoc`
This commit is contained in:
Eric Huss 2025-01-25 21:50:47 +00:00 committed by GitHub
commit 0ee565a5ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 11 deletions

View file

@ -4,7 +4,8 @@
There is a feature in mdBook that lets you hide code lines by prepending them with a specific prefix. There is a feature in mdBook that lets you hide code lines by prepending them with a specific prefix.
For the Rust language, you can use the `#` character as a prefix which will hide lines [like you would with Rustdoc][rustdoc-hide]. For the Rust language, you can prefix lines with `# ` (`#` followed by a space) to hide them [like you would with Rustdoc][rustdoc-hide].
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)
[rustdoc-hide]: https://doc.rust-lang.org/stable/rustdoc/write-documentation/documentation-tests.html#hiding-portions-of-the-example [rustdoc-hide]: https://doc.rust-lang.org/stable/rustdoc/write-documentation/documentation-tests.html#hiding-portions-of-the-example

View file

@ -1003,12 +1003,9 @@ fn hide_lines_rust(content: &str) -> String {
result += &caps[3]; result += &caps[3];
result += newline; result += newline;
continue; continue;
} else if &caps[2] != "!" && &caps[2] != "[" { } else if matches!(&caps[2], "" | " ") {
result += "<span class=\"boring\">"; result += "<span class=\"boring\">";
result += &caps[1]; result += &caps[1];
if &caps[2] != " " {
result += &caps[2];
}
result += &caps[3]; result += &caps[3];
result += newline; result += newline;
result += "</span>"; result += "</span>";
@ -1239,6 +1236,10 @@ mod tests {
( (
"<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\">\n<span class=\"boring\">#![allow(unused)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}</span></code></pre>",), "<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>",),
// # must be followed by a space for a line to be hidden
(
"<pre class=\"playground\"><code class=\"language-rust\">\n#fn main() {\nx()\n#}</code></pre>",
"<pre class=\"playground\"><code class=\"language-rust\">\n#fn main() {\nx()\n#}</code></pre>",),
( (
"<pre class=\"playground\"><code class=\"language-rust\">fn main() {}</code></pre>", "<pre class=\"playground\"><code class=\"language-rust\">fn main() {}</code></pre>",
"<pre class=\"playground\"><code class=\"language-rust\">fn main() {}</code></pre>",), "<pre class=\"playground\"><code class=\"language-rust\">fn main() {}</code></pre>",),