| 1 |
# Configuring Preprocessors |
| 2 |
|
| 3 |
Preprocessors are extensions that can modify the raw Markdown source before it gets sent to the renderer. |
| 4 |
|
| 5 |
The following preprocessors are built-in and included by default: |
| 6 |
|
| 7 |
- `links`: Expands the `{{ #playground }}`, `{{ #include }}`, and `{{ #rustdoc_include }}` handlebars |
| 8 |
helpers in a chapter to include the contents of a file. |
| 9 |
See [Including files] for more. |
| 10 |
- `index`: Convert all chapter files named `README.md` into `index.md`. That is |
| 11 |
to say, all `README.md` would be rendered to an index file `index.html` in the |
| 12 |
rendered book. |
| 13 |
|
| 14 |
The built-in preprocessors can be disabled with the [`build.use-default-preprocessors`] config option. |
| 15 |
|
| 16 |
The community has developed several preprocessors. |
| 17 |
See the [Third Party Plugins] wiki page for a list of available preprocessors. |
| 18 |
|
| 19 |
For information on how to create a new preprocessor, see the [Preprocessors for Developers] chapter. |
| 20 |
|
| 21 |
[Including files]: ../mdbook.md#including-files |
| 22 |
[`build.use-default-preprocessors`]: general.md#build-options |
| 23 |
[Third Party Plugins]: https://github.com/rust-lang/mdBook/wiki/Third-party-plugins |
| 24 |
[Preprocessors for Developers]: ../../for_developers/preprocessors.md |
| 25 |
|
| 26 |
## Custom preprocessor configuration |
| 27 |
|
| 28 |
Preprocessors can be added by including a `preprocessor` table in `book.toml` with the name of the preprocessor. |
| 29 |
For example, if you have a preprocessor called `mdbook-example`, then you can include it with: |
| 30 |
|
| 31 |
```toml |
| 32 |
[preprocessor.example] |
| 33 |
``` |
| 34 |
|
| 35 |
With this table, mdBook will execute the `mdbook-example` preprocessor. |
| 36 |
|
| 37 |
This table can include additional key-value pairs that are specific to the preprocessor. |
| 38 |
For example, if our example preprocessor needed some extra configuration options: |
| 39 |
|
| 40 |
```toml |
| 41 |
[preprocessor.example] |
| 42 |
some-extra-feature = true |
| 43 |
``` |
| 44 |
|
| 45 |
## Locking a preprocessor dependency to a renderer |
| 46 |
|
| 47 |
You can explicitly specify that a preprocessor should run for a renderer by |
| 48 |
binding the two together. |
| 49 |
|
| 50 |
```toml |
| 51 |
[preprocessor.example] |
| 52 |
renderers = ["html"] # example preprocessor only runs with the HTML renderer |
| 53 |
``` |
| 54 |
|
| 55 |
## Provide your own command |
| 56 |
|
| 57 |
By default when you add a `[preprocessor.foo]` table to your `book.toml` file, |
| 58 |
`mdbook` will try to invoke the `mdbook-foo` executable. If you want to use a |
| 59 |
different program name or pass in command-line arguments, this behaviour can |
| 60 |
be overridden by adding a `command` field. |
| 61 |
|
| 62 |
```toml |
| 63 |
[preprocessor.random] |
| 64 |
command = "python random.py" |
| 65 |
``` |
| 66 |
|
| 67 |
### Optional preprocessors |
| 68 |
|
| 69 |
If you enable a preprocessor that isn't installed, the default behavior is to throw an error. |
| 70 |
This behavior can be changed by marking the preprocessor as optional: |
| 71 |
|
| 72 |
```toml |
| 73 |
[preprocessor.example] |
| 74 |
optional = true |
| 75 |
``` |
| 76 |
|
| 77 |
This demotes the error to a warning. |
| 78 |
|
| 79 |
## Require a certain order |
| 80 |
|
| 81 |
The order in which preprocessors are run can be controlled with the `before` and `after` fields. |
| 82 |
For example, suppose you want your `linenos` preprocessor to process lines that may have been `{{#include}}`d; then you want it to run after the built-in `links` preprocessor, which you can require using either the `before` or `after` field: |
| 83 |
|
| 84 |
```toml |
| 85 |
[preprocessor.linenos] |
| 86 |
after = [ "links" ] |
| 87 |
``` |
| 88 |
|
| 89 |
or |
| 90 |
|
| 91 |
```toml |
| 92 |
[preprocessor.links] |
| 93 |
before = [ "linenos" ] |
| 94 |
``` |
| 95 |
|
| 96 |
It would also be possible, though redundant, to specify both of the above in the same config file. |
| 97 |
|
| 98 |
Preprocessors having the same priority specified through `before` and `after` are sorted by name. |
| 99 |
Any infinite loops will be detected and produce an error. |