@@ -407,6 +407,10 @@ def __init__(self, config_file_path: str | None = None) -> None:
407407 self .config_file_path = config_file_path
408408
409409
410+ _AUTOREFS = None
411+ _HANDLERS = None
412+
413+
410414def makeExtension ( # noqa: N802
411415 * ,
412416 default_handler : str | None = None ,
@@ -423,40 +427,65 @@ def makeExtension( # noqa: N802
423427 We only support this function being used by Zensical.
424428 Consider this function private API.
425429 """
426- mdx , mdx_config = _split_configs (markdown_extensions or [])
427- tool_config = _ToolConfig (config_file_path = config_file_path )
428-
429- autorefs = AutorefsPlugin ()
430- autorefs .config = AutorefsConfig ()
431- autorefs .config .resolve_closest = True
432- autorefs .config .link_titles = "auto"
433- autorefs .config .strip_title_tags = "auto"
434- autorefs .scan_toc = True
435- autorefs ._link_titles = "external"
436- autorefs ._strip_title_tags = False
437-
438- mdx .append (AutorefsExtension (autorefs ))
439-
440- handlers_instance = Handlers (
441- theme = "material" ,
442- default = default_handler or _default_config ["default_handler" ],
443- inventory_project = inventory_project or "Project" ,
444- inventory_version = inventory_version or "0.0.0" ,
445- handlers_config = handlers or _default_config ["handlers" ],
446- custom_templates = custom_templates or _default_config ["custom_templates" ],
447- mdx = mdx ,
448- mdx_config = mdx_config ,
449- locale = locale or _default_config ["locale" ],
450- tool_config = tool_config ,
451- )
430+ global _AUTOREFS # noqa: PLW0603
431+ if _AUTOREFS is None :
432+ _AUTOREFS = AutorefsPlugin ()
433+ _AUTOREFS .config = AutorefsConfig ()
434+ _AUTOREFS .config .resolve_closest = True
435+ _AUTOREFS .config .link_titles = "auto"
436+ _AUTOREFS .config .strip_title_tags = "auto"
437+ _AUTOREFS .scan_toc = True
438+ _AUTOREFS ._link_titles = "external"
439+ _AUTOREFS ._strip_title_tags = False
440+
441+ global _HANDLERS # noqa: PLW0603
442+ if _HANDLERS is None :
443+ mdx , mdx_config = _split_configs (markdown_extensions or [])
444+ tool_config = _ToolConfig (config_file_path = config_file_path )
445+ mdx .append (AutorefsExtension (_AUTOREFS ))
446+ _HANDLERS = Handlers (
447+ theme = "material" ,
448+ default = default_handler or _default_config ["default_handler" ],
449+ inventory_project = inventory_project or "Project" ,
450+ inventory_version = inventory_version or "0.0.0" ,
451+ handlers_config = handlers or _default_config ["handlers" ],
452+ custom_templates = custom_templates or _default_config ["custom_templates" ],
453+ mdx = mdx ,
454+ mdx_config = mdx_config ,
455+ locale = locale or _default_config ["locale" ],
456+ tool_config = tool_config ,
457+ )
452458
453- handlers_instance ._download_inventories ()
454- register = autorefs .register_url
455- for identifier , url in handlers_instance ._yield_inventory_items ():
456- register (identifier , url )
459+ _HANDLERS ._download_inventories ()
460+ register = _AUTOREFS .register_url
461+ for identifier , url in _HANDLERS ._yield_inventory_items ():
462+ register (identifier , url )
457463
458464 return MkdocstringsExtension (
459- handlers = handlers_instance ,
460- autorefs = autorefs ,
465+ handlers = _HANDLERS ,
466+ autorefs = _AUTOREFS ,
461467 autorefs_extension = True ,
462468 )
469+
470+
471+ def _reset () -> None :
472+ global _AUTOREFS , _HANDLERS # noqa: PLW0603
473+ _AUTOREFS = None
474+ _HANDLERS = None
475+
476+
477+ def _get_autorefs () -> dict [str , Any ]:
478+ if _AUTOREFS :
479+ return {
480+ "primary" : _AUTOREFS ._primary_url_map ,
481+ "secondary" : _AUTOREFS ._secondary_url_map ,
482+ "inventory" : _AUTOREFS ._abs_url_map ,
483+ "titles" : _AUTOREFS ._title_map ,
484+ }
485+ return {}
486+
487+
488+ def _get_inventory () -> bytes :
489+ if _HANDLERS :
490+ return _HANDLERS .inventory .format_sphinx ()
491+ return b""
0 commit comments