Skip to content

Commit 61708ad

Browse files
authored
Merge pull request #2093 from ehuss/hiddenlines
Support hidden lines in languages other than Rust
2 parents 3a51abf + c9cfe22 commit 61708ad

File tree

7 files changed

+282
-102
lines changed

7 files changed

+282
-102
lines changed

guide/book.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path
1717
editable = true
1818
line-numbers = true
1919

20+
[output.html.code.hidelines]
21+
python = "~"
22+
2023
[output.html.search]
2124
limit-results = 20
2225
use-boolean-and = true

guide/src/format/configuration/renderers.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,20 @@ runnable = true # displays a run button for rust code
223223

224224
[Ace]: https://ace.c9.io/
225225

226+
### `[output.html.code]`
227+
228+
The `[output.html.code]` table provides options for controlling code blocks.
229+
230+
```toml
231+
[output.html.code]
232+
# A prefix string per language (one or more chars).
233+
# Any line starting with whitespace+prefix is hidden.
234+
hidelines = { python = "~" }
235+
```
236+
237+
- **hidelines:** A table that defines how [hidden code lines](../mdbook.md#hiding-code-lines) work for each language.
238+
The key is the language and the value is a string that will cause code lines starting with that prefix to be hidden.
239+
226240
### `[output.html.search]`
227241

228242
The `[output.html.search]` table provides options for controlling the built-in text [search].

guide/src/format/mdbook.md

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
## Hiding code lines
44

5-
There is a feature in mdBook that lets you hide code lines by prepending them
6-
with a `#` [like you would with Rustdoc][rustdoc-hide].
7-
This currently only works with Rust language code blocks.
5+
There is a feature in mdBook that lets you hide code lines by prepending them with a specific prefix.
86

9-
[rustdoc-hide]: https://doc.rust-lang.org/stable/rustdoc/documentation-tests.html#hiding-portions-of-the-example
7+
For the Rust language, you can use the `#` character as a prefix which will hide lines [like you would with Rustdoc][rustdoc-hide].
8+
9+
[rustdoc-hide]: https://doc.rust-lang.org/stable/rustdoc/write-documentation/documentation-tests.html#hiding-portions-of-the-example
1010

1111
```bash
1212
# fn main() {
@@ -28,7 +28,47 @@ Will render as
2828
# }
2929
```
3030

31-
The code block has an eyeball icon (<i class="fa fa-eye"></i>) which will toggle the visibility of the hidden lines.
31+
When you tap or hover the mouse over the code block, there will be an eyeball icon (<i class="fa fa-eye"></i>) which will toggle the visibility of the hidden lines.
32+
33+
By default, this only works for code examples that are annotated with `rust`.
34+
However, you can define custom prefixes for other languages by adding a new line-hiding prefix in your `book.toml` with the language name and prefix character(s):
35+
36+
```toml
37+
[output.html.code.hidelines]
38+
python = "~"
39+
```
40+
41+
The prefix will hide any lines that begin with the given prefix. With the python prefix shown above, this:
42+
43+
```bash
44+
~hidden()
45+
nothidden():
46+
~ hidden()
47+
~hidden()
48+
nothidden()
49+
```
50+
51+
will render as
52+
53+
```python
54+
~hidden()
55+
nothidden():
56+
~ hidden()
57+
~hidden()
58+
nothidden()
59+
```
60+
61+
This behavior can be overridden locally with a different prefix. This has the same effect as above:
62+
63+
~~~markdown
64+
```python,hidelines=!!!
65+
!!!hidden()
66+
nothidden():
67+
!!! hidden()
68+
!!!hidden()
69+
nothidden()
70+
```
71+
~~~
3272

3373
## Rust Playground
3474

guide/src/format/theme/syntax-highlighting.md

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -77,38 +77,6 @@ the `theme` folder of your book.
7777

7878
Now your theme will be used instead of the default theme.
7979

80-
## Hiding code lines
81-
82-
There is a feature in mdBook that lets you hide code lines by prepending them
83-
with a `#`.
84-
85-
86-
```bash
87-
# fn main() {
88-
let x = 5;
89-
let y = 6;
90-
91-
println!("{}", x + y);
92-
# }
93-
```
94-
95-
Will render as
96-
97-
```rust
98-
# fn main() {
99-
let x = 5;
100-
let y = 7;
101-
102-
println!("{}", x + y);
103-
# }
104-
```
105-
106-
**At the moment, this only works for code examples that are annotated with
107-
`rust`. Because it would collide with semantics of some programming languages.
108-
In the future, we want to make this configurable through the `book.toml` so that
109-
everyone can benefit from it.**
110-
111-
11280
## Improve default theme
11381

11482
If you think the default theme doesn't look quite right for a specific language,

src/config.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,8 @@ pub struct HtmlConfig {
504504
/// Playground settings.
505505
#[serde(alias = "playpen")]
506506
pub playground: Playground,
507+
/// Code settings.
508+
pub code: Code,
507509
/// Print settings.
508510
pub print: Print,
509511
/// Don't render section labels.
@@ -556,6 +558,7 @@ impl Default for HtmlConfig {
556558
additional_js: Vec::new(),
557559
fold: Fold::default(),
558560
playground: Playground::default(),
561+
code: Code::default(),
559562
print: Print::default(),
560563
no_section_label: false,
561564
search: None,
@@ -642,6 +645,22 @@ impl Default for Playground {
642645
}
643646
}
644647

648+
/// Configuration for tweaking how the the HTML renderer handles code blocks.
649+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
650+
#[serde(default, rename_all = "kebab-case")]
651+
pub struct Code {
652+
/// A prefix string to hide lines per language (one or more chars).
653+
pub hidelines: HashMap<String, String>,
654+
}
655+
656+
impl Default for Code {
657+
fn default() -> Code {
658+
Code {
659+
hidelines: HashMap::new(),
660+
}
661+
}
662+
}
663+
645664
/// Configuration of the search functionality of the HTML renderer.
646665
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
647666
#[serde(default, rename_all = "kebab-case")]

0 commit comments

Comments
 (0)