Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support hidden lines in languages other than Rust #2093

Merged
merged 4 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions guide/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path
editable = true
line-numbers = true

[output.html.code.hidelines]
python = "~"

[output.html.search]
limit-results = 20
use-boolean-and = true
Expand Down
14 changes: 14 additions & 0 deletions guide/src/format/configuration/renderers.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,20 @@ runnable = true # displays a run button for rust code

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

### `[output.html.code]`

The `[output.html.code]` table provides options for controlling code blocks.

```toml
[output.html.code]
# A prefix string per language (one or more chars).
# Any line starting with whitespace+prefix is hidden.
hidelines = { python = "~" }
```

- **hidelines:** A table that defines how [hidden code lines](../mdbook.md#hiding-code-lines) work for each language.
The key is the language and the value is a string that will cause code lines starting with that prefix to be hidden.

### `[output.html.search]`

The `[output.html.search]` table provides options for controlling the built-in text [search].
Expand Down
50 changes: 45 additions & 5 deletions guide/src/format/mdbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

## Hiding code lines

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

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

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

```bash
# fn main() {
Expand All @@ -28,7 +28,47 @@ Will render as
# }
```

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

By default, this only works for code examples that are annotated with `rust`.
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):

```toml
[output.html.code.hidelines]
python = "~"
```

The prefix will hide any lines that begin with the given prefix. With the python prefix shown above, this:

```bash
~hidden()
nothidden():
~ hidden()
~hidden()
nothidden()
```

will render as

```python
~hidden()
nothidden():
~ hidden()
~hidden()
nothidden()
```

This behavior can be overridden locally with a different prefix. This has the same effect as above:

~~~markdown
```python,hidelines=!!!
!!!hidden()
nothidden():
!!! hidden()
!!!hidden()
nothidden()
```
~~~

## Rust Playground

Expand Down
32 changes: 0 additions & 32 deletions guide/src/format/theme/syntax-highlighting.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,38 +77,6 @@ the `theme` folder of your book.

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

## Hiding code lines

There is a feature in mdBook that lets you hide code lines by prepending them
with a `#`.


```bash
# fn main() {
let x = 5;
let y = 6;

println!("{}", x + y);
# }
```

Will render as

```rust
# fn main() {
let x = 5;
let y = 7;

println!("{}", x + y);
# }
```

**At the moment, this only works for code examples that are annotated with
`rust`. Because it would collide with semantics of some programming languages.
In the future, we want to make this configurable through the `book.toml` so that
everyone can benefit from it.**


## Improve default theme

If you think the default theme doesn't look quite right for a specific language,
Expand Down
19 changes: 19 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ pub struct HtmlConfig {
/// Playground settings.
#[serde(alias = "playpen")]
pub playground: Playground,
/// Code settings.
pub code: Code,
/// Print settings.
pub print: Print,
/// Don't render section labels.
Expand Down Expand Up @@ -556,6 +558,7 @@ impl Default for HtmlConfig {
additional_js: Vec::new(),
fold: Fold::default(),
playground: Playground::default(),
code: Code::default(),
print: Print::default(),
no_section_label: false,
search: None,
Expand Down Expand Up @@ -642,6 +645,22 @@ impl Default for Playground {
}
}

/// Configuration for tweaking how the the HTML renderer handles code blocks.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct Code {
/// A prefix string to hide lines per language (one or more chars).
pub hidelines: HashMap<String, String>,
}

impl Default for Code {
fn default() -> Code {
Code {
hidelines: HashMap::new(),
}
}
}

/// Configuration of the search functionality of the HTML renderer.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
Expand Down
Loading