mkdocs-material/docs/customization.md

241 lines
8.3 KiB
Markdown
Raw Normal View History

2016-02-09 23:59:37 +03:00
# Customization
2017-01-03 01:11:32 +03:00
## A great starting point
2016-02-09 23:59:37 +03:00
2020-03-10 16:07:03 +03:00
Project documentation is as diverse as the projects themselves and Material for
MkDocs is a good starting point for making it look great. However, as you write
2017-03-11 20:36:34 +03:00
your documentation, you may reach a point where some small adjustments are
2020-03-10 16:07:03 +03:00
necessary to preserve your brand's style.
2016-02-09 23:59:37 +03:00
2017-01-03 01:11:32 +03:00
## Adding assets
2016-02-09 23:59:37 +03:00
2017-01-03 01:11:32 +03:00
[MkDocs][1] provides several ways to interfere with themes. In order to make a
few tweaks to an existing theme, you can just add your stylesheets and
JavaScript files to the `docs` directory.
[1]: https://www.mkdocs.org
2017-01-03 01:11:32 +03:00
### Additional stylesheets
If you want to tweak some colors or change the spacing of certain elements,
you can do this in a separate stylesheet. The easiest way is by creating a
new stylesheet file in your `docs` directory:
2017-01-03 01:11:32 +03:00
``` sh
mkdir docs/stylesheets
touch docs/stylesheets/extra.css
```
Then, add the following line to your `mkdocs.yml`:
``` yaml
2017-01-09 23:41:30 +03:00
extra_css:
2020-03-10 13:55:25 +03:00
- stylesheets/extra.css
2017-01-03 01:11:32 +03:00
```
Spin up the development server with `mkdocs serve` and start typing your
changes in your additional stylesheet file you can see them instantly after
2020-03-10 16:07:03 +03:00
saving, as the MkDocs development server supports live reloading.
2017-01-03 01:11:32 +03:00
### Additional JavaScript
The same is true for additional JavaScript. If you want to integrate another
syntax highlighter or add some custom logic to your theme, create a new
JavaScript file in your `docs` directory:
2017-01-03 01:11:32 +03:00
``` sh
mkdir docs/javascripts
touch docs/javascripts/extra.js
```
Then, add the following line to your `mkdocs.yml`:
``` yaml
2017-01-09 23:41:30 +03:00
extra_javascript:
2020-03-10 13:55:25 +03:00
- javascripts/extra.js
2017-01-03 01:11:32 +03:00
```
Further assistance can be found in the [MkDocs documentation][2].
[2]: https://www.mkdocs.org/user-guide/styling-your-docs/#customizing-a-theme
2017-01-03 01:11:32 +03:00
## Extending the theme
If you want to alter the HTML source (e.g. add or remove some part), you can
2020-03-10 16:07:03 +03:00
extend the theme. MkDocs supports [theme extension][3], an easy way to override
parts of a theme without forking and changing the main theme.
2017-01-03 01:11:32 +03:00
[3]: https://www.mkdocs.org/user-guide/styling-your-docs/#using-the-theme-custom_dir
2017-01-03 01:11:32 +03:00
### Setup and theme structure
Reference the Material theme as usual in your `mkdocs.yml`, and create a
2020-03-10 16:07:03 +03:00
new folder for `overrides` which you reference using `custom_dir`:
2016-02-09 23:59:37 +03:00
``` yaml
2017-10-31 21:48:20 +03:00
theme:
2020-03-10 13:55:25 +03:00
name: material
2020-03-10 16:07:03 +03:00
custom_dir: overrides
2017-01-03 01:11:32 +03:00
```
!!! warning "Theme extension prerequisites"
2017-10-31 21:48:20 +03:00
As the `custom_dir` variable is used for the theme extension process, the
2020-03-10 16:07:03 +03:00
Material for MkDocs needs to be installed via `pip` and referenced with the
2017-10-31 21:48:20 +03:00
`name` parameter in your `mkdocs.yml`.
2017-01-03 01:11:32 +03:00
2020-03-10 16:07:03 +03:00
The structure in the `overrides` directory must mirror the directory structure
of the original theme, as any file in the `overrides` directory will replace the
file with the same name which is part of the original theme. Besides, further
assets may also be put in the `overrides` directory.
2017-01-03 01:11:32 +03:00
2020-03-10 16:07:03 +03:00
The directory layout of the theme is as follows:
2017-01-03 01:11:32 +03:00
``` sh
.
├─ assets/
│ ├─ images/ # Images and icons
│ ├─ javascripts/ # JavaScript
│ └─ stylesheets/ # Stylesheets
├─ partials/
2018-01-22 00:56:54 +03:00
│ ├─ integrations/ # 3rd-party integrations
2017-10-31 21:48:20 +03:00
│ ├─ language/ # Localized languages
2017-01-03 01:11:32 +03:00
│ ├─ footer.html # Footer bar
│ ├─ header.html # Header bar
2018-01-22 00:56:54 +03:00
│ ├─ hero.html # Hero teaser
2017-01-13 02:31:37 +03:00
│ ├─ language.html # Localized labels
2017-01-03 01:11:32 +03:00
│ ├─ nav-item.html # Main navigation item
│ ├─ nav.html # Main navigation
│ ├─ search.html # Search box
│ ├─ social.html # Social links
2020-03-10 16:07:03 +03:00
│ ├─ source-date.html # Last updated date
│ ├─ source-link.html # Link to source file
2017-01-03 01:11:32 +03:00
│ ├─ source.html # Repository information
│ ├─ tabs-item.html # Tabs navigation item
│ ├─ tabs.html # Tabs navigation
2017-01-03 01:11:32 +03:00
│ ├─ toc-item.html # Table of contents item
│ └─ toc.html # Table of contents
├─ 404.html # 404 error page
├─ base.html # Base template
└─ main.html # Default page
```
### Overriding partials
In order to override the footer, we can replace the `footer.html` partial with
our own partial. To do this, create the file `partials/footer.html` in the
2020-03-10 16:07:03 +03:00
`overrides` directory. MkDocs will now use the new partial when rendering the
theme. This can be done with any file.
2017-01-03 01:11:32 +03:00
### Overriding template blocks
2020-03-10 16:07:03 +03:00
Besides overriding partials, one can also override so called *template blocks*,
which are defined inside the templates and wrap specific features. To override a
template block, create a `main.html` inside the `overrides` directory and define
the block, e.g.:
2017-01-03 01:11:32 +03:00
``` jinja
{% extends "base.html" %}
{% block htmltitle %}
<title>Lorem ipsum dolor sit amet</title>
{% endblock %}
2016-02-09 23:59:37 +03:00
```
2020-03-10 16:07:03 +03:00
Material for MkDocs provides the following template blocks:
2017-01-03 01:11:32 +03:00
| Block name | Wrapped contents |
| ------------ | ----------------------------------------------- |
| `analytics` | Wraps the Google Analytics integration |
2020-03-10 16:07:03 +03:00
| `announce` | Wraps the Announcement bar |
| `config` | Wraps the JavaScript application config |
2017-01-03 01:11:32 +03:00
| `content` | Wraps the main content |
| `disqus` | Wraps the disqus integration |
2017-01-03 01:11:32 +03:00
| `extrahead` | Empty block to define additional meta tags |
| `fonts` | Wraps the webfont definitions |
| `footer` | Wraps the footer with navigation and copyright |
| `header` | Wraps the fixed header bar |
2020-02-13 12:10:04 +03:00
| `hero` | Wraps the hero teaser (if available) |
2017-01-03 01:11:32 +03:00
| `htmltitle` | Wraps the `<title>` tag |
2020-02-13 12:10:04 +03:00
| `libs` | Wraps the JavaScript libraries (header) |
| `scripts` | Wraps the JavaScript application (footer) |
| `source` | Wraps the linked source files |
2017-01-03 01:11:32 +03:00
| `site_meta` | Wraps the meta tags in the document head |
| `site_nav` | Wraps the site navigation and table of contents |
| `styles` | Wraps the stylesheets (also extra sources) |
2020-02-13 12:10:04 +03:00
| `tabs` | Wraps the tabs navigation (if available) |
2017-01-03 01:11:32 +03:00
For more on this topic refer to the [MkDocs documentation][4]
2016-02-09 23:59:37 +03:00
[4]: https://www.mkdocs.org/user-guide/styling-your-docs/#overriding-template-blocks
2016-02-09 23:59:37 +03:00
2017-01-03 01:11:32 +03:00
## Theme development
2016-02-09 23:59:37 +03:00
2020-03-10 16:07:03 +03:00
Material for MkDocs uses [Webpack][5] as a build tool to leverage modern web
technologies like [TypeScript][6] and [SASS][7]. If you want to make more
fundamental changes, it may be necessary to make the adjustments directly in
the source of the theme and recompile it. This is fairly easy.
2016-02-09 23:59:37 +03:00
[5]: https://webpack.js.org/
2020-03-10 16:07:03 +03:00
[6]: https://www.typescriptlang.org/
[7]: https://sass-lang.com
2016-02-17 20:08:11 +03:00
2017-01-03 01:11:32 +03:00
### Environment setup
2020-03-10 16:07:03 +03:00
In order to start development on Material for MkDocs, a [Node.js][8] version of
at least 12 is required. First, clone the repository:
2016-02-09 23:59:37 +03:00
``` sh
git clone https://github.com/squidfunk/mkdocs-material
```
2017-01-03 01:11:32 +03:00
Next, all dependencies need to be installed, which is done with:
2016-02-09 23:59:37 +03:00
``` sh
cd mkdocs-material
pip install -r requirements.txt
2018-02-21 23:06:11 +03:00
npm install
2016-02-09 23:59:37 +03:00
```
2017-01-03 01:11:32 +03:00
[8]: https://nodejs.org
2016-02-09 23:59:37 +03:00
2017-01-03 01:11:32 +03:00
### Development mode
Start the Webpack watchdog with:
2016-02-09 23:59:37 +03:00
``` sh
2020-03-10 16:07:03 +03:00
npm start
2016-02-09 23:59:37 +03:00
```
Then, in a second session, start the MkDocs server with:
```sh
mkdocs serve
```
Point your browser to [localhost:8000][9] and you should see this documentation in front of you.
2016-02-09 23:59:37 +03:00
!!! warning "Automatically generated files"
Never make any changes in the `material` directory, as the contents of this
directory are automatically generated from the `src` directory and will be
overridden when the theme is built.
2020-03-10 16:07:03 +03:00
[9]: http://localhost:8000
2016-02-10 02:01:17 +03:00
2017-01-03 01:11:32 +03:00
### Build process
2016-02-09 23:59:37 +03:00
2017-03-11 20:36:34 +03:00
When you've finished making your changes, you can build the theme by invoking:
2016-02-09 23:59:37 +03:00
``` sh
2018-02-21 23:06:11 +03:00
npm run build
2016-02-09 23:59:37 +03:00
```
2017-01-03 01:11:32 +03:00
This triggers the production-level compilation and minification of all
2020-03-10 16:07:03 +03:00
stylesheets and JavaScript sources. When the command exits, the final files are
2017-03-11 20:36:34 +03:00
located in the `material` directory. Add the `theme_dir` variable pointing to
the aforementioned directory in your original `mkdocs.yml`.
2016-02-17 20:08:11 +03:00
Now you can run `mkdocs build` and you should see your documentation with your
2020-03-10 16:07:03 +03:00
changes to the original theme.