Debian Codegraph @

docs/user-guide/writing-your-docs.md

raw
1 # Writing your docs
2
3 How to layout and write your Markdown source files.
4
5 ---
6
7 ## File layout
8
9 Your documentation source should be written as regular Markdown files (see
10 [Writing with Markdown](#writing-with-markdown) below), and placed in the
11 [documentation directory](configuration.md#docs_dir). By default, this directory
12 will be named `docs` and will exist at the top level of your project, alongside
13 the `mkdocs.yml` configuration file.
14
15 The simplest project you can create will look something like this:
16
17 ```text
18 mkdocs.yml
19 docs/
20 index.md
21 ```
22
23 By convention your project homepage should be named `index.md` (see [Index
24 pages](#index-pages) below for details). Any of the following file
25 extensions may be used for your Markdown source files: `markdown`, `mdown`,
26 `mkdn`, `mkd`, `md`. All Markdown files included in your documentation
27 directory will be rendered in the built site regardless of any settings.
28
29 NOTE:
30 Files and directories with names which begin with a dot (for example: `.foo.md` or `.bar/baz.md`) are ignored by MkDocs. This can be overridden with the [`exclude_docs` config](configuration.md#exclude_docs).
31
32 You can also create multi-page documentation, by creating several Markdown
33 files:
34
35 ```text
36 mkdocs.yml
37 docs/
38 index.md
39 about.md
40 license.md
41 ```
42
43 The file layout you use determines the URLs that are used for the generated
44 pages. Given the above layout, pages would be generated for the following URLs:
45
46 ```text
47 /
48 /about/
49 /license/
50 ```
51
52 You can also include your Markdown files in nested directories if that better
53 suits your documentation layout.
54
55 ```text
56 docs/
57 index.md
58 user-guide/getting-started.md
59 user-guide/configuration-options.md
60 license.md
61 ```
62
63 Source files inside nested directories will cause pages to be generated with
64 nested URLs, like so:
65
66 ```text
67 /
68 /user-guide/getting-started/
69 /user-guide/configuration-options/
70 /license/
71 ```
72
73 Any files which are not identified as Markdown files (by their file extension)
74 within the [documentation directory](configuration.md#docs_dir) are copied by
75 MkDocs to the built site unaltered. See
76 [how to link to images and media](#linking-to-images-and-media) below for details.
77
78 ### Index pages
79
80 When a directory is requested, by default, most web servers will return an index
81 file (usually named `index.html`) contained within that directory if one exists.
82 For that reason, the homepage in all of the examples above has been named
83 `index.md`, which MkDocs will render to `index.html` when building the site.
84
85 Many repository hosting sites provide special treatment for README files by
86 displaying the contents of the README file when browsing the contents of a
87 directory. Therefore, MkDocs will allow you to name your index pages as
88 `README.md` instead of `index.md`. In that way, when users are browsing your
89 source code, the repository host can display the index page of that directory as
90 it is a README file. However, when MkDocs renders your site, the file will be
91 renamed to `index.html` so that the server will serve it as a proper index file.
92
93 If both an `index.md` file and a `README.md` file are found in the same
94 directory, then the `index.md` file is used and the `README.md` file is
95 ignored.
96
97 ### Configure Pages and Navigation
98
99 The [nav](configuration.md#nav) configuration setting in your `mkdocs.yml` file
100 defines which pages are included in the global site navigation menu as well as
101 the structure of that menu. If not provided, the navigation will be
102 automatically created by discovering all the Markdown files in the
103 [documentation directory](configuration.md#docs_dir). An automatically created
104 navigation configuration will always be sorted alphanumerically by file name
105 (except that index files will always be listed first within a sub-section). You
106 will need to manually define your navigation configuration if you would like
107 your navigation menu sorted differently.
108
109 A minimal navigation configuration could look like this:
110
111 ```yaml
112 nav:
113 - index.md
114 - about.md
115 ```
116
117 All paths in the navigation configuration must be relative to the `docs_dir`
118 configuration option. If that option is set to the default value, `docs`, the
119 source files for the above configuration would be located at `docs/index.md` and
120 `docs/about.md`.
121
122 The above example will result in two navigation items being created at the top
123 level and with their titles inferred from the contents of the Markdown file or,
124 if no title is defined within the file, of the file name. To override the title
125 in the `nav` setting add a title right before the filename.
126
127 ```yaml
128 nav:
129 - Home: index.md
130 - About: about.md
131 ```
132
133 Note that if a title is defined for a page in the navigation, that title will be
134 used throughout the site for that page and will override any title defined
135 within the page itself.
136
137 Navigation sub-sections can be created by listing related pages together under a
138 section title. For example:
139
140 ```yaml
141 nav:
142 - Home: index.md
143 - User Guide:
144 - Writing your docs: writing-your-docs.md
145 - Styling your docs: styling-your-docs.md
146 - About:
147 - License: license.md
148 - Release Notes: release-notes.md
149 ```
150
151 With the above configuration we have three top level items: "Home", "User Guide"
152 and "About." "Home" is a link to the homepage for the site. Under the "User
153 Guide" section two pages are listed: "Writing your docs" and "Styling your
154 docs." Under the "About" section two more pages are listed: "License" and
155 "Release Notes."
156
157 Note that a section cannot have a page assigned to it. Sections are only
158 containers for child pages and sub-sections. You may nest sections as deeply as
159 you like. However, be careful that you don't make it too difficult for your
160 users to navigate through the site navigation by over-complicating the nesting.
161 While sections may mirror your directory structure, they do not have to.
162
163 Any pages not listed in your navigation configuration will still be rendered and
164 included with the built site, however, they will not be linked from the global
165 navigation and will not be included in the `previous` and `next` links. Such
166 pages will be "hidden" unless linked to directly.
167
168 ## Writing with Markdown
169
170 MkDocs pages must be authored in [Markdown][md], a lightweight markup language
171 which results in easy-to-read, easy-to-write plain text documents that can be
172 converted to valid HTML documents in a predictable manner.
173
174 MkDocs uses the [Python-Markdown] library to render Markdown documents to HTML.
175 Python-Markdown is almost completely compliant with the [reference
176 implementation][md], although there are a few very minor [differences].
177
178 In addition to the base Markdown [syntax] which is common across all Markdown
179 implementations, MkDocs includes support for extending the Markdown syntax with
180 Python-Markdown [extensions]. See the MkDocs' [markdown_extensions]
181 configuration setting for details on how to enable extensions.
182
183 MkDocs includes some extensions by default, which are highlighted below.
184
185 [Python-Markdown]: https://python-markdown.github.io/
186 [md]: https://daringfireball.net/projects/markdown/
187 [differences]: https://python-markdown.github.io/#differences
188 [syntax]: https://daringfireball.net/projects/markdown/syntax
189 [extensions]: https://python-markdown.github.io/extensions/
190 [markdown_extensions]: configuration.md#markdown_extensions
191
192 ### Internal links
193
194 MkDocs allows you to interlink your documentation by using regular Markdown
195 [links]. However, there are a few additional benefits to formatting those links
196 specifically for MkDocs as outlined below.
197
198 [links]: https://daringfireball.net/projects/markdown/syntax#link
199
200 #### Linking to pages
201
202 When linking between pages in the documentation you can simply use the regular
203 Markdown [linking][links] syntax, including the *relative path* to the Markdown
204 document you wish to link to.
205
206 ```markdown
207 Please see the [project license](license.md) for further details.
208 ```
209
210 When the MkDocs build runs, these Markdown links will automatically be
211 transformed into an HTML hyperlink to the appropriate HTML page.
212
213 WARNING:
214 Using absolute paths with links is not officially supported. Relative paths
215 are adjusted by MkDocs to ensure they are always relative to the page. Absolute
216 paths are not modified at all. This means that your links using absolute paths
217 might work fine in your local environment but they might break once you deploy
218 them to your production server.
219
220 If the target documentation file is in another directory you'll need to make
221 sure to include any relative directory path in the link.
222
223 ```markdown
224 Please see the [project license](../about/license.md) for further details.
225 ```
226
227 The [toc] extension is used by MkDocs to generate an ID for every header in your
228 Markdown documents. You can use that ID to link to a section within a target
229 document by using an anchor link. The generated HTML will correctly transform
230 the path portion of the link, and leave the anchor portion intact.
231
232 ```markdown
233 Please see the [project license](about.md#license) for further details.
234 ```
235
236 Note that IDs are created from the text of a header. All text is converted to
237 lowercase and any disallowed characters, including white-space, are converted to
238 dashes. Consecutive dashes are then reduced to a single dash.
239
240 There are a few configuration settings provided by the toc extension which you
241 can set in your `mkdocs.yml` configuration file to alter the default behavior:
242
243 * **`permalink`**
244
245 Generate permanent links at the end of each header. Default: `False`.
246
247 When set to True the paragraph symbol (¶ or `¶`) is used as the
248 link text. When set to a string, the provided string is used as the link
249 text. For example, to use the hash symbol (`#`) instead, do:
250
251 ```yaml
252 markdown_extensions:
253 - toc:
254 permalink: "#"
255 ```
256
257 * **`baselevel`**
258
259 Base level for headers. Default: `1`.
260
261 This setting allows the header levels to be automatically adjusted to fit
262 within the hierarchy of your HTML templates. For example, if the Markdown
263 text for a page should not contain any headers higher than level 2 (`<h2>`),
264 do:
265
266 ```yaml
267 markdown_extensions:
268 - toc:
269 baselevel: 2
270 ```
271
272 Then any headers in your document would be increased by 1. For example, the
273 header `# Header` would be rendered as a level 2 header (`<h2>`) in the HTML
274 output.
275
276 * **`separator`**
277
278 Word separator. Default: `-`.
279
280 Character which replaces white-space in generated IDs. If you prefer
281 underscores, then do:
282
283 ```yaml
284 markdown_extensions:
285 - toc:
286 separator: "_"
287 ```
288
289 Note that if you would like to define multiple of the above settings, you must
290 do so under a single `toc` entry in the `markdown_extensions` configuration
291 option.
292
293 ```yml
294 markdown_extensions:
295 - toc:
296 permalink: "#"
297 baselevel: 2
298 separator: "_"
299 ```
300
301 [toc]: https://python-markdown.github.io/extensions/toc/
302
303 #### Linking to images and media
304
305 As well as the Markdown source files, you can also include other file types in
306 your documentation, which will be copied across when generating your
307 documentation site. These might include images and other media.
308
309 For example, if your project documentation needed to include a [GitHub Pages
310 CNAME file] and a PNG formatted screenshot image then your file layout might
311 look as follows:
312
313 ```text
314 mkdocs.yml
315 docs/
316 CNAME
317 index.md
318 about.md
319 license.md
320 img/
321 screenshot.png
322 ```
323
324 To include images in your documentation source files, simply use any of the
325 regular Markdown image syntaxes:
326
327 ```Markdown
328 Cupcake indexer is a snazzy new project for indexing small cakes.
329
330 ![Screenshot](img/screenshot.png)
331
332 *Above: Cupcake indexer in progress*
333 ```
334
335 Your image will now be embedded when you build the documentation, and should
336 also be previewed if you're working on the documentation with a Markdown editor.
337
338 [GitHub Pages CNAME file]: https://help.github.com/articles/using-a-custom-domain-with-github-pages/
339
340 #### Linking from raw HTML
341
342 Markdown allows document authors to fall back to raw HTML when the Markdown
343 syntax does not meets the author's needs. MkDocs does not limit Markdown in this
344 regard. However, as all raw HTML is ignored by the Markdown parser, MkDocs is
345 not able to validate or convert links contained in raw HTML. When including
346 internal links within raw HTML, you will need to manually format the link
347 appropriately for the rendered document.
348
349 ### Meta-Data
350
351 MkDocs includes support for both YAML and MultiMarkdown style meta-data (often
352 called front-matter). Meta-data consists of a series of keywords and values
353 defined at the beginning of a Markdown document, which are stripped from the
354 document prior to it being processing by Python-Markdown. The key/value pairs
355 are passed by MkDocs to the page template. Therefore, if a theme includes
356 support, the values of any keys can be displayed on the page or used to control
357 the page rendering. See your theme's documentation for information about which
358 keys may be supported, if any.
359
360 In addition to displaying information in a template, MkDocs includes support for
361 a few predefined meta-data keys which can alter the behavior of MkDocs for that
362 specific page. The following keys are supported:
363
364 * **`template`**
365
366 The template to use with the current page.
367
368 By default, MkDocs uses the `main.html` template of a theme to render
369 Markdown pages. You can use the `template` meta-data key to define a
370 different template file for that specific page. The template file must be
371 available on the path(s) defined in the theme's environment.
372
373 * **`title`**
374
375 The "title" to use for the document.
376
377 MkDocs will attempt to determine the title of a document in the following
378 ways, in order:
379
380 1. A title defined in the [nav] configuration setting for a document.
381
382 2. A title defined in the `title` meta-data key of a document.
383
384 3. A level 1 Markdown header on the first line of the document body.
385 ([Setext-style] headers are supported *only since MkDocs 1.5*.)
386
387 4. The filename of a document.
388
389 Upon finding a title for a page, MkDoc does not continue checking any
390 additional sources in the above list.
391
392 [Setext-style]: https://daringfireball.net/projects/markdown/syntax#header
393
394 #### YAML Style Meta-Data
395
396 YAML style meta-data consists of [YAML] key/value pairs wrapped in YAML style
397 delimiters to mark the start and/or end of the meta-data. The first line of
398 a document must be `---`. The meta-data ends at the first line containing an
399 end deliminator (either `---` or `...`). The content between the delimiters is
400 parsed as [YAML].
401
402 ```text
403 ---
404 title: My Document
405 summary: A brief description of my document.
406 authors:
407 - Waylan Limberg
408 - Tom Christie
409 date: 2018-07-10
410 some_url: https://example.com
411 ---
412 This is the first paragraph of the document.
413 ```
414
415 YAML is able to detect data types. Therefore, in the above example, the values
416 of `title`, `summary` and `some_url` are strings, the value of `authors` is a
417 list of strings and the value of `date` is a `datetime.date` object. Note that
418 the YAML keys are case sensitive and MkDocs expects keys to be all lowercase.
419 The top level of the YAML must be a collection of key/value pairs, which results
420 in a Python `dict` being returned. If any other type is returned or the YAML
421 parser encounters an error, then MkDocs does not recognize the section as
422 meta-data, the page's `meta` attribute will be empty, and the section is not
423 removed from the document.
424
425 #### MultiMarkdown Style Meta-Data
426
427 MultiMarkdown style meta-data uses a format first introduced by the
428 [MultiMarkdown] project. The data consists of a series of keywords and values
429 defined at the beginning of a Markdown document, like this:
430
431 ```text
432 Title: My Document
433 Summary: A brief description of my document.
434 Authors: Waylan Limberg
435 Tom Christie
436 Date: January 23, 2018
437 blank-value:
438 some_url: https://example.com
439
440 This is the first paragraph of the document.
441 ```
442
443 The keywords are case-insensitive and may consist of letters, numbers,
444 underscores and dashes and must end with a colon. The values consist of anything
445 following the colon on the line and may even be blank.
446
447 If a line is indented by 4 or more spaces, that line is assumed to be an
448 additional line of the value for the previous keyword. A keyword may have as
449 many lines as desired. All lines are joined into a single string.
450
451 The first blank line ends all meta-data for the document. Therefore, the first
452 line of a document must not be blank.
453
454 NOTE:
455 MkDocs does not support YAML style delimiters (`---` or `...`) for
456 MultiMarkdown style meta-data. In fact, MkDocs relies on the the presence or
457 absence of the delimiters to determine whether YAML style meta-data or
458 MultiMarkdown style meta-data is being used. If the delimiters are
459 detected, but the content between the delimiters is not valid YAML
460 meta-data, MkDocs does not attempt to parse the content as MultiMarkdown
461 style meta-data.
462
463 [YAML]: https://yaml.org
464 [MultiMarkdown]: https://fletcherpenney.net/MultiMarkdown_Syntax_Guide#metadata
465 [nav]: configuration.md#nav
466
467 ### Tables
468
469 The [tables] extension adds a basic table syntax to Markdown which is popular
470 across multiple implementations. The syntax is rather simple and is generally
471 only useful for simple tabular data.
472
473 A simple table looks like this:
474
475 ```markdown
476 First Header | Second Header | Third Header
477 ------------ | ------------- | ------------
478 Content Cell | Content Cell | Content Cell
479 Content Cell | Content Cell | Content Cell
480 ```
481
482 If you wish, you can add a leading and tailing pipe to each line of the table:
483
484 ```markdown
485 | First Header | Second Header | Third Header |
486 | ------------ | ------------- | ------------ |
487 | Content Cell | Content Cell | Content Cell |
488 | Content Cell | Content Cell | Content Cell |
489 ```
490
491 Specify alignment for each column by adding colons to separator lines:
492
493 ```markdown
494 First Header | Second Header | Third Header
495 :----------- |:-------------:| -----------:
496 Left | Center | Right
497 Left | Center | Right
498 ```
499
500 Note that table cells cannot contain any block level elements and cannot contain
501 multiple lines of text. They can, however, include inline Markdown as defined in
502 Markdown's [syntax] rules.
503
504 Additionally, a table must be surrounded by blank lines. There must be a blank
505 line before and after the table.
506
507 [tables]: https://python-markdown.github.io/extensions/tables/
508
509 ### Fenced code blocks
510
511 The [fenced code blocks] extension adds an alternate method of defining code
512 blocks without indentation.
513
514 The first line should contain 3 or more backtick (`` ` ``) characters, and the
515 last line should contain the same number of backtick characters (`` ` ``):
516
517 ````markdown
518 ```
519 Fenced code blocks are like Standard
520 Markdown’s regular code blocks, except that
521 they’re not indented and instead rely on
522 start and end fence lines to delimit the
523 code block.
524 ```
525 ````
526
527 With this approach, the language can optionally be specified on the first line
528 after the backticks which informs any syntax highlighters of the language used:
529
530 ````markdown
531 ```python
532 def fn():
533 pass
534 ```
535 ````
536
537 Note that fenced code blocks can not be indented. Therefore, they cannot be
538 nested inside list items, blockquotes, etc.
539
540 [fenced code blocks]: https://python-markdown.github.io/extensions/fenced_code_blocks/