Skip to content

Commit 58b3b4d

Browse files
authored
Add basic support for Jinja2 options (#29)
* Add support for Jinja2 config * Make it so that the "start" replacement does not necessarily need to be the last thing in the content * Var instead of magic value
1 parent b816b32 commit 58b3b4d

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

‎README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,19 @@ If inside your data folder you have a directory and a file file
102102
called `[path/to/datafiles/]sections/captions.yaml` - where `[path/to/datafiles/]` is the path in your configuration -
103103
the data inside that file will be available in your templates as `sections.captions.whatever_variable_in_the_yaml`.
104104

105+
### Jinja2 Template Engine Configuration
105106

107+
You may provide [Jinja2 configuration](https://jinja.palletsprojects.com/en/2.11.x/api/#high-level-api) as plugin options:
108+
109+
```yml
110+
plugins:
111+
- markdownextradata:
112+
jinja_options:
113+
comment_start_string: __CUSTOMCOMMENTSTART__
114+
```
115+
116+
The above example will make it so that instead of `{#`, the template engine will interpret `__CUSTOMCOMMENTSTART__` as comment start delimiter. This is useful in cases where
117+
you write Markdown that contains Jinja-like syntax that's colliding with the template engine. Alternatively, it lets you control what the variable delimiter is (instead of the default `{{ }}`).
106118

107119
## Testing
108120

‎markdownextradata/plugin.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ class MarkdownExtraDataPlugin(BasePlugin):
2727
Inject certain config variables into the markdown
2828
"""
2929

30+
JINJA_OPTIONS = "jinja_options"
31+
3032
config_scheme = (
3133
("data", mkdocs.config.config_options.Type(str_type, default=None)),
34+
(JINJA_OPTIONS, mkdocs.config.config_options.Type(dict, default={}))
3235
)
3336

3437
def __add_data__(self, config, namespace, data):
@@ -91,6 +94,7 @@ def on_pre_build(self, config, **kwargs):
9194
def on_page_markdown(self, markdown, config, **kwargs):
9295
context = {key: config.get(key) for key in CONFIG_KEYS if key in config}
9396
context.update(config.get("extra", {}))
94-
env = jinja2.Environment(undefined=jinja2.DebugUndefined)
97+
jinja_options = self.config[self.JINJA_OPTIONS]
98+
env = jinja2.Environment(undefined=jinja2.DebugUndefined, **jinja_options)
9599
md_template = env.from_string(markdown)
96100
return md_template.render(**config.get("extra"))

‎test/docs/index.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
Welcome to {{ customer.web_url }}
44

5-
Inside the included md file there 3 {{ star }}
5+
Inside the included md file there 3 {{ star }}
6+
7+
<!-- throws TemplateSyntaxError('Missing end of comment tag') unless comment_start_string is configured -->
8+
You can use `{#binding.path}` to update the UI when the model changes.

‎test/mkdocs.yml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use_directory_urls: true
55
# custom_dir: 'theme/'
66
plugins:
77
- search
8-
- markdownextradata
8+
- markdownextradata:
9+
jinja_options:
10+
comment_start_string: __COMMENTSTART__
911

1012
extra:
1113
star: "![star](ressources/star.png)"

‎test/test_basic.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def test_basic_working():
2323
contents = index_file.read_text()
2424

2525
assert '<h1 id="hi-there-your-name-here">Hi there, Your name here</h1>' in contents, f"customer.name is not in index"
26-
assert '<p>Inside the included md file there 3 <img alt="star" src="ressources/star.png" /></p></div>' in contents, f"customer.star is not in index or not rendering as expected"
26+
assert '<p>Inside the included md file there 3 <img alt="star" src="ressources/star.png" /></p>' in contents, f"customer.star is not in index or not rendering as expected"
2727
assert f"Welcome to {customer.get('web_url')}" in contents, f"customer.web_url is not in index"
28+
assert f"{{#binding.path}}" in contents, f"Jinja2 comment syntax wasn't reconfigured via jinja_options as expected"
2829
assert isinstance(test_json_string, str), "test_json_string is not a str it should be"
2930
assert '{"name": "Bob"}' == test_json_string, f"Json string is not correct"

0 commit comments

Comments
 (0)