diff --git a/README.md b/README.md index a573c4f..125cc5b 100644 --- a/README.md +++ b/README.md @@ -48,10 +48,12 @@ plugins: Use code-fences with a tag of kroki-`` to replace the code with the wanted diagram. +[Diagram options](https://docs.kroki.io/kroki/setup/diagram-options/) can be set as well. + Example for BlockDiag: ````markdown -```kroki-blockdiag +```kroki-blockdiag no-transparency=false blockdiag { blockdiag -> generates -> "block-diagrams"; blockdiag -> is -> "very easy!"; diff --git a/kroki/client.py b/kroki/client.py index 216dbe1..970fb3c 100644 --- a/kroki/client.py +++ b/kroki/client.py @@ -29,31 +29,34 @@ def _kroki_uri(self, kroki_type): file_type = self.diagram_types.get_file_ext(kroki_type) return f'{self.server_url}/{kroki_type}/{file_type}' - def _get_url(self, kroki_type, kroki_diagram_data): + def _get_url(self, kroki_type, kroki_diagram_data, kroki_diagram_options={}): kroki_data_param = \ base64.urlsafe_b64encode( zlib.compress(str.encode(kroki_diagram_data), 9)).decode() - + + kroki_query_param = \ + "&".join([ f'{k}={v}' for k, v in kroki_diagram_options.items()]) if len(kroki_diagram_options)>0 else '' if len(kroki_data_param) >= 4096: debug(f'Length of encoded diagram is {len(kroki_data_param)}. ' 'Kroki may not be able to read the data completely!') kroki_uri = self._kroki_uri(kroki_type) - return f'{kroki_uri}/{kroki_data_param}' + error(f'{kroki_uri}/{kroki_data_param}?{kroki_query_param}') + return f'{kroki_uri}/{kroki_data_param}?{kroki_query_param}' - def get_url(self, kroki_type, kroki_diagram_data): + def get_url(self, kroki_type, kroki_diagram_data,kroki_diagram_options={}): debug(f'get_url: {kroki_type}') if self.http_method != 'GET': error(f'HTTP method is {self.http_method}. Config error!') return None - return self._get_url(kroki_type, kroki_diagram_data) + return self._get_url(kroki_type, kroki_diagram_data, kroki_diagram_options) - def get_image_data(self, kroki_type, kroki_diagram_data): + def get_image_data(self, kroki_type, kroki_diagram_data, kroki_diagram_options={}): try: if self.http_method == 'GET': - url = self._get_url(kroki_type, kroki_diagram_data) + url = self._get_url(kroki_type, kroki_diagram_data, kroki_diagram_options) debug(f'get_image_data [GET {url[:50]}..]') r = requests.get(url) @@ -63,7 +66,8 @@ def get_image_data(self, kroki_type, kroki_diagram_data): debug(f'get_image_data [POST {url}]') r = requests.post(url, json={ - "diagram_source": kroki_diagram_data + "diagram_source": kroki_diagram_data, + "diagram_options": kroki_diagram_options }) debug(f'get_image_data [Response: {r}]') diff --git a/kroki/config.py b/kroki/config.py index 615d879..faf64e6 100644 --- a/kroki/config.py +++ b/kroki/config.py @@ -70,7 +70,7 @@ def __init__(self, blockdiag_enabled, bpmn_enabled, excalidraw_enabled, def get_block_regex(self, fence_prefix): diagram_types_re = "|".join(self.diagram_types_supporting_file.keys()) - return rf'(?:```{fence_prefix})({diagram_types_re})\n(.*?)(?:```)' + return rf'(?:```{fence_prefix})({diagram_types_re})((?:\s?[a-zA-Z0-9\-_]+=[a-zA-Z0-9\-_]+)*)\n(.*?)(?:```)' def get_file_ext(self, kroki_type): return self.diagram_types_supporting_file[kroki_type] diff --git a/kroki/plugin.py b/kroki/plugin.py index 1b17e4d..84c0c42 100644 --- a/kroki/plugin.py +++ b/kroki/plugin.py @@ -9,6 +9,7 @@ from mkdocs.plugins import log from pathlib import Path from os.path import relpath +import os from .config import KrokiDiagramTypes from .client import KrokiClient @@ -21,7 +22,7 @@ class KrokiPlugin(BasePlugin): config_scheme = ( - ('ServerURL', config.config_options.Type(str, default='https://kroki.io')), + ('ServerURL', config.config_options.Type(str, default=os.getenv('KROKI_SERVER_URL','https://kroki.io'))), ('EnableBlockDiag', config.config_options.Type(bool, default=True)), ('Enablebpmn', config.config_options.Type(bool, default=True)), ('EnableExcalidraw', config.config_options.Type(bool, default=True)), @@ -93,17 +94,19 @@ def _save_kroki_image_and_get_url(self, file_name, image_data, files): def _replace_kroki_block(self, match_obj, files, page): kroki_type = match_obj.group(1).lower() - kroki_data = match_obj.group(2) + kroki_options = match_obj.group(2) + kroki_data = match_obj.group(3) + kroki_diagram_options=dict(x.split('=') for x in kroki_options.strip().split(' ')) if kroki_options else {} get_url = None if self.config["DownloadImages"]: - image_data = self.kroki_client.get_image_data(kroki_type, kroki_data) + image_data = self.kroki_client.get_image_data(kroki_type, kroki_data, kroki_diagram_options) if image_data: file_name = self._kroki_filename(kroki_data, kroki_type, page) get_url = self._save_kroki_image_and_get_url(file_name, image_data, files) else: - get_url = self.kroki_client.get_url(kroki_type, kroki_data) + get_url = self.kroki_client.get_url(kroki_type, kroki_data, kroki_diagram_options) if get_url is not None: return f'![Kroki]({get_url})'