From 8da317e061d7294bd2849acf91c5c3bca0f203e2 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 23 Sep 2023 22:19:55 +0100 Subject: [PATCH] Dynamically get supported kroki languages --- hooks/kroki.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/hooks/kroki.py b/hooks/kroki.py index 5cf1b15..5e2eec4 100644 --- a/hooks/kroki.py +++ b/hooks/kroki.py @@ -11,14 +11,10 @@ import os from py_svg_hush import filter_svg from scour.scour import scourString from slugify import slugify +import json logger = get_plugin_logger("kroki") -DIAGRAM_TYPES = { - "blockdiag", - "mermaid" -} - DIAGRAM_CACHE_TIME = timedelta(days=7) @@ -62,13 +58,22 @@ def fence_div_format(site_dir, source, language, *args, attrs, **kwargs): return f"{title}" def on_config(config): + kroki_langs = [ + lang + for lang, version + in json.loads(download_and_cache_url("https://kroki.io/health", DIAGRAM_CACHE_TIME))["version"].items() + if isinstance(version, str) + ] + + format_kroki_diagram = partial(fence_div_format, config["site_dir"]) + custom_fences = [ { "name": diagram, "class": diagram, - "format": partial(fence_div_format, config["site_dir"]) + "format": format_kroki_diagram } - for diagram in DIAGRAM_TYPES + for diagram in kroki_langs ] config.mdx_configs["pymdownx.superfences"] = {