|
3 | 3 |
|
4 | 4 | """This module defines macros for use in Markdown files.""" |
5 | 5 |
|
6 | | -from typing import Any |
7 | | - |
8 | | -import markdown as md |
| 6 | +from frequenz.repo.config.mkdocs.mkdocstrings_macros import hook_env_with_everything |
9 | 7 | from griffe import ModulesCollection, Object |
10 | | -from markdown.extensions import toc |
11 | | -from mkdocs_macros import plugin as macros |
| 8 | +from mkdocs_macros.plugin import MacrosPlugin |
12 | 9 | from mkdocstrings_handlers.python.handler import PythonHandler |
13 | 10 |
|
14 | | -_CODE_ANNOTATION_MARKER: str = ( |
15 | | - r'<span class="md-annotation">' |
16 | | - r'<span class="md-annotation__index" tabindex="-1">' |
17 | | - r'<span data-md-annotation-id="1"></span>' |
18 | | - r"</span>" |
19 | | - r"</span>" |
20 | | -) |
21 | | - |
22 | | - |
23 | | -def _slugify(text: str) -> str: |
24 | | - """Slugify a text. |
25 | | -
|
26 | | - Args: |
27 | | - text: The text to slugify. |
28 | | -
|
29 | | - Returns: |
30 | | - The slugified text. |
31 | | - """ |
32 | | - return toc.slugify_unicode(text, "-") |
33 | | - |
34 | | - |
35 | | -def _hook_macros_plugin(env: macros.MacrosPlugin) -> None: |
36 | | - """Integrate the `mkdocs-macros` plugin into `mkdocstrings`. |
37 | | -
|
38 | | - This is a temporary workaround to make `mkdocs-macros` work with |
39 | | - `mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See |
40 | | - https://github.com/mkdocstrings/mkdocstrings/issues/615 for details. |
41 | | -
|
42 | | - Args: |
43 | | - env: The environment to hook the plugin into. |
44 | | - """ |
45 | | - # get mkdocstrings' Python handler |
46 | | - python_handler = env.conf["plugins"]["mkdocstrings"].get_handler("python") |
47 | | - |
48 | | - # get the `update_env` method of the Python handler |
49 | | - update_env = python_handler.update_env |
50 | | - |
51 | | - # override the `update_env` method of the Python handler |
52 | | - def patched_update_env(markdown: md.Markdown, config: dict[str, Any]) -> None: |
53 | | - update_env(markdown, config) |
54 | | - |
55 | | - # get the `convert_markdown` filter of the env |
56 | | - convert_markdown = python_handler.env.filters["convert_markdown"] |
57 | 11 |
|
58 | | - # build a chimera made of macros+mkdocstrings |
59 | | - def render_convert(markdown: str, *args: Any, **kwargs: Any) -> Any: |
60 | | - return convert_markdown(env.render(markdown), *args, **kwargs) |
61 | | - |
62 | | - # patch the filter |
63 | | - python_handler.env.filters["convert_markdown"] = render_convert |
64 | | - |
65 | | - # patch the method |
66 | | - python_handler.update_env = patched_update_env |
67 | | - |
68 | | - |
69 | | -def define_env(env: macros.MacrosPlugin) -> None: |
| 12 | +def define_env(env: MacrosPlugin) -> None: |
70 | 13 | """Define the hook to create macro functions for use in Markdown. |
71 | 14 |
|
72 | 15 | Args: |
73 | 16 | env: The environment to define the macro functions in. |
74 | 17 | """ |
75 | | - # A variable to easily show an example code annotation from mkdocs-material. |
76 | | - # https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations |
77 | | - env.variables["code_annotation_marker"] = _CODE_ANNOTATION_MARKER |
78 | | - |
79 | 18 | python_handler = env.conf["plugins"]["mkdocstrings"].get_handler("python") |
80 | 19 | assert isinstance(python_handler, PythonHandler) |
81 | 20 |
|
@@ -108,9 +47,10 @@ def docstring_summary(symbol: str) -> str: |
108 | 47 | """ |
109 | 48 | docstring = _get_docstring(symbol) |
110 | 49 | summary = docstring.splitlines(keepends=False)[0] |
111 | | - return python_handler.do_convert_markdown( |
| 50 | + # The python_handler is untyped here, so ignore the type |
| 51 | + return python_handler.do_convert_markdown( # type: ignore[no-any-return] |
112 | 52 | summary, heading_level=1, strip_paragraph=True |
113 | 53 | ) |
114 | 54 |
|
115 | | - # This hook needs to be done at the end of the `define_env` function. |
116 | | - _hook_macros_plugin(env) |
| 55 | + # This must be at the end to enable all standard features |
| 56 | + hook_env_with_everything(env) |
0 commit comments