|
4 | 4 | """This module defines macros for use in Markdown files."""
|
5 | 5 |
|
6 | 6 | import pathlib
|
| 7 | +from typing import Any |
7 | 8 |
|
| 9 | +import markdown as md |
8 | 10 | from markdown.extensions import toc
|
9 | 11 | from mkdocs_macros import plugin as macros
|
10 | 12 |
|
@@ -56,3 +58,30 @@ def glossary(term: str) -> str:
|
56 | 58 | glossary_path = pathlib.Path("intro/glossary.md")
|
57 | 59 | link_path = glossary_path.relative_to(current_path.parent)
|
58 | 60 | return f"[{term}]({link_path}#{_slugify(term)})"
|
| 61 | + |
| 62 | + # The code below is a temporary workaround to make `mkdocs-macros` work with |
| 63 | + # `mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See |
| 64 | + # https://github.com/mkdocstrings/mkdocstrings/issues/615 for details. |
| 65 | + |
| 66 | + # get mkdocstrings' Python handler |
| 67 | + python_handler = env.conf["plugins"]["mkdocstrings"].get_handler("python") |
| 68 | + |
| 69 | + # get the `update_env` method of the Python handler |
| 70 | + update_env = python_handler.update_env |
| 71 | + |
| 72 | + # override the `update_env` method of the Python handler |
| 73 | + def patched_update_env(markdown: md.Markdown, config: dict[str, Any]) -> None: |
| 74 | + update_env(markdown, config) |
| 75 | + |
| 76 | + # get the `convert_markdown` filter of the env |
| 77 | + convert_markdown = python_handler.env.filters["convert_markdown"] |
| 78 | + |
| 79 | + # build a chimera made of macros+mkdocstrings |
| 80 | + def render_convert(markdown: str, *args: Any, **kwargs: Any) -> Any: |
| 81 | + return convert_markdown(env.render(markdown), *args, **kwargs) |
| 82 | + |
| 83 | + # patch the filter |
| 84 | + python_handler.env.filters["convert_markdown"] = render_convert |
| 85 | + |
| 86 | + # patch the method |
| 87 | + python_handler.update_env = patched_update_env |
0 commit comments