From be1da5eb49d97777b151bc95675742d99e5c70f5 Mon Sep 17 00:00:00 2001 From: Frisk Date: Sat, 25 May 2024 12:21:30 +0200 Subject: [PATCH] Added escaping algorithm based on MarkusRost's code snipped, fixed replace in discussions.py --- extensions/base/discussions.py | 2 +- src/api/util.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/extensions/base/discussions.py b/extensions/base/discussions.py index f75323e..06587c3 100644 --- a/extensions/base/discussions.py +++ b/extensions/base/discussions.py @@ -345,7 +345,7 @@ def compact_discussion_article_comment(ctx: Context, post: dict): article_paths = ctx.comment_page if article_paths is None: article_paths = {"title": ctx._("unknown"), "fullUrl": ctx.settings["fandom_discussions"]["wiki_url"]} # No page known - article_paths["fullUrl"] = article_paths["fullUrl"].replace(")", "\)").replace("()", "\(") + article_paths["fullUrl"] = article_paths["fullUrl"].replace(")", "\\)").replace("()", "\\(") if not post["isReply"]: event_type = "discussion/comment/post" message = ctx._( diff --git a/src/api/util.py b/src/api/util.py index e87fa5e..51cf8a2 100644 --- a/src/api/util.py +++ b/src/api/util.py @@ -42,7 +42,12 @@ def clean_link(link: str) -> str: def sanitize_to_markdown(text: str) -> str: """Sanitizes given text to escape markdown formatting. It is used in values that will be visible on Discord in messages""" - return re.sub(r"([`_*~:<>{}@|#\-\.\\])", "\\\\\\1", text).replace('//', "/\\/").replace('](', "]\\(") + text = text.replace("\\", "\\\\").replace('/\\/', '\\').replace("](", "]\\(") # replace escaping and links + text = re.sub(r"([`_*~:<>{}@|])", "\\\\\\1", text) # Escape common Markdown characters + text = re.sub(r"^#+ ", '\\\\\\g<0>', text) # Escape headers + text = re.sub(r"^(\s*)- ", '\\1\\- ', text) # Escape lists + text = re.sub(r"^(\s*\d+)\. ", "\\1\\. ", text) # Escape numbered lists + return text def sanitize_to_url(text: str) -> str: # TODO ) replaces needed?