Skip to content

Commit b70a8ee

Browse files
committed
Support hidden lines in languages other than Rust
1 parent 5921f59 commit b70a8ee

File tree

7 files changed

+278
-109
lines changed

7 files changed

+278
-109
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
@@ -217,6 +217,17 @@ line-numbers = false # displays line numbers for editable code
217217

218218
[Ace]: https://ace.c9.io/
219219

220+
### `[output.html.code]`
221+
222+
The `[output.html.code]` table provides options for controlling code blocks.
223+
224+
```toml
225+
[output.html.code]
226+
# A prefix string per language (one or more chars).
227+
# Any line starting with whitespace+prefix is hidden.
228+
hidelines = { python = "~" }
229+
```
230+
220231
### `[output.html.search]`
221232

222233
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
@@ -507,6 +507,8 @@ pub struct HtmlConfig {
507507
/// Playground settings.
508508
#[serde(alias = "playpen")]
509509
pub playground: Playground,
510+
/// Code settings.
511+
pub code: Code,
510512
/// Print settings.
511513
pub print: Print,
512514
/// Don't render section labels.
@@ -560,6 +562,7 @@ impl Default for HtmlConfig {
560562
additional_js: Vec::new(),
561563
fold: Fold::default(),
562564
playground: Playground::default(),
565+
code: Code::default(),
563566
print: Print::default(),
564567
no_section_label: false,
565568
search: None,
@@ -643,6 +646,22 @@ impl Default for Playground {
643646
}
644647
}
645648

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

0 commit comments

Comments
 (0)