I would like to write a plugin for mkdocs that allows to add a file of custom python code. Here is an example of plugin.
I would like to put the module in the website's main dir, alongside the mkdocs.yml
file, and declare that module in there, e.g.:
python_module=mycode.py
Then I would use in the code of the plugin, something like:
from mkdocs.plugins import BasePlugin
from jinja2 import Template
import importlib
class MarkdownExtraDataPlugin(BasePlugin):
"Execute piece of code"
def on_page_markdown(self, markdown, page, config, site_navigation, **kwargs):
python_module = config.get('python_module')
if python_module:
# Add path, here is the question:
python_module = os.path.join(*?????*, python_module)
# do import here:
importlib.import_module(python_module)
....
return result
The problem I am having at this point is I don't know how the code of the module could know the location of the yaml file, or the location of the markdown file. By any chance, would that be part of the config
dictionary?
Update
I wrote the plugin and made it available on github. Thanks a lot for your help.