Skip to content

mkdocs_nype

Synopsis

MkDocs Nype theme, based on the Material for MkDocs theme.

This __init__.py file gets loaded by MkDocs before plugins are initialized. Therefore, this is a good place to monkey-patch changes into the build process.

Consider both nype/ and material/ namespaces when loading plugins

Due to how themename/pluginname namespaces work to make using Nype projects more convenient we change the logic of how namespaces are resolved. pluginname will be resolved in order:

  1. nype/pluginname
  2. material/pluginname
  3. pluginname

Inject predefined plugins for loading even when they're not in mkdocs.yml

Instead of requiring to enable a plugin in a project mkdocs.yml, enable it by default. This allows nype_tweaks to operate as a project manager for the entirety of the event loop.

Extend Material theme plugins directly

Instead of configuring other plugins and overriding internals of mkdocs-material plugins during the event loop, extend them before they're loaded into "Python MkDocs memory". This allows to directly modify how the PluginConfig is resolved, therefore allows to add options directly under the extended plugin in mkdocs.yml instead of under another plugin.

MIT License 2024 Kamil Krzyśków (HRY) for Nype (npe.cm)

LOG

LOG: PrefixedLogger = PrefixedLogger('mkdocs_nype', getLogger(f'mkdocs.themes.mkdocs_nype'))

issue_counter

issue_counter = CountHandler()

This is fetched in nype_tweaks to trigger --strict flag based on __init__.py

WANTED_PLUGINS

WANTED_PLUGINS: tuple[PluginEntry] = (PluginEntry('nype_tweaks', scope='nype'), PluginEntry('minify_html', config={'enabled': _parse_env_flag(), 'keep_html_and_head_opening_tags': True, 'keep_closing_tags': True}), PluginEntry('webp_images', scope='nype', config={'enabled': _parse_env_flag(), 'ignore_paths': dedent(strip())}))

Tuple of wanted PluginEntries. Scope can be None. Config can be None to use defaults.

PluginEntry

PluginEntry(name: str, scope: str = None, config: dict = None)

Used for injecting plugins

name

name: str

scope

scope: str = None

config

config: dict = None

patch_plugin_loading

patch_plugin_loading()

Monkey patches the load_plugin_with_namespace function after confirming that the hash is the same.

load_plugin_with_namespace

load_plugin_with_namespace(self, name: str, config)

Adapted code from the MkDocs 1.6.0 repository. Modification adds material/pluginname fallback.

patch_default_plugins_auto_load

patch_default_plugins_auto_load()

Monkey patches the run_validation function after confirming that the hash is the same.

run_validation

run_validation(self, value: object) -> mkdocs.plugins.PluginCollection

Adapted code from the MkDocs 1.6.0 repository. Modification adds nype_tweaks plugin when missing.

_get_checksum

_get_checksum(source: str) -> str

Remove comments, spaces, turn lowercase to get a minified token and return a sha256 hash from it

_parse_env_flag

_parse_env_flag(varname: str = 'CI', default: bool = False) -> bool

Parse environment variable as a flag for injected plugin config