From d247f39d0b5bfade3593cdc97c7c2fef9ccb5b1b Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 15 Sep 2023 18:05:28 +0100 Subject: [PATCH] Allow reading diagrams from file --- content/nested/diagram.mmd | 11 +++++++++++ content/nested/index.md | 4 ++++ hooks/include-file.py | 19 +++++++++++++++++++ hooks/kroki.py | 17 ++++++++++++----- mkdocs.yml | 1 + 5 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 content/nested/diagram.mmd create mode 100644 hooks/include-file.py diff --git a/content/nested/diagram.mmd b/content/nested/diagram.mmd new file mode 100644 index 0000000..b57c7aa --- /dev/null +++ b/content/nested/diagram.mmd @@ -0,0 +1,11 @@ +sequenceDiagram + participant Alice + participant Bob + Alice->John: Hello John, how are you? + loop Healthcheck + John->John: Fight against hypochondria + end + Note right of John: Rational thoughts prevail... + John-->Alice: Great! + John->Bob: How about you? + Bob-->John: Jolly good! diff --git a/content/nested/index.md b/content/nested/index.md index 0d61f29..0df712b 100644 --- a/content/nested/index.md +++ b/content/nested/index.md @@ -4,3 +4,7 @@ tags: - tag2 --- # Nested + +```mermaid +{% include_file "./diagram.mmd" %} +``` diff --git a/hooks/include-file.py b/hooks/include-file.py new file mode 100644 index 0000000..ed3621b --- /dev/null +++ b/hooks/include-file.py @@ -0,0 +1,19 @@ +from jinja2_simple_tags import StandaloneTag +import os + +def on_env(env, config, files): + env.add_extension(IncludeFileTag) + + return env + +class IncludeFileTag(StandaloneTag): + tags = {"include_file"} + + def render(self, path): + if path.startswith("/"): + file_path = os.path.join(self.context["config"]["site_dir"], path.removeprefix("/")) + else: + file_path = os.path.join(os.path.dirname(self.context["page"].file.abs_src_path), path) + + with open(file_path) as f: + return f.read() diff --git a/hooks/kroki.py b/hooks/kroki.py index a8bcbaf..05ebf0e 100644 --- a/hooks/kroki.py +++ b/hooks/kroki.py @@ -47,11 +47,18 @@ def on_config(config): opener.addheaders = [('User-Agent','mkdocs')] install_opener(opener) - custom_fences = [{ - "name": "blockdiag", - "class": "blockdiag", - "format": partial(fence_div_format, config["site_dir"]) - }] + custom_fences = [ + { + "name": "blockdiag", + "class": "blockdiag", + "format": partial(fence_div_format, config["site_dir"]) + }, + { + "name": "mermaid", + "class": "mermaid", + "format": partial(fence_div_format, config["site_dir"]) + } + ] config.mdx_configs["pymdownx.superfences"] = { "custom_fences": custom_fences diff --git a/mkdocs.yml b/mkdocs.yml index 5ba3acc..5084d41 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -18,6 +18,7 @@ hooks: - hooks/globals.py - hooks/pygments-styles.py - hooks/kroki.py + - hooks/include-file.py markdown_extensions: - attr_list