Skip to content

Commit

Permalink
feat: Expose the full config to handlers
Browse files Browse the repository at this point in the history
Instead of just passing the handler config on initialization pass the
full dictionary of `extension_config` directly to the handler.

Issue #501: #501
PR #509: #509
  • Loading branch information
jdpatt authored Apr 2, 2023
1 parent 82016df commit 15dacf6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
8 changes: 6 additions & 2 deletions docs/handlers/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,12 @@ This function takes the following parameters:

These arguments are all passed as keyword arguments, so you can ignore them
by adding `**kwargs` or similar to your signature. You can also accept
additional parameters: the handler's global-only options will be passed
to this function when instantiating your handler.
additional parameters: the handler's global-only options and/or the root
config options. This gives flexibility and access to the mkdocs config, mkdocstring
config etc.. You should never modify the root config but can use it to get
information about the MkDocs instance such as where the current `site_dir` lives.
See the [Mkdocs Configuration](https://www.mkdocs.org/user-guide/configuration/) for
more info about what is accessible from it.

Check out how the
[Python handler](https://github.com/mkdocstrings/python/blob/master/src/mkdocstrings_handlers/python)
Expand Down
5 changes: 3 additions & 2 deletions src/mkdocstrings/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def __init__(self, config: dict) -> None:
"""
self._config = config
self._handlers: Dict[str, BaseHandler] = {}
self.inventory: Inventory = Inventory(project=self._config["site_name"])
self.inventory: Inventory = Inventory(project=self._config["mkdocs"]["site_name"])

def get_anchors(self, identifier: str) -> Sequence[str]:
"""Return the canonical HTML anchor for the identifier, if any of the seen handlers can collect it.
Expand Down Expand Up @@ -582,6 +582,7 @@ def get_handler(self, name: str, handler_config: Optional[dict] = None) -> BaseH
if name not in self._handlers:
if handler_config is None:
handler_config = self.get_handler_config(name)
handler_config.update(self._config)
try:
module = importlib.import_module(f"mkdocstrings_handlers.{name}")
except ModuleNotFoundError:
Expand All @@ -596,7 +597,7 @@ def get_handler(self, name: str, handler_config: Optional[dict] = None) -> BaseH
self._handlers[name] = module.get_handler(
theme=self._config["theme_name"],
custom_templates=self._config["mkdocstrings"]["custom_templates"],
config_file_path=self._config["config_file_path"],
config_file_path=self._config["mkdocs"]["config_file_path"],
**handler_config,
)
return self._handlers[name]
Expand Down
3 changes: 1 addition & 2 deletions src/mkdocstrings/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,11 @@ def on_config(self, config: Config, **kwargs: Any) -> Config: # noqa: W0613 (un
to_import.append((handler_name, import_item))

extension_config = {
"site_name": config["site_name"],
"config_file_path": config["config_file_path"],
"theme_name": theme_name,
"mdx": config["markdown_extensions"],
"mdx_configs": config["mdx_configs"],
"mkdocstrings": self.config,
"mkdocs": config,
}
self._handlers = Handlers(extension_config)

Expand Down

0 comments on commit 15dacf6

Please sign in to comment.