-
This is related to mkdocs/mkdocs#3487 I have this code snippet used as a hook in mkdocs to pull Strings from a JSON file and make them available for jinja2 templating through a Tho, since I started using this plugin does it seem like the hook doesn't work anymore. Probs because the variables in jinja2 are overriden by it, making the json value no longer available. This is proofen by the fact that I have a Now, since I already use this plugin could I also just move the entire code over to it. My python code from the hook: import jinja2
import posixpath
import json
from mkdocs.structure.files import File, Files
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.structure.pages import Page
def on_page_markdown(markdown: str, page: Page, files: Files, config: MkDocsConfig, **kwargs):
if page.file.src_uri != 'twitch-minigames/common/quip-battle.md':
return
path = posixpath.sep.join([config.docs_dir, "assets/extra_quips.json"])
with open(path, encoding="utf-8") as json_file:
json_content = json.load(json_file)
return jinja2.Template(markdown).render(json = json_content) The templating used on the page:
What would be the best aproach to move this over to the plugin to re-add this behaviour? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
If my understanding is correct, it should be straightfoward:
I haven't tested but it should be along those lines: def define_env(env):
"""
This is the hook for defining variables, macros and filters
- variables: the dictionary that contains the environment variables
- macro: a decorator function, to declare a macro.
- filter: a function with one of more arguments,
used to perform a transformation
"""
@env.macro
def read_json_file():
"Return json variable"
path = "assets/extra_quips.json"
with open(path, encoding="utf-8") as json_file:
return json.load(json_file) Then your markdown file:
|
Beta Was this translation helpful? Give feedback.
-
Yes, you're right. |
Beta Was this translation helpful? Give feedback.
-
If I am not mistaken, the environment uses Where is |
Beta Was this translation helpful? Give feedback.
If my understanding is correct, it should be straightfoward:
read_json_file()
inmain.py
which is a function that returns thejson
variable.I haven't tested but it should be along those lines: