| 1 |
# Developing Themes |
| 2 |
|
| 3 |
A guide to creating and distributing custom themes. |
| 4 |
|
| 5 |
--- |
| 6 |
|
| 7 |
NOTE: |
| 8 |
If you are looking for existing third party themes, they are listed in the |
| 9 |
[community wiki] page and the [MkDocs project catalog][catalog]. If you want to |
| 10 |
share a theme you create, you should list it there. |
| 11 |
|
| 12 |
When creating a new theme, you can either follow the steps in this guide to |
| 13 |
create one from scratch or you can download the `mkdocs-basic-theme` as a |
| 14 |
basic, yet complete, theme with all the boilerplate required. **You can find |
| 15 |
this base theme on [GitHub][basic theme]**. It contains detailed comments in |
| 16 |
the code to describe the different features and their usage. |
| 17 |
|
| 18 |
[community wiki]: https://github.com/mkdocs/mkdocs/wiki/MkDocs-Themes |
| 19 |
[catalog]: https://github.com/mkdocs/catalog#-theming |
| 20 |
[basic theme]: https://github.com/mkdocs/mkdocs-basic-theme |
| 21 |
|
| 22 |
## Creating a custom theme |
| 23 |
|
| 24 |
The bare minimum required for a custom theme is a `main.html` [Jinja2 |
| 25 |
template] file which is placed in a directory that is *not* a child of the |
| 26 |
[docs_dir]. Within `mkdocs.yml`, set the [`theme.custom_dir`][custom_dir] |
| 27 |
option to the path of the directory containing `main.html`. The path should be |
| 28 |
relative to the configuration file. For example, given this example project |
| 29 |
layout: |
| 30 |
|
| 31 |
```text |
| 32 |
mkdocs.yml |
| 33 |
docs/ |
| 34 |
index.md |
| 35 |
about.md |
| 36 |
custom_theme/ |
| 37 |
main.html |
| 38 |
... |
| 39 |
``` |
| 40 |
|
| 41 |
... you would include the following settings in `mkdocs.yml` to use the custom theme |
| 42 |
directory: |
| 43 |
|
| 44 |
```yaml |
| 45 |
theme: |
| 46 |
name: null |
| 47 |
custom_dir: 'custom_theme/' |
| 48 |
``` |
| 49 |
|
| 50 |
> NOTE: |
| 51 |
> Generally, when building your own custom theme, the theme.[name] |
| 52 |
> configuration setting would be set to `null`. However, if the |
| 53 |
> theme.[custom_dir] configuration value is used in combination with an |
| 54 |
> existing theme, the theme.[custom_dir] can be used to replace only specific |
| 55 |
> parts of a built-in theme. For example, with the above layout and if you set |
| 56 |
> `name: "mkdocs"` then the `main.html` file in the theme.[custom_dir] would |
| 57 |
> replace the file of the same name in the `mkdocs` theme but otherwise the |
| 58 |
> `mkdocs` theme would remain unchanged. This is useful if you want to make |
| 59 |
> small adjustments to an existing theme. |
| 60 |
> |
| 61 |
> For more specific information, see [Customizing Your Theme]. |
| 62 |
<!-- --> |
| 63 |
> WARNING: |
| 64 |
> A theme's [configuration] defined in a `mkdocs_theme.yml` file is not loaded |
| 65 |
> from `theme.custom_dir`. When an entire theme exists in `theme.custom_dir` |
| 66 |
> and `theme.name` is set to `null`, then the entire theme configuration must |
| 67 |
> be defined in the [theme] configuration option in the `mkdocs.yml` file. |
| 68 |
> |
| 69 |
> However, when a theme is [packaged] up for distribution, and loaded using |
| 70 |
> the `theme.name` configuration option, then a `mkdocs_theme.yml` file |
| 71 |
> is required for the theme. |
| 72 |
|
| 73 |
[Customizing Your Theme]: ../user-guide/customizing-your-theme.md#using-the-theme-custom_dir |
| 74 |
[custom_dir]: ../user-guide/configuration.md#custom_dir |
| 75 |
[name]: ../user-guide/configuration.md#name |
| 76 |
[docs_dir]: ../user-guide/configuration.md#docs_dir |
| 77 |
[configuration]: #theme-configuration |
| 78 |
[packaged]: #packaging-themes |
| 79 |
[theme]: ../user-guide/configuration.md#theme |
| 80 |
|
| 81 |
## Basic theme |
| 82 |
|
| 83 |
The simplest `main.html` file is the following: |
| 84 |
|
| 85 |
```django |
| 86 |
<!DOCTYPE html> |
| 87 |
<html> |
| 88 |
<head> |
| 89 |
<title>{% if page.title %}{{ page.title }} - {% endif %}{{ config.site_name }}</title> |
| 90 |
{%- for path in config.extra_css %} |
| 91 |
<link href="{{ path | url }}" rel="stylesheet"> |
| 92 |
{%- endfor %} |
| 93 |
</head> |
| 94 |
<body> |
| 95 |
{{ page.content }} |
| 96 |
|
| 97 |
{%- for script in config.extra_javascript %} |
| 98 |
{{ script | script_tag }} |
| 99 |
{%- endfor %} |
| 100 |
</body> |
| 101 |
</html> |
| 102 |
``` |
| 103 |
|
| 104 |
The body content from each page specified in `mkdocs.yml` is inserted using the |
| 105 |
`{{ page.content }}` tag. Style-sheets and scripts can be brought into this |
| 106 |
theme as with a normal HTML file. Navbars and tables of contents can also be |
| 107 |
generated and included automatically, through the `nav` and `toc` objects, |
| 108 |
respectively. If you wish to write your own theme, it is recommended to start |
| 109 |
with one of the [built-in themes] and modify it accordingly. |
| 110 |
|
| 111 |
NOTE: |
| 112 |
As MkDocs uses [Jinja] as its template engine, you have access to all the |
| 113 |
power of Jinja, including [template inheritance]. You may notice that the |
| 114 |
themes included with MkDocs make extensive use of template inheritance and |
| 115 |
blocks, allowing users to easily override small bits and pieces of the |
| 116 |
templates from the theme [custom_dir]. Therefore, the built-in themes are |
| 117 |
implemented in a `base.html` file, which `main.html` extends. Although not |
| 118 |
required, third party template authors are encouraged to follow a similar |
| 119 |
pattern and may want to define the same [blocks] as are used in the built-in |
| 120 |
themes for consistency. |
| 121 |
|
| 122 |
[Jinja]: https://jinja.palletsprojects.com/ |
| 123 |
[template inheritance]: https://jinja.palletsprojects.com/en/latest/templates/#template-inheritance |
| 124 |
[blocks]: ../user-guide/customizing-your-theme.md#overriding-template-blocks |
| 125 |
|
| 126 |
### Picking up CSS and JavaScript from the config |
| 127 |
|
| 128 |
MkDocs defines the top-level [extra_css](../user-guide/configuration.md#extra_css) and [extra_javascript](../user-guide/configuration.md#extra_javascript) configs. These are lists of files. |
| 129 |
|
| 130 |
The theme must include the HTML that links the items from these configs, otherwise the configs will be non-functional. You can see the recommended way to render both of them in the [base example above](#basic-theme). |
| 131 |
|
| 132 |
> NEW: **Changed in version 1.5:** |
| 133 |
> |
| 134 |
> The items of the `config.extra_javascript` list used to be simple strings but now became objects that have these fields: `path`, `type`, `async`, `defer`. |
| 135 |
> |
| 136 |
> In that version, MkDocs also gained the [`script_tag` filter](#script_tag). |
| 137 |
> |
| 138 |
> >? EXAMPLE: **Obsolete style:** |
| 139 |
> > |
| 140 |
> > ```django |
| 141 |
> > {%- for path in extra_javascript %} |
| 142 |
> > <script src="{{ path }}"></script> |
| 143 |
> > {%- endfor %} |
| 144 |
> > ``` |
| 145 |
> > |
| 146 |
> > This old-style example even uses the obsolete top-level `extra_javascript` list. Please always use `config.extra_javascript` instead. |
| 147 |
> > |
| 148 |
> > So, a slightly more modern approach is the following, but it is still obsolete because it ignores the extra attributes of the script: |
| 149 |
> > |
| 150 |
> > ```django |
| 151 |
> > {%- for path in config.extra_javascript %} |
| 152 |
> > <script src="{{ path | url }}"></script> |
| 153 |
> > {%- endfor %} |
| 154 |
> > ``` |
| 155 |
> <!-- --> |
| 156 |
> >? EXAMPLE: **New style:** |
| 157 |
> > |
| 158 |
> > ```django |
| 159 |
> > {%- for script in config.extra_javascript %} |
| 160 |
> > {{ script | script_tag }} |
| 161 |
> > {%- endfor %} |
| 162 |
> > ``` |
| 163 |
> |
| 164 |
> If you wish to be able to pick up the new customizations while keeping your theme compatible with older versions of MkDocs, use this snippet: |
| 165 |
> |
| 166 |
> >! EXAMPLE: **Backwards-compatible style:** |
| 167 |
> > |
| 168 |
> > ```django |
| 169 |
> > {%- for script in config.extra_javascript %} |
| 170 |
> > {%- if script.path %} {# Detected MkDocs 1.5+ which has `script.path` and `script_tag` #} |
| 171 |
> > {{ script | script_tag }} |
| 172 |
> > {%- else %} {# Fallback - examine the file name directly #} |
| 173 |
> > <script src="{{ script | url }}"{% if script.endswith(".mjs") %} type="module"{% endif %}></script> |
| 174 |
> > {%- endif %} |
| 175 |
> > {%- endfor %} |
| 176 |
> > ``` |
| 177 |
|
| 178 |
## Theme Files |
| 179 |
|
| 180 |
There are various files which a theme treats special in some way. Any other |
| 181 |
files are simply copied from the theme directory to the same path in the |
| 182 |
`site_dir` when the site it built. For example image and CSS files have no |
| 183 |
special significance and are copied as-is. Note, however, that if the user |
| 184 |
provides a file with the same path in their `docs_dir`, then the user's file |
| 185 |
will replace the theme file. |
| 186 |
|
| 187 |
### Template Files |
| 188 |
|
| 189 |
Any files with the `.html` extension are considered to be template files and are |
| 190 |
not copied from the theme directory or any subdirectories. Also, any files |
| 191 |
listed in [static_templates] are treated as templates regardless of their file |
| 192 |
extension. |
| 193 |
|
| 194 |
[static_templates]: #static_templates |
| 195 |
|
| 196 |
### Theme Meta Files |
| 197 |
|
| 198 |
The various files required for packaging a theme are also ignored. Specifically, |
| 199 |
the `mkdocs_theme.yml` configuration file and any Python files. |
| 200 |
|
| 201 |
### Dot Files |
| 202 |
|
| 203 |
Theme authors can explicitly force MkDocs to ignore files by starting a file or |
| 204 |
directory name with a dot. Any of the following files would be ignored: |
| 205 |
|
| 206 |
```text |
| 207 |
.ignored.txt |
| 208 |
.ignored/file.txt |
| 209 |
foo/.ignored.txt |
| 210 |
foo/.ignored/file.txt |
| 211 |
``` |
| 212 |
|
| 213 |
### Documentation Files |
| 214 |
|
| 215 |
All documentation files are ignored. Specifically, any Markdown files (using any |
| 216 |
of the file extensions supported by MKDocs). Additionally, any README files |
| 217 |
which may exist in the theme directories are ignored. |
| 218 |
|
| 219 |
## Template Variables |
| 220 |
|
| 221 |
Each template in a theme is built with a template context. These are the |
| 222 |
variables that are available to themes. The context varies depending on the |
| 223 |
template that is being built. At the moment templates are either built with |
| 224 |
the global context or with a page specific context. The global context is used |
| 225 |
for HTML pages that don't represent an individual Markdown document, for |
| 226 |
example a 404.html page or search.html. |
| 227 |
|
| 228 |
### Global Context |
| 229 |
|
| 230 |
The following variables are available globally on any template. |
| 231 |
|
| 232 |
#### config |
| 233 |
|
| 234 |
The `config` variable is an instance of MkDocs' config object generated from the |
| 235 |
`mkdocs.yml` config file. While you can use any config option, some commonly |
| 236 |
used options include: |
| 237 |
|
| 238 |
* [config.site_name](../user-guide/configuration.md#site_name) |
| 239 |
* [config.site_url](../user-guide/configuration.md#site_url) |
| 240 |
* [config.site_author](../user-guide/configuration.md#site_author) |
| 241 |
* [config.site_description](../user-guide/configuration.md#site_description) |
| 242 |
* [config.theme.locale](../user-guide/configuration.md#locale) (See also [Theme Configuration](#locale) below) |
| 243 |
* [config.extra_javascript](../user-guide/configuration.md#extra_javascript) |
| 244 |
* [config.extra_css](../user-guide/configuration.md#extra_css) |
| 245 |
* [config.repo_url](../user-guide/configuration.md#repo_url) |
| 246 |
* [config.repo_name](../user-guide/configuration.md#repo_name) |
| 247 |
* [config.copyright](../user-guide/configuration.md#copyright) |
| 248 |
|
| 249 |
#### nav |
| 250 |
|
| 251 |
The `nav` variable is used to create the navigation for the documentation. The |
| 252 |
`nav` object is an iterable of [navigation objects](#navigation-objects) as |
| 253 |
defined by the [nav] configuration setting. |
| 254 |
|
| 255 |
[nav]: ../user-guide/configuration.md#nav |
| 256 |
|
| 257 |
::: mkdocs.structure.nav.Navigation |
| 258 |
options: |
| 259 |
show_root_heading: false |
| 260 |
show_root_toc_entry: true |
| 261 |
members: [] |
| 262 |
heading_level: 4 |
| 263 |
|
| 264 |
In addition to the iterable of [navigation objects](#navigation-objects), the |
| 265 |
`nav` object contains the following attributes: |
| 266 |
|
| 267 |
::: mkdocs.structure.nav.Navigation.homepage |
| 268 |
options: |
| 269 |
show_root_full_path: false |
| 270 |
heading_level: 5 |
| 271 |
|
| 272 |
::: mkdocs.structure.nav.Navigation.pages |
| 273 |
options: |
| 274 |
show_root_full_path: false |
| 275 |
heading_level: 5 |
| 276 |
|
| 277 |
This list is not necessarily a complete list of all site pages as it does not contain |
| 278 |
pages which are not included in the navigation. This list does match the list |
| 279 |
and order of pages used for all "next page" and "previous page" links. For a |
| 280 |
list of all pages, use the [pages](#pages) template variable. |
| 281 |
|
| 282 |
##### Nav Example |
| 283 |
|
| 284 |
Following is a basic usage example which outputs the first and second level |
| 285 |
navigation as a nested list. |
| 286 |
|
| 287 |
```django |
| 288 |
{% if nav|length > 1 %} |
| 289 |
<ul> |
| 290 |
{% for nav_item in nav %} |
| 291 |
{% if nav_item.children %} |
| 292 |
<li>{{ nav_item.title }} |
| 293 |
<ul> |
| 294 |
{% for nav_item in nav_item.children %} |
| 295 |
<li class="{% if nav_item.active %}current{% endif %}"> |
| 296 |
<a href="{{ nav_item.url|url }}">{{ nav_item.title }}</a> |
| 297 |
</li> |
| 298 |
{% endfor %} |
| 299 |
</ul> |
| 300 |
</li> |
| 301 |
{% else %} |
| 302 |
<li class="{% if nav_item.active %}current{% endif %}"> |
| 303 |
<a href="{{ nav_item.url|url }}">{{ nav_item.title }}</a> |
| 304 |
</li> |
| 305 |
{% endif %} |
| 306 |
{% endfor %} |
| 307 |
</ul> |
| 308 |
{% endif %} |
| 309 |
``` |
| 310 |
|
| 311 |
#### base_url |
| 312 |
|
| 313 |
The `base_url` provides a relative path to the root of the MkDocs project. While |
| 314 |
this can be used directly by prepending it to a local relative URL, it is best |
| 315 |
to use the [url](#url) template filter, which is smarter about how it applies |
| 316 |
`base_url`. |
| 317 |
|
| 318 |
#### mkdocs_version |
| 319 |
|
| 320 |
Contains the current MkDocs version. |
| 321 |
|
| 322 |
#### build_date_utc |
| 323 |
|
| 324 |
A Python datetime object that represents the date and time the documentation |
| 325 |
was built in UTC. This is useful for showing how recently the documentation |
| 326 |
was updated. |
| 327 |
|
| 328 |
#### pages |
| 329 |
|
| 330 |
A flat list of `File` objects for *all* pages in the project. This list can |
| 331 |
contain pages not included in the global [navigation](#nav) and may not match |
| 332 |
the order of pages within that navigation. The [page](#page) object for each |
| 333 |
`File` can be accessed from `file.page`. |
| 334 |
|
| 335 |
#### page |
| 336 |
|
| 337 |
In templates which are not rendered from a Markdown source file, the `page` |
| 338 |
variable is `None`. In templates which are rendered from a Markdown source file, |
| 339 |
the `page` variable contains a `page` object. The same `page` objects are used |
| 340 |
as `page` [navigation objects](#navigation-objects) in the global |
| 341 |
[navigation](#nav) and in the [pages](#pages) template variable. |
| 342 |
|
| 343 |
::: mkdocs.structure.pages.Page |
| 344 |
options: |
| 345 |
show_root_heading: false |
| 346 |
show_root_toc_entry: true |
| 347 |
members: [] |
| 348 |
heading_level: 4 |
| 349 |
|
| 350 |
All `page` objects contain the following attributes: |
| 351 |
|
| 352 |
::: mkdocs.structure.pages.Page.title |
| 353 |
options: |
| 354 |
show_root_full_path: false |
| 355 |
heading_level: 5 |
| 356 |
|
| 357 |
::: mkdocs.structure.pages.Page.content |
| 358 |
options: |
| 359 |
show_root_full_path: false |
| 360 |
heading_level: 5 |
| 361 |
|
| 362 |
::: mkdocs.structure.pages.Page.toc |
| 363 |
options: |
| 364 |
show_root_full_path: false |
| 365 |
heading_level: 5 |
| 366 |
|
| 367 |
The following example would display the top two levels of the Table of Contents |
| 368 |
for a page. |
| 369 |
|
| 370 |
```django |
| 371 |
<ul> |
| 372 |
{% for toc_item in page.toc %} |
| 373 |
<li><a href="{{ toc_item.url }}">{{ toc_item.title }}</a></li> |
| 374 |
{% for toc_item in toc_item.children %} |
| 375 |
<li><a href="{{ toc_item.url }}">{{ toc_item.title }}</a></li> |
| 376 |
{% endfor %} |
| 377 |
{% endfor %} |
| 378 |
</ul> |
| 379 |
``` |
| 380 |
|
| 381 |
::: mkdocs.structure.pages.Page.meta |
| 382 |
options: |
| 383 |
show_root_full_path: false |
| 384 |
heading_level: 5 |
| 385 |
|
| 386 |
In this example we define a `source` property above the page title: |
| 387 |
|
| 388 |
```text |
| 389 |
source: generics.py |
| 390 |
mixins.py |
| 391 |
|
| 392 |
# Page title |
| 393 |
|
| 394 |
Content... |
| 395 |
``` |
| 396 |
|
| 397 |
A template can access this metadata for the page with the `meta.source` |
| 398 |
variable. This could then be used to link to source files related to the |
| 399 |
documentation page. |
| 400 |
|
| 401 |
```django |
| 402 |
{% for filename in page.meta.source %} |
| 403 |
<a class="github" href="https://github.com/.../{{ filename }}"> |
| 404 |
<span class="label label-info">{{ filename }}</span> |
| 405 |
</a> |
| 406 |
{% endfor %} |
| 407 |
``` |
| 408 |
|
| 409 |
::: mkdocs.structure.pages.Page.url |
| 410 |
options: |
| 411 |
show_root_full_path: false |
| 412 |
heading_level: 5 |
| 413 |
|
| 414 |
It is expected that this be used with the [url](#url) filter to ensure the URL is relative to the current |
| 415 |
page. |
| 416 |
|
| 417 |
```django |
| 418 |
<a href="{{ page.url|url }}">{{ page.title }}</a> |
| 419 |
``` |
| 420 |
|
| 421 |
::: mkdocs.structure.pages.Page.file |
| 422 |
options: |
| 423 |
show_root_full_path: false |
| 424 |
heading_level: 5 |
| 425 |
|
| 426 |
::: mkdocs.structure.pages.Page.abs_url |
| 427 |
options: |
| 428 |
show_root_full_path: false |
| 429 |
heading_level: 5 |
| 430 |
|
| 431 |
For example, if `site_url: https://example.com/`, then the value of |
| 432 |
`page.abs_url` for the page `foo.md` would be `/foo/`. However, if |
| 433 |
`site_url: https://example.com/bar/`, then the value of `page.abs_url` for the |
| 434 |
page `foo.md` would be `/bar/foo/`. |
| 435 |
|
| 436 |
::: mkdocs.structure.pages.Page.canonical_url |
| 437 |
options: |
| 438 |
show_root_full_path: false |
| 439 |
heading_level: 5 |
| 440 |
|
| 441 |
::: mkdocs.structure.pages.Page.edit_url |
| 442 |
options: |
| 443 |
show_root_full_path: false |
| 444 |
heading_level: 5 |
| 445 |
|
| 446 |
::: mkdocs.structure.pages.Page.is_homepage |
| 447 |
options: |
| 448 |
show_root_full_path: false |
| 449 |
heading_level: 5 |
| 450 |
|
| 451 |
This can be used in conjunction with other attributes of the `page` |
| 452 |
object to alter the behavior. For example, to display a different title |
| 453 |
on the homepage: |
| 454 |
|
| 455 |
```django |
| 456 |
{% if not page.is_homepage %}{{ page.title }} - {% endif %}{{ site_name }} |
| 457 |
``` |
| 458 |
|
| 459 |
::: mkdocs.structure.pages.Page.previous_page |
| 460 |
options: |
| 461 |
show_root_full_path: false |
| 462 |
heading_level: 5 |
| 463 |
|
| 464 |
::: mkdocs.structure.pages.Page.next_page |
| 465 |
options: |
| 466 |
show_root_full_path: false |
| 467 |
heading_level: 5 |
| 468 |
|
| 469 |
::: mkdocs.structure.StructureItem.parent |
| 470 |
options: |
| 471 |
show_root_full_path: false |
| 472 |
heading_level: 5 |
| 473 |
|
| 474 |
::: mkdocs.structure.pages.Page.children |
| 475 |
options: |
| 476 |
show_root_full_path: false |
| 477 |
heading_level: 5 |
| 478 |
|
| 479 |
::: mkdocs.structure.pages.Page.active |
| 480 |
options: |
| 481 |
show_root_full_path: false |
| 482 |
heading_level: 5 |
| 483 |
|
| 484 |
::: mkdocs.structure.pages.Page.is_section |
| 485 |
options: |
| 486 |
show_root_full_path: false |
| 487 |
heading_level: 5 |
| 488 |
|
| 489 |
::: mkdocs.structure.pages.Page.is_page |
| 490 |
options: |
| 491 |
show_root_full_path: false |
| 492 |
heading_level: 5 |
| 493 |
|
| 494 |
::: mkdocs.structure.pages.Page.is_link |
| 495 |
options: |
| 496 |
show_root_full_path: false |
| 497 |
heading_level: 5 |
| 498 |
|
| 499 |
#### AnchorLink |
| 500 |
|
| 501 |
::: mkdocs.structure.toc.AnchorLink |
| 502 |
options: |
| 503 |
show_root_heading: false |
| 504 |
show_root_toc_entry: true |
| 505 |
heading_level: 5 |
| 506 |
|
| 507 |
### Navigation Objects |
| 508 |
|
| 509 |
Navigation objects contained in the [nav](#nav) template variable may be one of |
| 510 |
[section](#section) objects, [page](#page) objects, and [link](#link) objects. |
| 511 |
While section objects may contain nested navigation objects, pages and links do |
| 512 |
not. |
| 513 |
|
| 514 |
Page objects are the full page object as used for the current [page](#page) with |
| 515 |
all of the same attributes available. Section and Link objects contain a subset |
| 516 |
of those attributes as defined below: |
| 517 |
|
| 518 |
#### Section |
| 519 |
|
| 520 |
A `section` navigation object defines a named section in the navigation and |
| 521 |
contains a list of child navigation objects. Note that sections do not contain |
| 522 |
URLs and are not links of any kind. However, by default, MkDocs sorts index |
| 523 |
pages to the top and the first child might be used as the URL for a section if a |
| 524 |
theme chooses to do so. |
| 525 |
|
| 526 |
::: mkdocs.structure.nav.Section |
| 527 |
options: |
| 528 |
show_root_heading: false |
| 529 |
show_root_toc_entry: true |
| 530 |
members: [] |
| 531 |
heading_level: 4 |
| 532 |
|
| 533 |
The following attributes are available on `section` objects: |
| 534 |
|
| 535 |
::: mkdocs.structure.nav.Section.title |
| 536 |
options: |
| 537 |
show_root_full_path: false |
| 538 |
heading_level: 5 |
| 539 |
|
| 540 |
::: mkdocs.structure.StructureItem.parent |
| 541 |
options: |
| 542 |
show_root_full_path: false |
| 543 |
heading_level: 5 |
| 544 |
|
| 545 |
::: mkdocs.structure.nav.Section.children |
| 546 |
options: |
| 547 |
show_root_full_path: false |
| 548 |
heading_level: 5 |
| 549 |
|
| 550 |
::: mkdocs.structure.nav.Section.active |
| 551 |
options: |
| 552 |
show_root_full_path: false |
| 553 |
heading_level: 5 |
| 554 |
|
| 555 |
::: mkdocs.structure.nav.Section.is_section |
| 556 |
options: |
| 557 |
show_root_full_path: false |
| 558 |
heading_level: 5 |
| 559 |
|
| 560 |
::: mkdocs.structure.nav.Section.is_page |
| 561 |
options: |
| 562 |
show_root_full_path: false |
| 563 |
heading_level: 5 |
| 564 |
|
| 565 |
::: mkdocs.structure.nav.Section.is_link |
| 566 |
options: |
| 567 |
show_root_full_path: false |
| 568 |
heading_level: 5 |
| 569 |
|
| 570 |
#### Link |
| 571 |
|
| 572 |
A `link` navigation object contains a link which does not point to an internal |
| 573 |
MkDocs page. |
| 574 |
|
| 575 |
::: mkdocs.structure.nav.Link |
| 576 |
options: |
| 577 |
show_root_heading: false |
| 578 |
show_root_toc_entry: true |
| 579 |
members: [] |
| 580 |
heading_level: 4 |
| 581 |
|
| 582 |
The following attributes are available on `link` objects: |
| 583 |
|
| 584 |
::: mkdocs.structure.nav.Link.title |
| 585 |
options: |
| 586 |
show_root_full_path: false |
| 587 |
heading_level: 5 |
| 588 |
|
| 589 |
::: mkdocs.structure.nav.Link.url |
| 590 |
options: |
| 591 |
show_root_full_path: false |
| 592 |
heading_level: 5 |
| 593 |
|
| 594 |
::: mkdocs.structure.StructureItem.parent |
| 595 |
options: |
| 596 |
show_root_full_path: false |
| 597 |
heading_level: 5 |
| 598 |
|
| 599 |
::: mkdocs.structure.nav.Link.children |
| 600 |
options: |
| 601 |
show_root_full_path: false |
| 602 |
heading_level: 5 |
| 603 |
|
| 604 |
::: mkdocs.structure.nav.Link.active |
| 605 |
options: |
| 606 |
show_root_full_path: false |
| 607 |
heading_level: 5 |
| 608 |
|
| 609 |
::: mkdocs.structure.nav.Link.is_section |
| 610 |
options: |
| 611 |
show_root_full_path: false |
| 612 |
heading_level: 5 |
| 613 |
|
| 614 |
::: mkdocs.structure.nav.Link.is_page |
| 615 |
options: |
| 616 |
show_root_full_path: false |
| 617 |
heading_level: 5 |
| 618 |
|
| 619 |
::: mkdocs.structure.nav.Link.is_link |
| 620 |
options: |
| 621 |
show_root_full_path: false |
| 622 |
heading_level: 5 |
| 623 |
|
| 624 |
### Extra Context |
| 625 |
|
| 626 |
Additional variables can be passed to the template with the |
| 627 |
[`extra`](../user-guide/configuration.md#extra) configuration option. This is a |
| 628 |
set of key value pairs that can make custom templates far more flexible. |
| 629 |
|
| 630 |
For example, this could be used to include the project version of all pages |
| 631 |
and a list of links related to the project. This can be achieved with the |
| 632 |
following `extra` configuration: |
| 633 |
|
| 634 |
```yaml |
| 635 |
extra: |
| 636 |
version: 0.13.0 |
| 637 |
links: |
| 638 |
- https://github.com/mkdocs |
| 639 |
- https://docs.readthedocs.org/en/latest/builds.html#mkdocs |
| 640 |
- https://www.mkdocs.org/ |
| 641 |
``` |
| 642 |
|
| 643 |
And then displayed with this HTML in the custom theme. |
| 644 |
|
| 645 |
```django |
| 646 |
{{ config.extra.version }} |
| 647 |
|
| 648 |
{% if config.extra.links %} |
| 649 |
<ul> |
| 650 |
{% for link in config.extra.links %} |
| 651 |
<li>{{ link }}</li> |
| 652 |
{% endfor %} |
| 653 |
</ul> |
| 654 |
{% endif %} |
| 655 |
``` |
| 656 |
|
| 657 |
## Template Filters |
| 658 |
|
| 659 |
In addition to [Jinja's default filters], the following custom filters are |
| 660 |
available to use in MkDocs templates: |
| 661 |
|
| 662 |
### url |
| 663 |
|
| 664 |
Normalizes a URL. Absolute URLs are passed through unaltered. If the URL is |
| 665 |
relative and the template context includes a page object, then the URL is |
| 666 |
returned relative to the page object. Otherwise, the URL is returned with |
| 667 |
[base_url](#base_url) prepended. |
| 668 |
|
| 669 |
```django |
| 670 |
<a href="{{ page.url|url }}">{{ page.title }}</a> |
| 671 |
``` |
| 672 |
|
| 673 |
### tojson |
| 674 |
|
| 675 |
Safely convert a Python object to a value in a JavaScript script. |
| 676 |
|
| 677 |
```django |
| 678 |
<script> |
| 679 |
var mkdocs_page_name = {{ page.title|tojson|safe }}; |
| 680 |
</script> |
| 681 |
``` |
| 682 |
|
| 683 |
### script_tag |
| 684 |
|
| 685 |
NEW: **New in version 1.5.** |
| 686 |
|
| 687 |
Convert an item from `extra_javascript` to a `<script>` tag that takes into account all [customizations of this config](../user-guide/configuration.md#extra_javascript) and has the equivalent of [`|url`](#url) behavior built-in. |
| 688 |
|
| 689 |
See how to use it in the [base example above](#basic-theme) |
| 690 |
|
| 691 |
## Search and themes |
| 692 |
|
| 693 |
As of MkDocs version *0.17* client side search support has been added to MkDocs |
| 694 |
via the `search` plugin. A theme needs to provide a few things for the plugin to |
| 695 |
work with the theme. |
| 696 |
|
| 697 |
While the `search` plugin is activated by default, users can disable the plugin |
| 698 |
and themes should account for this. It is recommended that theme templates wrap |
| 699 |
search specific markup with a check for the plugin: |
| 700 |
|
| 701 |
```django |
| 702 |
{% if 'search' in config.plugins %} |
| 703 |
search stuff here... |
| 704 |
{% endif %} |
| 705 |
``` |
| 706 |
|
| 707 |
At its most basic functionality, the search plugin will simply provide an index |
| 708 |
file which is no more than a JSON file containing the content of all pages. |
| 709 |
The theme would need to implement its own search functionality client-side. |
| 710 |
However, with a few settings and the necessary templates, the plugin can provide |
| 711 |
a complete functioning client-side search tool based on [lunr.js]. |
| 712 |
|
| 713 |
The following HTML needs to be added to the theme so that the provided |
| 714 |
JavaScript is able to properly load the search scripts and make relative links |
| 715 |
to the search results from the current page. |
| 716 |
|
| 717 |
```django |
| 718 |
<script>var base_url = {{ base_url|tojson }};</script> |
| 719 |
``` |
| 720 |
|
| 721 |
With properly configured settings, the following HTML in a template will add a |
| 722 |
full search implementation to your theme. |
| 723 |
|
| 724 |
```django |
| 725 |
<h1 id="search">Search Results</h1> |
| 726 |
|
| 727 |
<form action="search.html"> |
| 728 |
<input name="q" id="mkdocs-search-query" type="text" > |
| 729 |
</form> |
| 730 |
|
| 731 |
<div id="mkdocs-search-results"> |
| 732 |
Sorry, page not found. |
| 733 |
</div> |
| 734 |
``` |
| 735 |
|
| 736 |
The JavaScript in the plugin works by looking for the specific ID's used in the |
| 737 |
above HTML. The form input for the user to type the search query must be |
| 738 |
identified with `id="mkdocs-search-query"` and the div where the results will be |
| 739 |
placed must be identified with `id="mkdocs-search-results"`. |
| 740 |
|
| 741 |
The plugin supports the following options being set in the [theme's |
| 742 |
configuration file], `mkdocs_theme.yml`: |
| 743 |
|
| 744 |
### include_search_page |
| 745 |
|
| 746 |
Determines whether the search plugin expects the theme to provide a dedicated |
| 747 |
search page via a template located at `search/search.html`. |
| 748 |
|
| 749 |
When `include_search_page` is set to `true`, the search template will be built |
| 750 |
and available at `search/search.html`. This method is used by the `readthedocs` |
| 751 |
theme. |
| 752 |
|
| 753 |
When `include_search_page` is set to `false` or not defined, it is expected that |
| 754 |
the theme provide some other mechanisms for displaying search results. For |
| 755 |
example, the `mkdocs` theme displays results on any page via a modal. |
| 756 |
|
| 757 |
### search_index_only |
| 758 |
|
| 759 |
Determines whether the search plugin should only generate a search index or a |
| 760 |
complete search solution. |
| 761 |
|
| 762 |
When `search_index_only` is set to `false`, then the search plugin modifies the |
| 763 |
Jinja environment by adding its own `templates` directory (with a lower |
| 764 |
precedence than the theme) and adds its scripts to the `extra_javascript` config |
| 765 |
setting. |
| 766 |
|
| 767 |
When `search_index_only` is set to `true` or not defined, the search plugin |
| 768 |
makes no modifications to the Jinja environment. A complete solution using the |
| 769 |
provided index file is the responsibility of the theme. |
| 770 |
|
| 771 |
The search index is written to a JSON file at `search/search_index.json` in the |
| 772 |
[site_dir]. The JSON object contained within the file may contain up to three |
| 773 |
objects. |
| 774 |
|
| 775 |
```json |
| 776 |
{ |
| 777 |
config: {...}, |
| 778 |
docs: [...], |
| 779 |
index: {...} |
| 780 |
} |
| 781 |
``` |
| 782 |
|
| 783 |
If present, the `config` object contains the key/value pairs of config options |
| 784 |
defined for the plugin in the user's `mkdocs.yml` config file under |
| 785 |
`plugings.search`. The `config` object was new in MkDocs version *1.0*. |
| 786 |
|
| 787 |
The `docs` object contains a list of document objects. Each document object is |
| 788 |
made up of a `location` (URL), a `title`, and `text` which can be used to create |
| 789 |
a search index and/or display search results. |
| 790 |
|
| 791 |
If present, the `index` object contains a pre-built index which offers |
| 792 |
performance improvements for larger sites. Note that the pre-built index is only |
| 793 |
created if the user explicitly enables the [prebuild_index] config option. |
| 794 |
Themes should expect the index to not be present, but can choose to use the |
| 795 |
index when it is available. The `index` object was new in MkDocs version *1.0*. |
| 796 |
|
| 797 |
[Jinja2 template]: https://jinja.palletsprojects.com/ |
| 798 |
[built-in themes]: https://github.com/mkdocs/mkdocs/tree/master/mkdocs/themes |
| 799 |
[theme's configuration file]: #theme-configuration |
| 800 |
[lunr.js]: https://lunrjs.com/ |
| 801 |
[site_dir]: ../user-guide/configuration.md#site_dir |
| 802 |
[prebuild_index]: ../user-guide/configuration.md#prebuild_index |
| 803 |
[Jinja's default filters]: https://jinja.palletsprojects.com/en/latest/templates/#builtin-filters |
| 804 |
|
| 805 |
## Packaging Themes |
| 806 |
|
| 807 |
MkDocs makes use of [Python packaging] to distribute themes. This comes with a |
| 808 |
few requirements. |
| 809 |
|
| 810 |
To see an example of a package containing one theme, see the [MkDocs Bootstrap |
| 811 |
theme] and to see a package that contains many themes, see the [MkDocs |
| 812 |
Bootswatch theme]. |
| 813 |
|
| 814 |
NOTE: |
| 815 |
It is not strictly necessary to package a theme, as the entire theme |
| 816 |
can be contained in the `custom_dir`. If you have created a "one-off theme," |
| 817 |
that should be sufficient. However, if you intend to distribute your theme |
| 818 |
for others to use, packaging the theme has some advantages. By packaging |
| 819 |
your theme, your users can more easily install it, they can rely on a default |
| 820 |
[configuration] being defined, and they can then take advantage of the |
| 821 |
[custom_dir] to make tweaks to your theme to better suit their needs. |
| 822 |
|
| 823 |
[Python packaging]: https://packaging.python.org/en/latest/ |
| 824 |
[MkDocs Bootstrap theme]: https://mkdocs.github.io/mkdocs-bootstrap/ |
| 825 |
[MkDocs Bootswatch theme]: https://mkdocs.github.io/mkdocs-bootswatch/ |
| 826 |
|
| 827 |
### Package Layout |
| 828 |
|
| 829 |
The following layout is recommended for themes. Two files at the top level |
| 830 |
directory called `MANIFEST.in` and `setup.py` beside the theme directory which |
| 831 |
contains an empty `__init__.py` file, a theme configuration file |
| 832 |
(`mkdocs_theme.yml`), and your template and media files. |
| 833 |
|
| 834 |
```text |
| 835 |
. |
| 836 |
|-- MANIFEST.in |
| 837 |
|-- theme_name |
| 838 |
| |-- __init__.py |
| 839 |
| |-- mkdocs_theme.yml |
| 840 |
| |-- main.html |
| 841 |
| |-- styles.css |
| 842 |
`-- setup.py |
| 843 |
``` |
| 844 |
|
| 845 |
The `MANIFEST.in` file should contain the following contents but with |
| 846 |
theme_name updated and any extra file extensions added to the include. |
| 847 |
|
| 848 |
```text |
| 849 |
recursive-include theme_name *.ico *.js *.css *.png *.html *.eot *.svg *.ttf *.woff |
| 850 |
recursive-exclude * __pycache__ |
| 851 |
recursive-exclude * *.py[co] |
| 852 |
``` |
| 853 |
|
| 854 |
The `setup.py` should include the following text with the modifications |
| 855 |
described below. |
| 856 |
|
| 857 |
```python |
| 858 |
from setuptools import setup, find_packages |
| 859 |
|
| 860 |
VERSION = '0.0.1' |
| 861 |
|
| 862 |
setup( |
| 863 |
name="mkdocs-themename", |
| 864 |
version=VERSION, |
| 865 |
url='', |
| 866 |
license='', |
| 867 |
description='', |
| 868 |
author='', |
| 869 |
author_email='', |
| 870 |
packages=find_packages(), |
| 871 |
include_package_data=True, |
| 872 |
entry_points={ |
| 873 |
'mkdocs.themes': [ |
| 874 |
'themename = theme_name', |
| 875 |
] |
| 876 |
}, |
| 877 |
zip_safe=False |
| 878 |
) |
| 879 |
``` |
| 880 |
|
| 881 |
Fill in the URL, license, description, author and author email address. |
| 882 |
|
| 883 |
The name should follow the convention `mkdocs-themename` (like |
| 884 |
`mkdocs-bootstrap` and `mkdocs-bootswatch`), starting with MkDocs, using |
| 885 |
hyphens to separate words and including the name of your theme. |
| 886 |
|
| 887 |
Most of the rest of the file can be left unedited. The last section we need to |
| 888 |
change is the entry_points. This is how MkDocs finds the theme(s) you are |
| 889 |
including in the package. The name on the left is the one that users will use |
| 890 |
in their mkdocs.yml and the one on the right is the directory containing your |
| 891 |
theme files. |
| 892 |
|
| 893 |
The directory you created at the start of this section with the main.html file |
| 894 |
should contain all of the other theme files. The minimum requirement is that |
| 895 |
it includes a `main.html` for the theme. It **must** also include a |
| 896 |
`__init__.py` file which should be empty, this file tells Python that the |
| 897 |
directory is a package. |
| 898 |
|
| 899 |
### Theme Configuration |
| 900 |
|
| 901 |
A packaged theme is required to include a configuration file named |
| 902 |
`mkdocs_theme.yml` which is placed in the root of your template files. The file |
| 903 |
should contain default configuration options for the theme. However, if the |
| 904 |
theme offers no configuration options, the file is still required and can be |
| 905 |
left blank. A theme which is not packaged does not need a `mkdocs_theme.yml` |
| 906 |
file as that file is not loaded from `theme.custom_dir`. |
| 907 |
|
| 908 |
The theme author is free to define any arbitrary options deemed necessary and |
| 909 |
those options will be made available in the templates to control behavior. |
| 910 |
For example, a theme might want to make a sidebar optional and include the |
| 911 |
following in the `mkdocs_theme.yml` file: |
| 912 |
|
| 913 |
```yaml |
| 914 |
show_sidebar: true |
| 915 |
``` |
| 916 |
|
| 917 |
Then in a template, that config option could be referenced: |
| 918 |
|
| 919 |
```django |
| 920 |
{% if config.theme.show_sidebar %} |
| 921 |
<div id="sidebar">...</div> |
| 922 |
{% endif %} |
| 923 |
``` |
| 924 |
|
| 925 |
And the user could override the default in their project's `mkdocs.yml` config |
| 926 |
file: |
| 927 |
|
| 928 |
```yaml |
| 929 |
theme: |
| 930 |
name: themename |
| 931 |
show_sidebar: false |
| 932 |
``` |
| 933 |
|
| 934 |
In addition to arbitrary options defined by the theme, MkDocs defines a few |
| 935 |
special options which alters its behavior: |
| 936 |
|
| 937 |
> BLOCK: |
| 938 |
> |
| 939 |
> #### locale |
| 940 |
> |
| 941 |
> This option mirrors the [theme] config option of the same name. If this |
| 942 |
> value is not defined in the `mkdocs_theme.yml` file and the user does not |
| 943 |
> set it in `mkdocs.yml` then it will default to `en` (English). The value |
| 944 |
> is expected to match the language used in the text provided by the theme |
| 945 |
> (such a "next" and "previous" links) and should be used as the value of |
| 946 |
> the `<html>` tag's `lang` attribute. See [Supporting theme localization/ |
| 947 |
> translation](#supporting-theme-localizationtranslation) for more |
| 948 |
> information. |
| 949 |
> |
| 950 |
> Note that during configuration validation, the provided string is converted |
| 951 |
> to a `Locale` object. The object contains `Locale.language` and |
| 952 |
> `Locale.territory` attributes and will resolve as a string from within a |
| 953 |
> template. Therefore, the following will work fine: |
| 954 |
> |
| 955 |
> ```html |
| 956 |
> <html lang="{ config.theme.locale }"> |
| 957 |
> ``` |
| 958 |
> |
| 959 |
> If the locale was set to `fr_CA` (Canadian French), then the above template |
| 960 |
> would render as: |
| 961 |
> |
| 962 |
> ```html |
| 963 |
> <html lang="fr_CA"> |
| 964 |
> ``` |
| 965 |
> |
| 966 |
> If you did not want the territory attribute to be included, then reference |
| 967 |
> the `language` attribute directly: |
| 968 |
> |
| 969 |
> ```html |
| 970 |
> <html lang="{ config.theme.locale.language }"> |
| 971 |
> ``` |
| 972 |
> |
| 973 |
> That would render as: |
| 974 |
> |
| 975 |
> ```html |
| 976 |
> <html lang="fr"> |
| 977 |
> ``` |
| 978 |
> |
| 979 |
> #### static_templates |
| 980 |
> |
| 981 |
> This option mirrors the [theme] config option of the same name and allows |
| 982 |
> some defaults to be set by the theme. Note that while the user can add |
| 983 |
> templates to this list, the user cannot remove templates included in the |
| 984 |
> theme's config. |
| 985 |
> |
| 986 |
> #### extends |
| 987 |
> |
| 988 |
> Defines a parent theme that this theme inherits from. The value should be |
| 989 |
> the string name of the parent theme. Normal [Jinja inheritance rules] |
| 990 |
> apply. |
| 991 |
|
| 992 |
Plugins may also define some options which allow the theme to inform a plugin |
| 993 |
about which set of plugin options it expects. See the documentation for any |
| 994 |
plugins you may wish to support in your theme. |
| 995 |
|
| 996 |
### Distributing Themes |
| 997 |
|
| 998 |
With the above changes, your theme should now be ready to install. This can be |
| 999 |
done with pip, using `pip install .` if you are still in the same directory as |
| 1000 |
the setup.py. |
| 1001 |
|
| 1002 |
Most Python packages, including MkDocs, are distributed on PyPI. To do this, |
| 1003 |
you should run the following command. |
| 1004 |
|
| 1005 |
```bash |
| 1006 |
python setup.py register |
| 1007 |
``` |
| 1008 |
|
| 1009 |
If you don't have an account setup, you should be prompted to create one. |
| 1010 |
|
| 1011 |
For a much more detailed guide, see the official Python packaging |
| 1012 |
documentation for [Packaging and Distributing Projects]. |
| 1013 |
|
| 1014 |
[Packaging and Distributing Projects]: https://packaging.python.org/en/latest/distributing/ |
| 1015 |
[Jinja inheritance rules]: https://jinja.palletsprojects.com/en/latest/templates/#template-inheritance |
| 1016 |
|
| 1017 |
## Supporting theme Localization/Translation |
| 1018 |
|
| 1019 |
While the built-in themes provide support for [localization/translation] of |
| 1020 |
templates, custom themes and third-party themes may choose not to. Regardless, |
| 1021 |
the [`locale`](#locale) setting of the `theme` configuration option is always |
| 1022 |
present and is relied upon by other parts of the system. Therefore, it is |
| 1023 |
recommended that all third-party themes use the same setting for designating a |
| 1024 |
language regardless of the system they use for translation. In that way, users |
| 1025 |
will experience consistent behavior regardless of the theme they may choose. |
| 1026 |
|
| 1027 |
The method for managing translations is up to the developers of a theme. |
| 1028 |
However, if a theme developer chooses to use the same mechanisms used by the |
| 1029 |
built-in themes, the sections below outline how to enable and make use of the |
| 1030 |
same commands utilized by MkDocs. |
| 1031 |
|
| 1032 |
[localization/translation]: ../user-guide/localizing-your-theme.md |
| 1033 |
|
| 1034 |
### Using the Localization/Translation commands |
| 1035 |
|
| 1036 |
WARNING: |
| 1037 |
As **[pybabel] is not installed by default** and most users will not have |
| 1038 |
pybabel installed, theme developers and/or translators should make sure to |
| 1039 |
have installed the necessary dependencies |
| 1040 |
(using `pip install 'mkdocs[i18n]'`) in order for the commands to be |
| 1041 |
available for use. |
| 1042 |
|
| 1043 |
The translation commands should be called from the root of your theme's working tree. |
| 1044 |
|
| 1045 |
For an overview of the workflow used by MkDocs to translate the built-in |
| 1046 |
themes, see the appropriate [section] of the Contributing Guide and the |
| 1047 |
[Translation Guide]. |
| 1048 |
|
| 1049 |
[pybabel]: https://babel.pocoo.org/en/latest/setup.html |
| 1050 |
[section]: ../about/contributing.md#submitting-changes-to-the-builtin-themes |
| 1051 |
[Translation Guide]: translations.md |
| 1052 |
|
| 1053 |
### Example custom theme Localization/Translation workflow |
| 1054 |
|
| 1055 |
> NOTE: If your theme inherits from an existing theme which already provides |
| 1056 |
> translation catalogs, your theme's translations will be merged with the |
| 1057 |
> parent theme's translations during a MkDocs build. |
| 1058 |
> |
| 1059 |
> This means that you only need to concentrate on the added translations. |
| 1060 |
> Yet, you will still benefit from the translations of the parent theme. At |
| 1061 |
> the same time, you may override any of parent theme's translations! |
| 1062 |
|
| 1063 |
Let's suppose that you're working on your own fork of the |
| 1064 |
[mkdocs-basic-theme][basic theme] and want to add translations to it. |
| 1065 |
|
| 1066 |
Edit the templates by wrapping text in your HTML sources with |
| 1067 |
`{% trans %}` and `{% endtrans %}` as follows: |
| 1068 |
|
| 1069 |
```diff |
| 1070 |
--- a/basic_theme/base.html |
| 1071 |
+++ b/basic_theme/base.html |
| 1072 |
@@ -88,7 +88,7 @@ |
| 1073 |
|
| 1074 |
<body> |
| 1075 |
|
| 1076 |
- <h1>This is an example theme for MkDocs.</h1> |
| 1077 |
+ <h1>{% trans %}This is an example theme for MkDocs.{% endtrans %}</h1> |
| 1078 |
|
| 1079 |
<p> |
| 1080 |
It is designed to be read by looking at the theme HTML which is heavily |
| 1081 |
``` |
| 1082 |
|
| 1083 |
Then you would follow the [Translation Guide] as usual to get your translations |
| 1084 |
running. |
| 1085 |
|
| 1086 |
### Packaging Translations with your theme |
| 1087 |
|
| 1088 |
While the Portable Object Template (`pot`) file created by the |
| 1089 |
`extract_messages` command and the Portable Object (`po`) files created by the |
| 1090 |
`init_catalog` and `update_catalog` commands are useful for creating and |
| 1091 |
editing translations, they are not used by MkDocs directly and do not need to |
| 1092 |
be included in a packaged release of a theme. When MkDocs builds a site with |
| 1093 |
translations, it only makes use of the binary `mo` files(s) for the specified |
| 1094 |
locale. Therefore, when [packaging a theme], make sure to include it in the |
| 1095 |
"wheels", using a `MANIFEST.in` file or otherwise. |
| 1096 |
|
| 1097 |
Then, before building your Python package, you will want to ensure that the |
| 1098 |
binary `mo` file for each locale is up-to-date by running the `compile_catalog` |
| 1099 |
command for each locale. MkDocs expects the binary `mo` files to be located at |
| 1100 |
`locales/<locale>/LC_MESSAGES/messages.mo`, which the `compile_catalog` |
| 1101 |
command automatically does for you. See [Testing theme translations] for |
| 1102 |
details. |
| 1103 |
|
| 1104 |
NOTE: |
| 1105 |
As outlined in our [Translation Guide], the MkDocs project has chosen to |
| 1106 |
include the `pot` and `po` files in our code repository, but not the |
| 1107 |
`mo` files. This requires us to always run `compile_catalog` before |
| 1108 |
packaging a new release regardless of whether any changes were made to a |
| 1109 |
translation or not. However, you may chose an alternate workflow for your |
| 1110 |
theme. At a minimum, you need to ensure that up-to-date `mo` files are |
| 1111 |
included at the correct location in each release. However, you may use a |
| 1112 |
different process for generating those `mo` files if you chose to do so. |
| 1113 |
|
| 1114 |
[packaging a theme]: #packaging-themes |
| 1115 |
[Testing theme translations]: translations.md#testing-theme-translations |