From fa3cfce34af4acfc362bf990ef3e6a2fcebe89a1 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 15 Sep 2023 17:41:31 +0100 Subject: [PATCH] Compress and filter SVGs before saving --- hooks/kroki.py | 6 +++++- requirements.txt | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/hooks/kroki.py b/hooks/kroki.py index c05de6f..a8bcbaf 100644 --- a/hooks/kroki.py +++ b/hooks/kroki.py @@ -8,6 +8,8 @@ import hashlib from functools import partial from mkdocs.utils import write_file import os +from py_svg_hush import filter_svg +from scour.scour import scourString logger = get_plugin_logger("kroki") @@ -16,7 +18,9 @@ DIAGRAM_CACHE_TIME = timedelta(days=7) def get_kroki_diagram(source, language): encoded_diagram = base64.urlsafe_b64encode(zlib.compress(source.encode(), 9)).decode() - return download_and_cache_url(f"https://kroki.io/{language}/svg/{encoded_diagram}", DIAGRAM_CACHE_TIME) + svg = download_and_cache_url(f"https://kroki.io/{language}/svg/{encoded_diagram}", DIAGRAM_CACHE_TIME) + + return scourString(filter_svg(svg)).encode() def fence_div_format(site_dir, source, language, *args, attrs, **kwargs): diff --git a/requirements.txt b/requirements.txt index 06bdea3..41b807c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,5 @@ pymdown-extensions pygments minify-html beautifulsoup4 +py-svg-hush +scour