2015-08-05 20:36:21 +02:00
|
|
|
# Syntax Highlighting
|
|
|
|
|
|
2020-09-29 16:56:31 -07:00
|
|
|
mdBook uses [Highlight.js](https://highlightjs.org) with a custom theme
|
|
|
|
|
for syntax highlighting.
|
2015-08-05 20:36:21 +02:00
|
|
|
|
|
|
|
|
Automatic language detection has been turned off, so you will probably want to
|
2020-09-29 16:56:31 -07:00
|
|
|
specify the programming language you use like this:
|
2015-08-05 20:36:21 +02:00
|
|
|
|
2020-09-29 16:56:31 -07:00
|
|
|
~~~markdown
|
|
|
|
|
```rust
|
2015-08-05 20:36:21 +02:00
|
|
|
fn main() {
|
|
|
|
|
// Some code
|
|
|
|
|
}
|
2020-09-29 16:56:31 -07:00
|
|
|
```
|
|
|
|
|
~~~
|
|
|
|
|
|
|
|
|
|
## Supported languages
|
|
|
|
|
|
|
|
|
|
These languages are supported by default, but you can add more by supplying
|
|
|
|
|
your own `highlight.js` file:
|
|
|
|
|
|
|
|
|
|
- apache
|
|
|
|
|
- armasm
|
|
|
|
|
- bash
|
|
|
|
|
- c
|
|
|
|
|
- coffeescript
|
|
|
|
|
- cpp
|
|
|
|
|
- csharp
|
|
|
|
|
- css
|
|
|
|
|
- d
|
|
|
|
|
- diff
|
|
|
|
|
- go
|
|
|
|
|
- handlebars
|
|
|
|
|
- haskell
|
|
|
|
|
- http
|
|
|
|
|
- ini
|
|
|
|
|
- java
|
|
|
|
|
- javascript
|
|
|
|
|
- json
|
|
|
|
|
- julia
|
|
|
|
|
- kotlin
|
|
|
|
|
- less
|
|
|
|
|
- lua
|
|
|
|
|
- makefile
|
|
|
|
|
- markdown
|
|
|
|
|
- nginx
|
|
|
|
|
- objectivec
|
|
|
|
|
- perl
|
|
|
|
|
- php
|
|
|
|
|
- plaintext
|
|
|
|
|
- properties
|
|
|
|
|
- python
|
|
|
|
|
- r
|
|
|
|
|
- ruby
|
|
|
|
|
- rust
|
|
|
|
|
- scala
|
|
|
|
|
- scss
|
|
|
|
|
- shell
|
|
|
|
|
- sql
|
|
|
|
|
- swift
|
|
|
|
|
- typescript
|
|
|
|
|
- x86asm
|
|
|
|
|
- xml
|
|
|
|
|
- yaml
|
2015-08-05 20:36:21 +02:00
|
|
|
|
|
|
|
|
## Custom theme
|
2018-08-02 21:34:26 -05:00
|
|
|
Like the rest of the theme, the files used for syntax highlighting can be
|
|
|
|
|
overridden with your own.
|
2015-08-05 20:36:21 +02:00
|
|
|
|
2018-08-02 21:34:26 -05:00
|
|
|
- ***highlight.js*** normally you shouldn't have to overwrite this file, unless
|
|
|
|
|
you want to use a more recent version.
|
2015-08-05 20:36:21 +02:00
|
|
|
- ***highlight.css*** theme used by highlight.js for syntax highlighting.
|
|
|
|
|
|
2018-08-02 21:34:26 -05:00
|
|
|
If you want to use another theme for `highlight.js` download it from their
|
|
|
|
|
website, or make it yourself, rename it to `highlight.css` and put it in
|
2019-11-04 16:14:38 -08:00
|
|
|
the `theme` folder of your book.
|
2015-08-05 20:36:21 +02:00
|
|
|
|
|
|
|
|
Now your theme will be used instead of the default theme.
|
2015-12-30 15:04:24 +01:00
|
|
|
|
|
|
|
|
## Hiding code lines
|
|
|
|
|
|
2018-08-20 23:48:18 -04:00
|
|
|
There is a feature in mdBook that lets you hide code lines by prepending them
|
2018-08-02 21:34:26 -05:00
|
|
|
with a `#`.
|
2015-12-30 15:04:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
2016-08-06 15:10:41 -04:00
|
|
|
# fn main() {
|
2015-12-30 15:04:24 +01:00
|
|
|
let x = 5;
|
|
|
|
|
let y = 6;
|
|
|
|
|
|
|
|
|
|
println!("{}", x + y);
|
2016-08-06 15:10:41 -04:00
|
|
|
# }
|
2015-12-30 15:04:24 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Will render as
|
|
|
|
|
|
|
|
|
|
```rust
|
2016-08-06 15:10:41 -04:00
|
|
|
# fn main() {
|
2015-12-30 15:04:24 +01:00
|
|
|
let x = 5;
|
|
|
|
|
let y = 7;
|
|
|
|
|
|
|
|
|
|
println!("{}", x + y);
|
2016-08-06 15:10:41 -04:00
|
|
|
# }
|
2015-12-30 15:04:24 +01:00
|
|
|
```
|
|
|
|
|
|
2018-08-02 21:34:26 -05:00
|
|
|
**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.**
|
2015-12-30 15:04:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
## Improve default theme
|
|
|
|
|
|
2018-08-02 21:34:26 -05:00
|
|
|
If you think the default theme doesn't look quite right for a specific language,
|
|
|
|
|
or could be improved. Feel free to [submit a new
|
2019-10-29 08:04:16 -05:00
|
|
|
issue](https://github.com/rust-lang/mdBook/issues) explaining what you
|
2018-08-02 21:34:26 -05:00
|
|
|
have in mind and I will take a look at it.
|
2015-12-30 15:04:24 +01:00
|
|
|
|
|
|
|
|
You could also create a pull-request with the proposed improvements.
|
|
|
|
|
|
|
|
|
|
Overall the theme should be light and sober, without to many flashy colors.
|