Usage
Usage⚓︎
Autodoc Syntax⚓︎
Special Expressions⚓︎
Special Expressions
mkdocstrings works by processing special expressions in your Markdown files.
The syntax is as follows:
Resources on YAML⚓︎
NOTE
NOTE: Resources on YAML.
- YAML can sometimes be a bit tricky, particularly on indentation.
Here are some resources that other users found useful to better understand YAML's peculiarities.
- YAML multiline
Identifier⚓︎
Identifier
-
The
identifieris a string identifying the object you want to document. -
The format of an identifier can vary from one handler to another.
-
For example, the Python handler expects the full dotted-path to a Python object:
my_package.my_module.MyClass.my_method.
The YAML block is optional, and contains some configuration options:
-
handler: the name of the handler to use to collect and render this object. By default, it will use the value defined in the Global options'sdefault_handlerkey, or"python". -
options: a dictionary of options passed to the handler's methods responsible both for collecting and rendering the documentation. These options can be defined globally (inmkdocs.yml, see Global options), locally (as described here), or both.
Example with the Python handler
INFO
It is also possible to integrate a mkdocstrings identifier into a Markdown header:
The above is equivalent to:
Global Options⚓︎
Global Options
mkdocstrings accepts a few top-level configuration options in mkdocs.yml:
-
default_handler: The handler that is used by default when no handler is specified. -
custom_templates: The path to a directory containing custom templates.
The path is relative to the MkDocs configuration file. See Theming.
handlers: The handlers' global configuration.enable_inventory: Whether to enable inventory file generation.
See Cross-references to other projects / inventories
-
locale: The locale used for translations. See Internationalization. -
enabled(New in version 0.20): Whether to enable the plugin. Defaults totrue.
Can be used to reduce build times when doing local development. Especially useful when used with environment variables (see example below).
Example
plugins:
- mkdocstrings:
enabled: !ENV [ENABLE_MKDOCSTRINGS, true]
custom_templates: templates
default_handler: python
locale: en
handlers:
python:
options:
show_source: false
The handlers global configuration can then be overridden by local configurations:
Handler of Interest!
Some handlers accept additional global configuration. Check the documentation for your handler of interest in Handlers.
Internationalization (I18N)⚓︎
Multiple Languages
Some handlers support multiple languages.
If the handler supports localization, the locale it uses is determined by the following order of precedence:
localein global optionstheme.language: used by the MkDocs Material themetheme.localein MkDocs configuration
Cross-references⚓︎
Cross-References
Cross-references are written as Markdown reference-style links:
-
Any item that was inserted using the autodoc syntax (e.g.
full.path.object1) is possible to link to by using the same identifier with the cross-reference syntax ([example][full.path.object1]). -
But the cross-references are also applicable to the items' children that get pulled in.
Finding out the Anchor⚓︎
-
If you're not sure which exact identifier a doc item uses, you can look at its "anchor", which your Web browser will show in the URL bar when clicking an item's entry in the table of contents.
-
If the URL is
https://example.com/some/page.html#full.path.object1then you know that this item is possible to link to with[example][full.path.object1], regardless of the current page.
Cross-references to any Markdown Heading⚓︎
TIP!
TIP: Changed in version 0.15.
Linking to any Markdown heading used to be the default, but now opt-in is required.
If you want to link to any Markdown heading, not just mkdocstrings-inserted items, please enable the autorefs plugin for MkDocs by adding autorefs to plugins:
Note that you don't need to (pip) install anything more; this plugin is guaranteed to be pulled in with mkdocstrings.
Example
Cross-references to a Sub-heading in a Docstring⚓︎
TIP
TIP: New in version 0.14.
- If you have a Markdown heading inside your docstring, you can also link directly to it.
- In the example below you see the identifier to be linked is
foo.bar--tips, because it's the "Tips" heading that's part of thefoo.barobject, joined with "--".
Example
The above tip about Finding out the anchor also applies the same way here.
-
You may also notice that such a heading does not get rendered as a
<h1>element directly, but rather the level gets shifted to fit the encompassing document structure. -
If you're curious about the implementation, check out mkdocstrings.HeadingShiftingTreeprocessor and others.
Cross-references to Other Projects / Inventories⚓︎
TIP
TIP: New in version 0.16.
Coming from Sphinx
Python developers coming from Sphinx might know about its intersphinx extension, that allows to cross-reference items between several projects. mkdocstrings has a similar feature.
To reference an item from another project, you must first tell mkdocstrings to load the inventory it provides. Each handler will be responsible of loading inventories specific to its language.
For example, the Python handler can load Sphinx-generated inventories (objects.inv).
Load Inventory
In the following snippet, we load the inventory provided by installer:
Cross-reference installer
Now it is possible to cross-reference installer's items. For example:
Select another Version
You can of course select another version of the inventory, for example:
Explicitly specify both URL's
In case the inventory file is not served under the base documentation URL, you can explicitly specify both URLs:
Absolute URL's
Absolute URLs to cross-referenced items will then be based on https://docs.example.com/version/ instead of https://cdn.example.com/version/.
If you need authentication to access the inventory file, you can provide the credentials in the URL, either as username:password:
Inventory file in the Sphinx Format
-
Reciprocally, mkdocstrings also allows to generate an inventory file in the Sphinx format.
-
It will be enabled by default if the Python handler is used, and generated as
objects.invin the final site directory. -
Other projects will be able to cross-reference items from your project.
-
To explicitly enable or disable the generation of the inventory file, use the global
enable_inventoryoption:
info