# Usage ## Autodoc syntax *mkdocstrings* works by processing special expressions in your Markdown files. The syntax is as follows: ```md ::: identifier YAML block ``` > 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 idiosyncrasies](https://docs.saltproject.io/en/3000/topics/troubleshooting/yaml_idiosyncrasies.html) > - [YAML multiline](https://yaml-multiline.info/) The `identifier` is 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](#global-options)'s `default_handler` key, 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 (in `mkdocs.yml`, see [Global options](#global-options)), locally (as described here), or both. !!! example "Example with the Python handler" === "docs/my_page.md" ```md # Documentation for `MyClass` ::: my_package.my_module.MyClass handler: python options: members: - method_a - method_b show_root_heading: false show_source: false ``` === "mkdocs.yml" ```yaml nav: - "My page": my_page.md ``` === "src/my_package/my_module.py" ```python class MyClass: """Print print print!""" def method_a(self): """Print A!""" print("A!") def method_b(self): """Print B!""" print("B!") def method_c(self): """Print C!""" print("C!") ``` === "Result"
MyClassPrint print print!
method_a(self)
Print A!
method_b(self)
Print B!
With a custom title:
Object 1
With the identifier as title: full.path.object2
``` Any item that was inserted using the [autodoc syntax](#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.object1` then 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: **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*](https://github.com/mkdocstrings/autorefs) by adding `autorefs` to `plugins`: ```yaml title="mkdocs.yml" hl_lines="3" plugins: - search - autorefs - mkdocstrings: [...] ``` Note that you don't need to (`pip`) install anything more; this plugin is guaranteed to be pulled in with *mkdocstrings*. !!! example === "doc1.md" ```md ## Hello, world! Testing ``` === "doc2.md" ```md ## Something else Please see the [Hello, World!][hello-world] section. ``` === "Result HTML for doc2" ```htmlPlease see the Hello, World! section.
``` ### Cross-references to a sub-heading in a docstring 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 the `foo.bar` object, joined with "`--`". !!! example === "foo.py" ```python def bar(): """Hello, world! # Tips - Stay hydrated. """ ``` === "doc1.md" ```md ::: foo.bar ``` === "doc2.md" ```md Check out the [tips][foo.bar--tips] ``` === "HTML result for doc2" ```htmlCheck out the tips
``` The above tip about [Finding out the anchor](#finding-out-the-anchor) also applies the same way here. You may also notice that such a heading does not get rendered as a `See installer.records to learn about records.
``` === "Result (displayed)" See [installer.records][] to learn about records. You can of course select another version of the inventory, for example: ```yaml plugins: - mkdocstrings: handlers: python: import: # latest instead of stable - https://installer.readthedocs.io/en/latest/objects.inv ``` In case the inventory file is not served under the base documentation URL, you can explicitly specify both URLs: ```yaml plugins: - mkdocstrings: handlers: python: import: - url: https://cdn.example.com/version/objects.inv base_url: https://docs.example.com/version ``` Absolute URLs to cross-referenced items will then be based on `https://docs.example.com/version/` instead of `https://cdn.example.com/version/`. 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.inv` in 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_inventory` option: ```yaml plugins: - mkdocstrings: enable_inventory: false ``` ## Watch directories DANGER: **Deprecated since version 0.19.** Instead, use the built-in [`watch` feature of MkDocs](https://www.mkdocs.org/user-guide/configuration/#watch). You can add directories to watch with the `watch` key. It accepts a list of paths. ```yaml title="mkdocs.yml" plugins: - mkdocstrings: watch: - src/my_package_1 - src/my_package_2 ``` When serving your documentation and a change occur in one of the listed path, MkDocs will rebuild the site and reload the current page. NOTE: **The `watch` feature doesn't have special effects.** Adding directories to the `watch` list doesn't have any other effect than watching for changes. For example, it will not tell the Python handler to look for packages in these paths (the paths are not added to the `PYTHONPATH` variable). If you want to tell Python where to look for packages and modules, see [Python Handler: Finding modules](https://mkdocstrings.github.io/python/usage/#finding-modules).