Repository: rosscdh/mkdocs-markdownextradata-plugin

Issue:
Feature Request: Support Jinja2 config from mkdocs.yml
I just ran into an issue where the Markdown source docs would contain Jinja2-like syntax that lead to errors, more specifically the following:

`{#someVariableBindingString}`

This is misinterpreted (even though it was in a code block) as a Jinja2 comment due to the initial `{#`. I see two options here:

1. Make the plugin syntax-aware and ignore variables in code blocks
1. Support feeding in Jinja2 options as shown in [different delimiters in jinja2](https://gist.github.com/lost-theory/3925738) and [API](https://jinja.palletsprojects.com/en/2.11.x/api/#high-level-api)

I believe 1) to be more of a feature than an issue, and 2) appears like it would enable everyone to customize this plugin to their heart's content. I believe, therefore, that 2) is the way to go.

Will open a PR in a few minutes with a draft.

Discussion:

Patch:
diff --git a/README.md b/README.md
index a5913f4..1766c43 100644
--- a/README.md
+++ b/README.md
@@ -102,7 +102,19 @@ If inside your data folder you have a directory and a file file
 called `[path/to/datafiles/]sections/captions.yaml` - where `[path/to/datafiles/]` is the path in your configuration -
 the data inside that file will be available in your templates as `sections.captions.whatever_variable_in_the_yaml`.
 
+### Jinja2 Template Engine Configuration
 
+You may provide [Jinja2 configuration](https://jinja.palletsprojects.com/en/2.11.x/api/#high-level-api) as plugin options:
+
+```yml
+plugins:
+    - markdownextradata:
+        jinja_options:
+          comment_start_string: __CUSTOMCOMMENTSTART__
+```
+
+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
+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 `{{ }}`).
 
 ## Testing
 
diff --git a/markdownextradata/plugin.py b/markdownextradata/plugin.py
index 5ad5e2e..5433413 100644
--- a/markdownextradata/plugin.py
+++ b/markdownextradata/plugin.py
@@ -27,8 +27,11 @@ class MarkdownExtraDataPlugin(BasePlugin):
     Inject certain config variables into the markdown
     """
 
+    JINJA_OPTIONS = "jinja_options"
+
     config_scheme = (
         ("data", mkdocs.config.config_options.Type(str_type, default=None)),
+        (JINJA_OPTIONS, mkdocs.config.config_options.Type(dict, default={}))
     )
 
     def __add_data__(self, config, namespace, data):
@@ -91,6 +94,7 @@ class MarkdownExtraDataPlugin(BasePlugin):
     def on_page_markdown(self, markdown, config, **kwargs):
         context = {key: config.get(key) for key in CONFIG_KEYS if key in config}
         context.update(config.get("extra", {}))
-        env = jinja2.Environment(undefined=jinja2.DebugUndefined)
+        jinja_options = self.config[self.JINJA_OPTIONS]
+        env = jinja2.Environment(undefined=jinja2.DebugUndefined, **jinja_options)
         md_template = env.from_string(markdown)
         return md_template.render(**config.get("extra"))


Test patch:
diff --git a/test/docs/index.md b/test/docs/index.md
index 10cb91a..cdc28b4 100644
--- a/test/docs/index.md
+++ b/test/docs/index.md
@@ -2,4 +2,7 @@
 
 Welcome to {{ customer.web_url }}
 
-Inside the included md file there 3 {{ star }}
\ No newline at end of file
+Inside the included md file there 3 {{ star }}
+
+<!-- throws TemplateSyntaxError('Missing end of comment tag') unless comment_start_string is configured -->
+You can use `{#binding.path}` to update the UI when the model changes.
diff --git a/test/mkdocs.yml b/test/mkdocs.yml
index 5a27c0d..7f671f1 100644
--- a/test/mkdocs.yml
+++ b/test/mkdocs.yml
@@ -5,7 +5,9 @@ use_directory_urls: true
 #     custom_dir: 'theme/'
 plugins:
     - search
-    - markdownextradata
+    - markdownextradata:
+        jinja_options:
+          comment_start_string: __COMMENTSTART__
 
 extra:
   star: "![star](ressources/star.png)"
diff --git a/test/test_basic.py b/test/test_basic.py
index 5af748d..f544540 100644
--- a/test/test_basic.py
+++ b/test/test_basic.py
@@ -23,7 +23,8 @@ def test_basic_working():
         contents = index_file.read_text()
 
         assert '<h1 id="hi-there-your-name-here">Hi there, Your name here</h1>' in contents, f"customer.name is not in index"
-        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"
+        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"
         assert f"Welcome to {customer.get('web_url')}" in contents, f"customer.web_url is not in index"
+        assert f"{{#binding.path}}" in contents, f"Jinja2 comment syntax wasn't reconfigured via jinja_options as expected"
         assert isinstance(test_json_string, str), "test_json_string is not a str it should be"
         assert '{"name": "Bob"}' == test_json_string, f"Json string is not correct"