diff --git a/hooks/kroki.py b/hooks/kroki.py index 54aa5ed..39a2629 100644 --- a/hooks/kroki.py +++ b/hooks/kroki.py @@ -3,7 +3,7 @@ import zlib from mkdocs.utils.cache import download_and_cache_url from datetime import timedelta from mkdocs.plugins import get_plugin_logger -from urllib.request import build_opener, install_opener +from urllib.request import build_opener, install_opener, _opener import hashlib from functools import partial from mkdocs.utils import write_file @@ -51,11 +51,6 @@ def fence_div_format(site_dir, source, language, *args, attrs, **kwargs): return f"{title}" def on_config(config): - # Override user-agent so kroki accepts it - opener = build_opener() - opener.addheaders = [('User-Agent','mkdocs')] - install_opener(opener) - custom_fences = [ { "name": diagram, @@ -69,3 +64,14 @@ def on_config(config): "custom_fences": custom_fences } return config + + +def on_startup(command, dirty): + # Override user-agent so kroki accepts it + if _opener is None: + opener = build_opener() + install_opener(opener) + else: + opener = _opener + + opener.addheaders = [('User-Agent','mkdocs')]