|
1 |
| -This package provides an extended version of the [standard python handler][mkdocstrings_python] |
2 |
| -for [mkdocstrings] that provides the following additional features: |
| 1 | +## Relative cross-references |
3 | 2 |
|
4 |
| -* [relative cross-references](relative-crossref.md) allows you to write |
5 |
| - cross-reference paths compactly relative to the item it documents. |
| 3 | +By default, [mkdocstrings] only supports cross-references where the path is |
| 4 | +fully qualified or is empty, in which case it is taken from the title. |
| 5 | +If you work with long package and class names or with namespace packages, this can result in a lot |
| 6 | +of extra typing and harder to read doc-strings. |
6 | 7 |
|
7 |
| -Some of these features may eventually find their way into the official python handler. |
| 8 | +If you enable the `relative_crossrefs` option in the `python_xref` handler options |
| 9 | +in your mkdocs.yml file ([see configuration](config.md) for an example), then the handler |
| 10 | +will support more compact relative syntax: |
| 11 | + |
| 12 | +=== "Absolute" |
| 13 | + |
| 14 | + ```python |
| 15 | + class MyClass: |
| 16 | + def this_method(self): |
| 17 | + """ |
| 18 | + See [other_method][mypkg.mymod.MyClass.other_method] |
| 19 | + from [MyClass][mypkg.mymod.Myclass] |
| 20 | + """ |
| 21 | + ``` |
| 22 | + |
| 23 | +=== "Relative" |
| 24 | + |
| 25 | + ```python |
| 26 | + class MyClass: |
| 27 | + def this_method(self): |
| 28 | + """ |
| 29 | + See [other_method][..] from [MyClass][(c)] |
| 30 | + """ |
| 31 | + ``` |
| 32 | + |
| 33 | +The relative path specifier has the following form: |
| 34 | + |
| 35 | +* If the path ends in `.` then the title text will be appended to the path |
| 36 | + (ignoring bold, italic or code markup). |
| 37 | + |
| 38 | +* If the path begins with a single `.` then it will be expanded relative to the path |
| 39 | + of the doc-string in which it occurs. |
| 40 | + |
| 41 | +* If the path begins with `(c)`, that will be replaced by the path of the |
| 42 | + class that contains the doc-string |
| 43 | + |
| 44 | +* If the path begins with `(m)`, that will be replaced by the path of the |
| 45 | + module that contains the doc-string |
| 46 | + |
| 47 | +* If the path begins with `(p)`, that will be replaced by the path of the |
| 48 | + package that contains the doc-string. If there is only one module in the |
| 49 | + system it will be treated as a package. |
| 50 | + |
| 51 | +* If the path begins with one or more `^` characters, then will be replaced |
| 52 | + by the path of the parent element. For example, when used in a doc-string |
| 53 | + for a method, `^` would get replaced with the class and `^^` would get |
| 54 | + replaced with the module. |
| 55 | + |
| 56 | +* Similarly, if the path begins with two or more `.` characters, then all but |
| 57 | + the last `.` will be replaced by the parent element, and if nothing follows |
| 58 | + the last `.`, the title text will be appended according to the first rule. |
| 59 | + |
| 60 | + *NOTE: When using either `^` or `..` we have found that going up more than one |
| 61 | + or two levels makes cross-references difficult to read and should be avoided* |
| 62 | + |
| 63 | +These are demonstrated here: |
| 64 | + |
| 65 | +=== "Relative" |
| 66 | + |
| 67 | + ```python |
| 68 | + class MyClass: |
| 69 | + def this_method(self): |
| 70 | + """ |
| 71 | + [MyClass][^] |
| 72 | + Also [MyClass][(c)] |
| 73 | + [`that_method`][^.] |
| 74 | + Also [`that_method`][..] |
| 75 | + [init method][(c).__init__] |
| 76 | + [this module][(m)] |
| 77 | + [this package][(p)] |
| 78 | + [OtherClass][(m).] |
| 79 | + [some_func][^^.] or [some_func][...] |
| 80 | + """ |
| 81 | + ``` |
| 82 | + |
| 83 | +=== "Absolute" |
| 84 | + |
| 85 | + ```python |
| 86 | + class MyClass: |
| 87 | + def this_method(self): |
| 88 | + """ |
| 89 | + [MyClass][mypkg.mymod.MyClass] |
| 90 | + Also [MyClass][mypkg.mymod.MyClass] |
| 91 | + [`that_method`][mypkg.mymod.MyClass.that_method] |
| 92 | + Also [`that_method`][mypkg.mymod.MyClass.that_method] |
| 93 | + [init method][mypkg.mymod.MyClass.__init__] |
| 94 | + [this module][mypkg.mymod] |
| 95 | + [this package][mypkg] |
| 96 | + [OtherClass][mypkg.mymod.OtherClass] |
| 97 | + [some_func][mypkg.mymod.some_func] |
| 98 | + """ |
| 99 | + ``` |
| 100 | + |
| 101 | +This has been [proposed as a feature in the standard python handler][relative-crossref-issue] |
| 102 | +but has not yet been accepted. |
8 | 103 |
|
9 | 104 | [mkdocstrings]: https://mkdocstrings.github.io/
|
10 | 105 | [mkdocstrings_python]: https://mkdocstrings.github.io/python/
|
| 106 | +[relative-crossref-issue]: https://github.com/mkdocstrings/python/issues/27 |
11 | 107 |
|
12 | 108 |
|
0 commit comments