| 1 |
# Getting Started with MkDocs |
| 2 |
|
| 3 |
An introductory tutorial! |
| 4 |
|
| 5 |
--- |
| 6 |
|
| 7 |
## Installation |
| 8 |
|
| 9 |
To install MkDocs, run the following command from the command line: |
| 10 |
|
| 11 |
```bash |
| 12 |
pip install mkdocs |
| 13 |
``` |
| 14 |
|
| 15 |
For more details, see the [Installation Guide]. |
| 16 |
|
| 17 |
## Creating a new project |
| 18 |
|
| 19 |
Getting started is super easy. To create a new project, run the following |
| 20 |
command from the command line: |
| 21 |
|
| 22 |
```bash |
| 23 |
mkdocs new my-project |
| 24 |
cd my-project |
| 25 |
``` |
| 26 |
|
| 27 |
Take a moment to review the initial project that has been created for you. |
| 28 |
|
| 29 |
 |
| 30 |
|
| 31 |
There's a single configuration file named `mkdocs.yml`, and a folder named |
| 32 |
`docs` that will contain your documentation source files (`docs` is |
| 33 |
the default value for the [docs_dir] configuration setting). Right now the `docs` |
| 34 |
folder just contains a single documentation page, named `index.md`. |
| 35 |
|
| 36 |
MkDocs comes with a built-in dev-server that lets you preview your documentation |
| 37 |
as you work on it. Make sure you're in the same directory as the `mkdocs.yml` |
| 38 |
configuration file, and then start the server by running the `mkdocs serve` |
| 39 |
command: |
| 40 |
|
| 41 |
```console |
| 42 |
$ mkdocs serve |
| 43 |
INFO - Building documentation... |
| 44 |
INFO - Cleaning site directory |
| 45 |
INFO - Documentation built in 0.22 seconds |
| 46 |
INFO - [15:50:43] Watching paths for changes: 'docs', 'mkdocs.yml' |
| 47 |
INFO - [15:50:43] Serving on http://127.0.0.1:8000/ |
| 48 |
``` |
| 49 |
|
| 50 |
Open up <http://127.0.0.1:8000/> in your browser, and you'll see the default |
| 51 |
home page being displayed: |
| 52 |
|
| 53 |
 |
| 54 |
|
| 55 |
The dev-server also supports auto-reloading, and will rebuild your documentation |
| 56 |
whenever anything in the configuration file, documentation directory, or theme |
| 57 |
directory changes. |
| 58 |
|
| 59 |
Open the `docs/index.md` document in your text editor of choice, change the |
| 60 |
initial heading to `MkLorum`, and save your changes. Your browser will |
| 61 |
auto-reload and you should see your updated documentation immediately. |
| 62 |
|
| 63 |
Now try editing the configuration file: `mkdocs.yml`. Change the |
| 64 |
[`site_name`][site_name] setting to `MkLorum` and save the file. |
| 65 |
|
| 66 |
```yaml |
| 67 |
site_name: MkLorum |
| 68 |
``` |
| 69 |
|
| 70 |
Your browser should immediately reload, and you'll see your new site name take |
| 71 |
effect. |
| 72 |
|
| 73 |
 |
| 74 |
|
| 75 |
NOTE: |
| 76 |
The [`site_name`][site_name] configuration |
| 77 |
option is the only required option in your configuration file. |
| 78 |
|
| 79 |
## Adding pages |
| 80 |
|
| 81 |
Now add a second page to your documentation: |
| 82 |
|
| 83 |
```bash |
| 84 |
curl 'https://jaspervdj.be/lorem-markdownum/markdown.txt' > docs/about.md |
| 85 |
``` |
| 86 |
|
| 87 |
As our documentation site will include some navigation headers, you may want to |
| 88 |
edit the configuration file and add some information about the order, title, and |
| 89 |
nesting of each page in the navigation header by adding a [`nav`][nav] |
| 90 |
setting: |
| 91 |
|
| 92 |
```yaml |
| 93 |
site_name: MkLorum |
| 94 |
nav: |
| 95 |
- Home: index.md |
| 96 |
- About: about.md |
| 97 |
``` |
| 98 |
|
| 99 |
Save your changes and you'll now see a navigation bar with `Home` and `About` |
| 100 |
items on the left as well as `Search`, `Previous`, and `Next` items on the |
| 101 |
right. |
| 102 |
|
| 103 |
 |
| 104 |
|
| 105 |
Try the menu items and navigate back and forth between pages. Then click on |
| 106 |
`Search`. A search dialog will appear, allowing you to search for any text on |
| 107 |
any page. Notice that the search results include every occurrence of the search |
| 108 |
term on the site and links directly to the section of the page in which the |
| 109 |
search term appears. You get all of that with no effort or configuration on your |
| 110 |
part! |
| 111 |
|
| 112 |
 |
| 113 |
|
| 114 |
## Theming our documentation |
| 115 |
|
| 116 |
Now change the configuration file to alter how the documentation is displayed by |
| 117 |
changing the theme. Edit the `mkdocs.yml` file and add a [`theme`][theme] setting: |
| 118 |
|
| 119 |
```yaml |
| 120 |
site_name: MkLorum |
| 121 |
nav: |
| 122 |
- Home: index.md |
| 123 |
- About: about.md |
| 124 |
theme: readthedocs |
| 125 |
``` |
| 126 |
|
| 127 |
Save your changes, and you'll see the ReadTheDocs theme being used. |
| 128 |
|
| 129 |
 |
| 130 |
|
| 131 |
## Changing the Favicon Icon |
| 132 |
|
| 133 |
By default, MkDocs uses the [MkDocs favicon] icon. To use a different icon, create |
| 134 |
an `img` subdirectory in the `docs` directory and copy your custom `favicon.ico` |
| 135 |
file to that directory. MkDocs will automatically detect and use that file as your |
| 136 |
favicon icon. |
| 137 |
|
| 138 |
[MkDocs favicon]: img/favicon.ico |
| 139 |
|
| 140 |
## Building the site |
| 141 |
|
| 142 |
That's looking good. You're ready to deploy the first pass of your `MkLorum` |
| 143 |
documentation. First build the documentation: |
| 144 |
|
| 145 |
```bash |
| 146 |
mkdocs build |
| 147 |
``` |
| 148 |
|
| 149 |
This will create a new directory, named `site`. Take a look inside the |
| 150 |
directory: |
| 151 |
|
| 152 |
```console |
| 153 |
$ ls site |
| 154 |
about fonts index.html license search.html |
| 155 |
css img js mkdocs sitemap.xml |
| 156 |
``` |
| 157 |
|
| 158 |
Notice that your source documentation has been output as two HTML files named |
| 159 |
`index.html` and `about/index.html`. You also have various other media that's |
| 160 |
been copied into the `site` directory as part of the documentation theme. You |
| 161 |
even have a `sitemap.xml` file and `mkdocs/search_index.json`. |
| 162 |
|
| 163 |
If you're using source code control such as `git` you probably don't want to |
| 164 |
check your documentation builds into the repository. Add a line containing |
| 165 |
`site/` to your `.gitignore` file. |
| 166 |
|
| 167 |
```bash |
| 168 |
echo "site/" >> .gitignore |
| 169 |
``` |
| 170 |
|
| 171 |
If you're using another source code control tool you'll want to check its |
| 172 |
documentation on how to ignore specific directories. |
| 173 |
|
| 174 |
## Other Commands and Options |
| 175 |
|
| 176 |
There are various other commands and options available. For a complete list of |
| 177 |
commands, use the `--help` flag: |
| 178 |
|
| 179 |
```bash |
| 180 |
mkdocs --help |
| 181 |
``` |
| 182 |
|
| 183 |
To view a list of options available on a given command, use the `--help` flag |
| 184 |
with that command. For example, to get a list of all options available for the |
| 185 |
`build` command run the following: |
| 186 |
|
| 187 |
```bash |
| 188 |
mkdocs build --help |
| 189 |
``` |
| 190 |
|
| 191 |
## Deploying |
| 192 |
|
| 193 |
The documentation site that you just built only uses static files so you'll be |
| 194 |
able to host it from pretty much anywhere. Simply upload the contents of the |
| 195 |
entire `site` directory to wherever you're hosting your website from and |
| 196 |
you're done. For specific instructions on a number of common hosts, see the |
| 197 |
[Deploying your Docs][deploy] page. |
| 198 |
|
| 199 |
## Getting help |
| 200 |
|
| 201 |
See the [User Guide] for more complete documentation of all of MkDocs' features. |
| 202 |
|
| 203 |
To get help with MkDocs, please use the [GitHub discussions] or [GitHub issues]. |
| 204 |
|
| 205 |
[Installation Guide]: user-guide/installation.md |
| 206 |
[docs_dir]: user-guide/configuration.md#docs_dir |
| 207 |
[deploy]: user-guide/deploying-your-docs.md |
| 208 |
[nav]: user-guide/configuration.md#nav |
| 209 |
[GitHub discussions]: https://github.com/mkdocs/mkdocs/discussions |
| 210 |
[GitHub issues]: https://github.com/mkdocs/mkdocs/issues |
| 211 |
[site_name]: user-guide/configuration.md#site_name |
| 212 |
[theme]: user-guide/configuration.md#theme |
| 213 |
[User Guide]: user-guide/README.md |