1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2024-11-24 23:50:13 +01:00
mkdocs-material/docs/customization.md

313 lines
11 KiB
Markdown
Raw Normal View History

# Customization
Project documentation is as diverse as the projects themselves and Material for
2020-08-30 17:37:49 +02:00
MkDocs is a great starting point for making it look beautiful. However, as you
write your documentation, you may reach a point where small adjustments are
necessary to preserve your brand's style.
## Adding assets
2021-10-10 22:32:32 +02:00
[MkDocs] provides several ways to customize a theme. In order to make a few
2021-11-29 18:35:58 +01:00
small tweaks to Material for MkDocs, you can just add CSS and JavaScript files to
2021-10-10 22:32:32 +02:00
the `docs` directory.
2021-10-10 22:32:32 +02:00
[MkDocs]: https://www.mkdocs.org
2020-09-19 14:34:40 +02:00
### Additional CSS
If you want to tweak some colors or change the spacing of certain elements,
2021-12-10 10:12:07 +01:00
you can do this in a separate style sheet. The easiest way is by creating a
new style sheet file in the `docs` directory:
``` { .sh .no-copy }
2020-08-30 17:37:49 +02:00
.
├─ docs/
│ └─ stylesheets/
│ └─ extra.css
└─ mkdocs.yml
```
2021-10-10 22:32:32 +02:00
Then, add the following lines to `mkdocs.yml`:
``` yaml
extra_css:
- stylesheets/extra.css
```
### Additional JavaScript
2021-10-10 22:32:32 +02:00
If you want to integrate another syntax highlighter or add some custom logic to
your theme, create a new JavaScript file in the `docs` directory:
``` { .sh .no-copy }
2020-08-30 17:37:49 +02:00
.
├─ docs/
│ └─ javascripts/
│ └─ extra.js
└─ mkdocs.yml
```
2021-10-10 22:32:32 +02:00
Then, add the following lines to `mkdocs.yml`:
``` yaml
extra_javascript:
- javascripts/extra.js
```
## Extending the theme
2020-08-30 17:37:49 +02:00
If you want to alter the HTML source (e.g. add or remove some parts), you can
2021-10-10 22:32:32 +02:00
extend the theme. MkDocs supports [theme extension], an easy way to override
2020-08-30 17:37:49 +02:00
parts of Material for MkDocs without forking from git. This ensures that you
can update to the latest version more easily.
2021-10-10 22:32:32 +02:00
[theme extension]: https://www.mkdocs.org/user-guide/styling-your-docs/#using-the-theme-custom_dir
### Setup and theme structure
2020-08-30 17:37:49 +02:00
Enable Material for MkDocs as usual in `mkdocs.yml`, and create a new folder
2021-10-10 22:32:32 +02:00
for `overrides` which you then reference using the [`custom_dir`][custom_dir]
setting:
``` yaml
theme:
name: material
custom_dir: overrides
```
!!! warning "Theme extension prerequisites"
2021-10-10 22:32:32 +02:00
As the [`custom_dir`][custom_dir] setting is used for the theme extension
process, Material for MkDocs needs to be installed via `pip` and referenced
with the [`name`][name] setting in `mkdocs.yml`. It will not work when
cloning from `git`.
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
2021-10-10 22:32:32 +02:00
assets may also be put in the `overrides` directory:
``` { .sh .no-copy }
.
2020-07-22 11:57:41 +02:00
├─ .icons/ # Bundled icon sets
├─ assets/
│ ├─ images/ # Images and icons
2021-10-30 13:49:01 +02:00
│ ├─ javascripts/ # JavaScript files
2021-11-28 16:58:52 +01:00
│ └─ stylesheets/ # Style sheets
├─ partials/
2020-07-20 15:18:09 +02:00
│ ├─ integrations/ # Third-party integrations
2021-10-30 13:49:01 +02:00
│ │ ├─ analytics/ # Analytics integrations
2021-11-13 13:15:58 +01:00
│ │ └─ analytics.html # Analytics setup
2021-10-30 13:49:01 +02:00
│ ├─ languages/ # Translation languages
2022-09-11 19:25:40 +02:00
│ ├─ actions.html # Actions
│ ├─ comments.html # Comment system (empty by default)
│ ├─ consent.html # Consent
2021-11-14 08:46:35 +01:00
│ ├─ content.html # Page content
2021-11-13 14:23:10 +01:00
│ ├─ copyright.html # Copyright and theme information
2022-09-11 19:25:40 +02:00
│ ├─ feedback.html # Was this page helpful?
│ ├─ footer.html # Footer bar
│ ├─ header.html # Header bar
2022-09-11 19:25:40 +02:00
│ ├─ icons.html # Custom icons
2021-10-30 13:49:01 +02:00
│ ├─ language.html # Translation setup
│ ├─ logo.html # Logo in header and sidebar
│ ├─ nav.html # Main navigation
│ ├─ nav-item.html # Main navigation item
2022-09-11 19:25:40 +02:00
│ ├─ pagination.html # Pagination (used for blog)
│ ├─ post.html # Blog post excerpt
2021-11-13 14:23:10 +01:00
│ ├─ search.html # Search interface
│ ├─ social.html # Social links
│ ├─ source.html # Repository information
2021-10-10 22:32:32 +02:00
│ ├─ source-file.html # Source file information
│ ├─ tabs.html # Tabs navigation
│ ├─ tabs-item.html # Tabs navigation item
2022-09-11 19:25:40 +02:00
│ ├─ tags.html # Tags
│ ├─ toc.html # Table of contents
│ └─ toc-item.html # Table of contents item
├─ 404.html # 404 error page
├─ base.html # Base template
2022-09-12 09:42:53 +02:00
├─ blog.html # Blog index page
├─ blog-archive.html # Blog archive index page
├─ blog-category.html # Blog category index page
├─ blog-post.html # Blog post page
└─ main.html # Default page
```
2021-10-10 22:32:32 +02:00
[custom_dir]: https://www.mkdocs.org/user-guide/configuration/#custom_dir
[name]: https://www.mkdocs.org/user-guide/configuration/#name
### Overriding partials
2020-08-30 17:37:49 +02:00
In order to override a partial, we can replace it with a file of the same name
and location in the `overrides` directory. For example, to replace the original
2021-10-10 22:32:32 +02:00
`footer.html` partial, create a new `footer.html` partial in the `overrides`
2020-08-30 17:37:49 +02:00
directory:
``` { .sh .no-copy }
2020-08-30 17:37:49 +02:00
.
├─ overrides/
│ └─ partials/
│ └─ footer.html
└─ mkdocs.yml
```
MkDocs will now use the new partial when rendering the theme. This can be done
with any file.
2021-10-10 21:04:22 +02:00
### Overriding blocks <small>recommended</small> { #overriding-blocks data-toc-label="Overriding blocks" }
2020-08-30 17:37:49 +02:00
Besides overriding partials, it's also possible to override (and extend)
2021-10-10 22:32:32 +02:00
template blocks, which are defined inside the templates and wrap specific
features. In order to set up block overrides, create a `main.html` file inside
the `overrides` directory:
2020-08-30 17:37:49 +02:00
``` { .sh .no-copy }
2020-08-30 17:37:49 +02:00
.
├─ overrides/
│ └─ main.html
└─ mkdocs.yml
```
2021-10-10 22:32:32 +02:00
Then, e.g. to override the site title, add the following lines to `main.html`:
``` html
{% extends "base.html" %}
{% block htmltitle %}
<title>Lorem ipsum dolor sit amet</title>
{% endblock %}
```
2022-09-11 16:49:28 +02:00
If you intend to __add__ something to a block rather than to replace it
altogether with new content, use `{{ super() }}` inside the block to include the
original block content. This is particularly useful when adding third-party
scripts to your docs, e.g.
2022-09-11 16:49:28 +02:00
``` html
{% extends "base.html" %}
{% block scripts %}
2022-09-11 16:49:28 +02:00
<!-- Add scripts that need to run before here -->
{{ super() }}
<!-- Add scripts that need to run afterwards here -->
{% endblock %}
2022-09-11 16:49:28 +02:00
```
2021-10-10 22:32:32 +02:00
The following template blocks are provided by the theme:
2021-04-10 11:13:32 +02:00
| Block name | Purpose |
2021-11-30 19:33:13 +01:00
| :---------------- | :---------------------------------------------- |
| `analytics` | Wraps the Google Analytics integration |
| `announce` | Wraps the announcement bar |
| `config` | Wraps the JavaScript application config |
2022-09-11 19:25:40 +02:00
| `container` | Wraps the main content container |
| `content` | Wraps the main content |
| `extrahead` | Empty block to add custom meta tags |
| `fonts` | Wraps the font definitions |
| `footer` | Wraps the footer with navigation and copyright |
| `header` | Wraps the fixed header bar |
| `hero` | Wraps the hero teaser (if available) |
| `htmltitle` | Wraps the `<title>` tag |
| `libs` | Wraps the JavaScript libraries (header) |
| `outdated` | Wraps the version warning |
| `scripts` | Wraps the JavaScript application (footer) |
| `site_meta` | Wraps the meta tags in the document head |
| `site_nav` | Wraps the site navigation and table of contents |
2021-10-10 22:32:32 +02:00
| `styles` | Wraps the style sheets (also extra sources) |
| `tabs` | Wraps the tabs navigation (if available) |
## Theme development
2021-10-10 22:32:32 +02:00
Material for MkDocs is built on top of [TypeScript], [RxJS] and [SASS], and
uses a lean, custom build process to put everything together.[^1] 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.
2021-02-26 16:41:00 +01:00
[^1]:
2021-10-10 22:32:32 +02:00
Prior to :octicons-tag-24: 7.0.0 the build was based on Webpack, resulting
in occasional broken builds due to incompatibilities with loaders and
plugins. Therefore, we decided to swap Webpack for a leaner solution which
is now based on [RxJS] as the application itself. This allowed for the
pruning of more than 500 dependencies (~30% less).
2021-02-26 16:41:00 +01:00
2021-10-10 22:32:32 +02:00
[TypeScript]: https://www.typescriptlang.org/
[RxJS]: https://github.com/ReactiveX/rxjs
[SASS]: https://sass-lang.com
### Environment setup
2021-10-10 22:32:32 +02:00
In order to start development on Material for MkDocs, a [Node.js] version of
2021-02-26 16:41:00 +01:00
at least 14 is required. First, clone the repository:
```
git clone https://github.com/squidfunk/mkdocs-material
```
Next, all dependencies need to be installed, which is done with:
```
cd mkdocs-material
2022-03-01 09:28:42 +01:00
pip install -e .
pip install mkdocs-minify-plugin
pip install mkdocs-redirects
npm install
```
2021-10-10 22:32:32 +02:00
[Node.js]: https://nodejs.org
### Development mode
2021-02-22 23:28:43 +01:00
Start the watcher with:
```
npm start
```
2021-10-10 22:32:32 +02:00
Then, in a second terminal window, start the MkDocs live preview server with:
```
2021-10-30 13:29:35 +02:00
mkdocs serve --watch-theme
```
2021-10-10 22:32:32 +02:00
Point your browser to [localhost:8000][live preview] and you should see this
very documentation in front of you.
!!! 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
2021-06-12 13:29:20 +02:00
overwritten when the theme is built.
2021-10-10 22:32:32 +02:00
[live preview]: http://localhost:8000
### Building the theme
When you're finished making your changes, you can build the theme by invoking:
2022-04-09 23:33:08 +02:00
``` sh
npm run build # (1)!
```
2022-04-09 23:33:08 +02:00
1. While this command will build all theme files, it will skip the overrides
used in Material for MkDocs' own documentation which are not distributed
with the theme. If you forked the theme and want to build the overrides
as well, use:
```
npm run build:all
```
This will take longer, as now the icon search index, schema files, as
well as additional style sheet and JavaScript files are built.
2021-10-10 22:32:32 +02:00
This triggers the production-level compilation and minification of all style
sheets and JavaScript files. After the command exits, the compiled files are
located in the `material` directory. When running `mkdocs build`, you should
now see your changes to the original theme.
2023-06-02 11:18:46 +02:00
!!! note "Enterprise support"
If you're using Material for MkDocs in your organization and need
assistance, e.g., to __reduce build times__, __improve performance__ or
2023-06-06 11:39:17 +02:00
ensure compliance, [__get in touch__](mailto:contact@squidfunk.com)
2023-06-02 11:18:46 +02:00
to discuss our __enterprise support__ offerings. We're happy to help!