Skip to content

Commit 7df1d8c

Browse files
jannik4thecodewarrior
authored andcommitted
Support hidden lines in languages other than Rust
Co-Authored-By: thecodewarrior <5467669+thecodewarrior@users.noreply.github.com>
1 parent 3a51abf commit 7df1d8c

File tree

7 files changed

+272
-93
lines changed

7 files changed

+272
-93
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,17 @@ 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+
226237
### `[output.html.search]`
227238

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

guide/src/format/mdbook.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
There is a feature in mdBook that lets you hide code lines by prepending them
66
with a `#` [like you would with Rustdoc][rustdoc-hide].
7-
This currently only works with Rust language code blocks.
87

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

@@ -30,6 +29,46 @@ Will render as
3029

3130
The code block has an eyeball icon (<i class="fa fa-eye"></i>) which will toggle the visibility of the hidden lines.
3231

32+
By default, this only works for code examples that are annotated with `rust`.
33+
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):
34+
35+
```toml
36+
[output.html.code.hidelines]
37+
python = "~"
38+
```
39+
40+
The prefix will hide any lines that begin with the given prefix. With the python prefix shown above, this:
41+
42+
```bash
43+
~hidden()
44+
nothidden():
45+
~ hidden()
46+
~hidden()
47+
nothidden()
48+
```
49+
50+
will render as
51+
52+
```python
53+
~hidden()
54+
nothidden():
55+
~ hidden()
56+
~hidden()
57+
nothidden()
58+
```
59+
60+
This behavior can be overridden locally with a different prefix. This has the same effect as above:
61+
62+
~~~bash
63+
```python,hidelines=!!!
64+
!!!hidden()
65+
nothidden():
66+
!!! hidden()
67+
!!!hidden()
68+
nothidden()
69+
```
70+
~~~
71+
3372
## Rust Playground
3473

3574
Rust language code blocks will automatically get a play button (<i class="fa fa-play"></i>) which will execute the code and display the output just below the code block.

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)