MkDocs Simple Hooks
mkdocs-simple-hooks is a deprecated Python plugin for MkDocs that allowed users to define custom hooks directly within their project's Python modules without needing to create a separate MkDocs plugin package. Its functionality has been natively implemented in MkDocs 1.4 and later. The current version is 0.1.5, with the last release in January 2022, indicating an inactive release cadence.
Warnings
- breaking This plugin is officially deprecated and no longer supported as MkDocs 1.4.0 and later versions include native support for hooks, making this plugin redundant. Users are strongly advised to migrate to the native MkDocs hooks functionality.
- gotcha If you don't have a `plugins` entry in your `mkdocs.yml`, MkDocs typically enables the `search` plugin by default. When you add `mkdocs-simple-hooks`, you must explicitly list `search` if you still want it, as adding any plugin disables implicit defaults.
- gotcha To enable/disable the plugin dynamically, especially in different environments, you can use MkDocs' `!ENV` tag with an environment variable. Without careful handling, the plugin might be active when not intended or vice-versa.
Install
-
pip install mkdocs-simple-hooks
Imports
- Hook definition in mkdocs.yml
plugins: - mkdocs-simple-hooks: hooks: on_pre_build: "docs.hooks:copy_readme"
Quickstart
# 1. Create a Python file, e.g., 'docs/hooks.py'
# content of docs/hooks.py:
import shutil
def copy_readme(*args, **kwargs):
shutil.copy('README.md', 'docs/index.md')
# 2. Configure mkdocs.yml (add this to your existing mkdocs.yml):
# plugins:
# - mkdocs-simple-hooks:
# hooks:
# on_pre_build: "docs.hooks:copy_readme"
# 3. Ensure 'mkdocs' is installed and run from your project root:
# pip install mkdocs
# mkdocs build
# mkdocs serve