From 19e9fa647cbff446da882d7bfc6398843c269826 Mon Sep 17 00:00:00 2001 From: Jamie Banks Date: Fri, 10 Jul 2026 20:15:36 +0100 Subject: [PATCH] feat: :sparkles: sanitise markdown --- app.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 8e4baf2..90f83eb 100644 --- a/app.py +++ b/app.py @@ -33,11 +33,16 @@ logger = logging.getLogger(__name__) app = Flask(__name__) +import re + def clean_markdown(text: str) -> str: """Remove Discord markdown blocks to make text more readable.""" if not isinstance(text, str): return "" - return text.replace("```json\n", "").replace("```\n", "").replace("```", "").strip() + text = text.replace("```json\n", "").replace("```\n", "").replace("```", "") + # Strip Discord bold, underline, and strikethrough formatting + text = re.sub(r'(\*\*|__|~~)', '', text) + return text.strip() def discord_payload_to_log_lines(payload: dict) -> list[str]: @@ -80,7 +85,9 @@ def discord_payload_to_log_lines(payload: dict) -> list[str]: continue name = field.get("name") value = field.get("value") - if isinstance(name, str) and isinstance(value, str) and name.strip(): + if isinstance(name, str) and name.strip() and value is not None: + if not isinstance(value, str): + value = str(value) cleaned_val = clean_markdown(value) try: fields_out[name.strip()] = json.loads(cleaned_val) @@ -99,6 +106,25 @@ def discord_payload_to_log_lines(payload: dict) -> list[str]: if not result: return [] + # Generate a summary message so Grafana doesn't show a blank log line + msg_parts = [] + if "content" in result and isinstance(result["content"], str): + msg_parts.append(result["content"]) + for e in embeds_out: + if "title" in e: + msg_parts.append(f"[{e['title']}]") + if "description" in e and isinstance(e["description"], str): + # Only append the first 100 characters of the description to keep it clean + desc = e["description"] + if len(desc) > 100: + desc = desc[:97] + "..." + msg_parts.append(desc) + + if msg_parts: + result["message"] = " - ".join(msg_parts) + else: + result["message"] = "Discord Webhook Event" + return [json.dumps(result)]