Debian Codegraph @

guide/src/format/configuration/renderers.md

raw
1 # Configuring Renderers
2
3 Renderers (also called "backends") are responsible for creating the output of the book.
4
5 The following backends are built-in:
6
7 * [`html`](#html-renderer-options) --- This renders the book to HTML.
8 This is enabled by default if no other `[output]` tables are defined in `book.toml`.
9 * [`markdown`](#markdown-renderer) --- This outputs the book as markdown after running the preprocessors.
10 This is useful for debugging preprocessors.
11
12 The community has developed several backends.
13 See the [Third Party Plugins] wiki page for a list of available backends.
14
15 For information on how to create a new backend, see the [Backends for Developers] chapter.
16
17 [Third Party Plugins]: https://github.com/rust-lang/mdBook/wiki/Third-party-plugins
18 [Backends for Developers]: ../../for_developers/backends.md
19
20 ## Output tables
21
22 Backends can be added by including a `output` table in `book.toml` with the name of the backend.
23 For example, if you have a backend called `mdbook-wordcount`, then you can include it with:
24
25 ```toml
26 [output.wordcount]
27 ```
28
29 With this table, mdBook will execute the `mdbook-wordcount` backend.
30
31 This table can include additional key-value pairs that are specific to the backend.
32 For example, if our example backend needed some extra configuration options:
33
34 ```toml
35 [output.wordcount]
36 ignores = ["Example Chapter"]
37 ```
38
39 If you define any `[output]` tables, then the `html` backend is not enabled by default.
40 If you want to keep the `html` backend running, then just include it in the `book.toml` file.
41 For example:
42
43 ```toml
44 [book]
45 title = "My Awesome Book"
46
47 [output.wordcount]
48
49 [output.html]
50 ```
51
52 If more than one `output` table is included, this changes the behavior for the layout of the output directory.
53 If there is only one backend, then it places its output directly in the `book` directory (see [`build.build-dir`] to override this location).
54 If there is more than one backend, then each backend is placed in a separate directory underneath `book`.
55 For example, the above would have directories `book/html` and `book/wordcount`.
56
57 [`build.build-dir`]: general.md#build-options
58
59 ### Custom backend commands
60
61 By default when you add an `[output.foo]` table to your `book.toml` file,
62 `mdbook` will try to invoke the `mdbook-foo` executable.
63 If you want to use a different program name or pass in command-line arguments,
64 this behaviour can be overridden by adding a `command` field.
65
66 ```toml
67 [output.random]
68 command = "python random.py"
69 ```
70
71 ### Optional backends
72
73 If you enable a backend that isn't installed, the default behavior is to throw an error.
74 This behavior can be changed by marking the backend as optional:
75
76 ```toml
77 [output.wordcount]
78 optional = true
79 ```
80
81 This demotes the error to a warning.
82
83
84 ## HTML renderer options
85
86 The HTML renderer has a variety of options detailed below.
87 They should be specified in the `[output.html]` table of the `book.toml` file.
88
89 ```toml
90 # Example book.toml file with all output options.
91 [book]
92 title = "Example book"
93 authors = ["John Doe", "Jane Doe"]
94 description = "The example book covers examples."
95
96 [output.html]
97 theme = "my-theme"
98 default-theme = "light"
99 preferred-dark-theme = "navy"
100 smart-punctuation = true
101 definition-lists = true
102 admonitions = true
103 mathjax-support = false
104 additional-css = ["custom.css", "custom2.css"]
105 additional-js = ["custom.js"]
106 no-section-label = false
107 git-repository-url = "https://github.com/rust-lang/mdBook"
108 git-repository-icon = "fab-github"
109 edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}"
110 site-url = "/example-book/"
111 cname = "myproject.rs"
112 input-404 = "not-found.md"
113 sidebar-header-nav = true
114 ```
115
116 The following configuration options are available:
117
118 - **theme:** mdBook comes with a default theme and all the resource files needed
119 for it. But if this option is set, mdBook will selectively overwrite the theme
120 files with the ones found in the specified folder.
121 - **default-theme:** The theme color scheme to select by default in the
122 'Change Theme' dropdown. Defaults to `light`.
123 - **preferred-dark-theme:** The default dark theme. This theme will be used if
124 the browser requests the dark version of the site via the
125 [`prefers-color-scheme`](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme)
126 CSS media query. Defaults to `navy`.
127 - **smart-punctuation:** Converts quotes to curly quotes, `...` to `…`, `--` to en-dash, and `---` to em-dash.
128 See [Smart Punctuation](../markdown.md#smart-punctuation).
129 Defaults to `true`.
130 - **definition-lists:** Enables [definition lists](../markdown.md#definition-lists). Defaults to `true`.
131 - **admonitions:** Enables [admonitions](../markdown.md#admonitions). Defaults to `true`.
132 - **mathjax-support:** Adds support for [MathJax](../mathjax.md). Defaults to
133 `false`.
134 - **additional-css:** If you need to slightly change the appearance of your book
135 without overwriting the whole style, you can specify a set of stylesheets that
136 will be loaded after the default ones where you can surgically change the
137 style.
138 - **additional-js:** If you need to add some behaviour to your book without
139 removing the current behaviour, you can specify a set of JavaScript files that
140 will be loaded alongside the default one.
141 - **no-section-label:** mdBook by defaults adds numeric section labels in the table of
142 contents column. For example, "1.", "2.1". Set this option to true to disable
143 those labels. Defaults to `false`.
144 - **git-repository-url:** A url to the git repository for the book. If provided
145 an icon link will be output in the menu bar of the book.
146 - **git-repository-icon:** The Font Awesome icon class to use for the git repository link. Defaults to `fab-github` which looks like <i class="fab fa-github"></i>. If you are not using GitHub, another option to consider is `fas-code-fork` which looks like <i class="fas fa-code-fork"></i>. The start of the string should be `fa-` for regular icons, `fas-` for solid icons, or `fab-` for brand icons. See the [free icon set](https://fontawesome.com/v6/search) for the available icons.
147 - **edit-url-template:** Edit url template, when provided shows a
148 "Suggest an edit" button (which looks like <i class="fas fa-pencil"></i>) for directly jumping to editing the currently
149 viewed page. For e.g. GitHub projects set this to
150 `https://github.com/<owner>/<repo>/edit/<branch>/{path}` or for
151 Bitbucket projects set it to
152 `https://bitbucket.org/<owner>/<repo>/src/<branch>/{path}?mode=edit`
153 where {path} will be replaced with the full path of the file in the
154 repository.
155 - **input-404:** The name of the markdown file used for missing files.
156 The corresponding output file will be the same, with the extension replaced with `html`.
157 Defaults to `404.md`.
158 - **site-url:** The url where the book will be hosted. This is required to ensure
159 navigation links and script/css imports in the 404 file work correctly, even when accessing
160 urls in subdirectories. Defaults to `/`. If `site-url` is set,
161 make sure to use document relative links for your assets, meaning they should not start with `/`.
162 - **cname:** The DNS subdomain or apex domain at which your book will be hosted.
163 This string will be written to a file named CNAME in the root of your site, as
164 required by GitHub Pages (see [*Managing a custom domain for your GitHub Pages
165 site*][custom domain]).
166 - **hash-files:** Include a cryptographic "fingerprint" of the files' contents in static asset filenames,
167 so that if the contents of the file are changed, the name of the file will also change.
168 For example, `css/chrome.css` may become `css/chrome-9b8f428e.css`.
169 Chapter HTML files are not renamed.
170 Static CSS and JS files can reference each other using `{{ resource "filename" }}` directives.
171 Defaults to `true`.
172 - **sidebar-header-nav:** If `true`, the sidebar will contain navigation for headers on the current page. Default is `true`.
173
174 [custom domain]: https://docs.github.com/en/github/working-with-github-pages/managing-a-custom-domain-for-your-github-pages-site
175
176 ### `[output.html.print]`
177
178 The `[output.html.print]` table provides options for controlling the printable output.
179 By default, mdBook will include an icon on the top right of the book (which looks like <i class="fas fa-print"></i>) that will print the book as a single page.
180
181 ```toml
182 [output.html.print]
183 enable = true # include support for printable output
184 page-break = true # insert page-break after each chapter
185 ```
186
187 - **enable:** Enable print support. When `false`, all print support will not be
188 rendered. Defaults to `true`.
189 - **page-break:** Insert page breaks between chapters. Defaults to `true`.
190
191 ### `[output.html.fold]`
192
193 The `[output.html.fold]` table provides options for controlling folding of the chapter listing in the navigation sidebar.
194
195 ```toml
196 [output.html.fold]
197 enable = false # whether or not to enable section folding
198 level = 0 # the depth to start folding
199 ```
200
201 - **enable:** Enable section-folding. When off, all folds are open.
202 Defaults to `false`.
203 - **level:** The higher the more folded regions are open. When level is 0, all
204 folds are closed. Defaults to `0`.
205
206 ### `[output.html.playground]`
207
208 The `[output.html.playground]` table provides options for controlling Rust sample code blocks, and their integration with the [Rust Playground].
209
210 [Rust Playground]: https://play.rust-lang.org/
211
212 ```toml
213 [output.html.playground]
214 editable = false # allows editing the source code
215 copyable = true # include the copy button for copying code snippets
216 copy-js = true # includes the JavaScript for the code editor
217 line-numbers = false # displays line numbers for editable code
218 runnable = true # displays a run button for rust code
219 ```
220
221 - **editable:** Allow editing the source code. Defaults to `false`.
222 - **copyable:** Display the copy button on code snippets. Defaults to `true`.
223 - **copy-js:** Copy JavaScript files for the editor to the output directory.
224 Defaults to `true`.
225 - **line-numbers:** Display line numbers on editable sections of code. Requires both `editable` and `copy-js` to be `true`. Defaults to `false`.
226 - **runnable:** Displays a run button for rust code snippets. Changing this to `false` will disable the run in playground feature globally. Defaults to `true`.
227
228 [Ace]: https://ace.c9.io/
229
230 ### `[output.html.code]`
231
232 The `[output.html.code]` table provides options for controlling code blocks.
233
234 ```toml
235 [output.html.code]
236 # A prefix string per language (one or more chars).
237 # Any line starting with whitespace+prefix is hidden.
238 hidelines = { python = "~" }
239 ```
240
241 - **hidelines:** A table that defines how [hidden code lines](../mdbook.md#hiding-code-lines) work for each language.
242 The key is the language and the value is a string that will cause code lines starting with that prefix to be hidden.
243
244 ### `[output.html.search]`
245
246 The `[output.html.search]` table provides options for controlling the built-in text [search].
247 mdBook must be compiled with the `search` feature enabled (on by default).
248
249 [search]: ../../guide/reading.md#search
250
251 ```toml
252 [output.html.search]
253 enable = true # enables the search feature
254 limit-results = 30 # maximum number of search results
255 teaser-word-count = 30 # number of words used for a search result teaser
256 use-boolean-and = true # multiple search terms must all match
257 boost-title = 2 # ranking boost factor for matches in headers
258 boost-hierarchy = 1 # ranking boost factor for matches in page names
259 boost-paragraph = 1 # ranking boost factor for matches in text
260 expand = true # partial words will match longer terms
261 heading-split-level = 3 # link results to heading levels
262 copy-js = true # include Javascript code for search
263 ```
264
265 - **enable:** Enables the search feature. Defaults to `true`.
266 - **limit-results:** The maximum number of search results. Defaults to `30`.
267 - **teaser-word-count:** The number of words used for a search result teaser.
268 Defaults to `30`.
269 - **use-boolean-and:** Define the logical link between multiple search words. If
270 true, all search words must appear in each result. Defaults to `false`.
271 - **boost-title:** Boost factor for the search result score if a search word
272 appears in the header. Defaults to `2`.
273 - **boost-hierarchy:** Boost factor for the search result score if a search word
274 appears in the hierarchy. The hierarchy contains all titles of the parent
275 documents and all parent headings. Defaults to `1`.
276 - **boost-paragraph:** Boost factor for the search result score if a search word
277 appears in the text. Defaults to `1`.
278 - **expand:** True if search should match longer results e.g. search `micro`
279 should match `microwave`. Defaults to `true`.
280 - **heading-split-level:** Search results will link to a section of the document
281 which contains the result. Documents are split into sections by headings this
282 level or less. Defaults to `3`. (`### This is a level 3 heading`)
283 - **copy-js:** Copy JavaScript files for the search implementation to the output
284 directory. Defaults to `true`.
285
286 #### `[output.html.search.chapter]`
287
288 The [`output.html.search.chapter`] table provides the ability to modify search settings per chapter or directory. Each key is the path to the chapter source file or directory, and the value is a table of settings to apply to that path. This will merge recursively, with more specific paths taking precedence.
289
290 ```toml
291 [output.html.search.chapter]
292 # Disables search indexing for all chapters in the `appendix` directory.
293 "appendix" = { enable = false }
294 # Enables search indexing for just this one appendix chapter.
295 "appendix/glossary.md" = { enable = true }
296 ```
297
298 - **enable:** Enables or disables search indexing for the given chapters. Defaults to `true`. This does not override the overall `output.html.search.enable` setting; that must be `true` for any search functionality to be enabled. Be cautious when disabling indexing for chapters because that can potentially lead to user confusion when they search for terms and expect them to be found. This should only be used in exceptional circumstances where keeping the chapter in the index will cause issues with the quality of the search results.
299
300 ### `[output.html.redirect]`
301
302 The `[output.html.redirect]` table provides a way to add redirects.
303 This is useful when you move, rename, or remove a page to ensure that links to the old URL will go to the new location.
304
305 ```toml
306 [output.html.redirect]
307 "/appendices/bibliography.html" = "https://rustc-dev-guide.rust-lang.org/appendix/bibliography.html"
308 "/other-installation-methods.html" = "../infra/other-installation-methods.html"
309
310 # Fragment redirects also work.
311 "/some-existing-page.html#old-fragment" = "some-existing-page.html#new-fragment"
312
313 # Fragment redirects also work for deleted pages.
314 "/old-page.html" = "new-page.html"
315 "/old-page.html#old-fragment" = "new-page.html#new-fragment"
316 ```
317
318 The table contains key-value pairs where the key is where the redirect file needs to be created, as an absolute path from the build directory, (e.g. `/appendices/bibliography.html`).
319 The value can be any valid URI the browser should navigate to (e.g. `https://rust-lang.org/`, `/overview.html`, or `../bibliography.html`).
320
321 This will generate an HTML page which will automatically redirect to the given location.
322
323 When fragment redirects are specified, the page must use JavaScript to redirect to the correct location. This is useful if you rename or move a section header. Fragment redirects work with existing pages and deleted pages.
324
325 ## Markdown renderer
326
327 The Markdown renderer will run preprocessors and then output the resulting
328 Markdown. This is mostly useful for debugging preprocessors, especially in
329 conjunction with `mdbook test` to see the Markdown that `mdbook` is passing
330 to `rustdoc`.
331
332 The Markdown renderer is included with `mdbook` but disabled by default.
333 Enable it by adding an empty table to your `book.toml` as follows:
334
335 ```toml
336 [output.markdown]
337 ```
338
339 There are no configuration options for the Markdown renderer at this time;
340 only whether it is enabled or disabled.
341
342 See [the preprocessors documentation](preprocessors.md) for how to
343 specify which preprocessors should run before the Markdown renderer.