Skip to content

Commit

Permalink
fix: some logic safeguards
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkrzyskow committed Sep 23, 2024
1 parent d496cb7 commit 657ed40
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
7 changes: 5 additions & 2 deletions mkdocs_nype/plugins/nype_tweaks/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

from ...utils import MACROS_INCLUDES_ROOT
from .config import NypeTweaksConfig
from .utils import get_file_system_loader
from .utils import ServeMode, get_file_system_loader

# region Core Logic Events

Expand Down Expand Up @@ -62,7 +62,8 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig | None:
handler.counts[level] += count

# Extend macros includes directory tweak
macros_module.FileSystemLoader = get_file_system_loader
if not ServeMode.run_once:
macros_module.FileSystemLoader = get_file_system_loader

LOG.info("Tweaks initialized")

Expand Down Expand Up @@ -109,6 +110,8 @@ def on_serve(
if "--watch-theme" in sys.argv:
server.watch(str(MACROS_INCLUDES_ROOT))

ServeMode.run_once = True


# endregion

Expand Down
7 changes: 7 additions & 0 deletions mkdocs_nype/plugins/nype_tweaks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
from ...utils import MACROS_INCLUDES_ROOT


class ServeMode:
"""Helps to track if serve runs again"""

run_once = False
"""Toggle"""


def get_file_system_loader(value: str | list[str]):
"""Proxy function to get the Jinja2 FileSystemLoader with theme macros_includes"""

Expand Down
5 changes: 5 additions & 0 deletions mkdocs_nype/templates/nype-base.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{% extends "base.html" -%}

{# The nype_tweaks does some config.theme value modification / validation. Best to force the usage. #}
{% if "nype/nype_tweaks" not in config.plugins %}
{% if {}.__getitem__('ERROR: The nype_tweaks plugin is required to run the nype theme. Please enable it in mkdocs.yml') %}{% endif %}
{% endif %}

{# Fallback for setting the is_serve status #}
{% if config.theme.is_serve is none %}
{% set _ = config.theme.update({ 'is_serve': config.site_url.split('//')[1].split(':')[0] in ['127.0.0.1', 'localhost'] }) %}
Expand Down

0 comments on commit 657ed40

Please sign in to comment.