Added escaping algorithm based on MarkusRost's code snipped, fixed replace in discussions.py

This commit is contained in:
Frisk 2024-05-25 12:21:30 +02:00
parent d4d50d3529
commit be1da5eb49
2 changed files with 7 additions and 2 deletions

View file

@ -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._(

View file

@ -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?