timvink / mkdocs-table-reader-plugin

MkDocs plugin that enables a markdown tag like {{ read_csv('table.csv') }} to directly insert various table formats into a page
https://timvink.github.io/mkdocs-table-reader-plugin/
MIT License
124 stars 20 forks source link

Experiment with jinja2 env instead of markdown replacement #22

Closed timvink closed 2 years ago

timvink commented 2 years ago

Related to #14 and #20 , a different approach might work better: add macros to the jinja2 environment.

timvink commented 2 years ago

The mkdocs-markdownextradata-plugin takes a different approach and creates its own jinja2 environment, which it applies during on_page_markdown (link).

That means it will give TemplatingErrors for any jinja tags or macros that are not defined. We would have the same problem..

timvink commented 2 years ago

A specific problem we have in #21 is that a tag like {{read_csv('path')}} is a valid jinja2 tag but not replaced by table_reader because of the missing space padding. Any other plugin that then uses jinja2 will complain about not knowing about the read_csv macro.

A more robust approach might be to make the regex replacement more robust and not enforce space padding:

https://github.com/timvink/mkdocs-table-reader-plugin/blob/1a35ceed87fdcdfe617cf27ebb47c6cd2841dc28/mkdocs_table_reader_plugin/plugin.py#L114-L119

timvink commented 2 years ago

Another alternative is using a completely different syntax, like ||| read_csv() ||| but that's not user friendly.

timvink commented 2 years ago

Addressed in https://github.com/timvink/mkdocs-table-reader-plugin/pull/21/commits/281de79acf6e13471abdbca7121ad23b8a37f1b7