From 7a25318dafa992bf061a588811df6d6b5abee77e Mon Sep 17 00:00:00 2001 From: Riley Ho Date: Sat, 4 Oct 2025 22:23:39 +0800 Subject: [PATCH 1/5] fix: escape markdown in fixed links to prevent formatting issues --- cogs/link_fix.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cogs/link_fix.py b/cogs/link_fix.py index 8f2c225..518f402 100644 --- a/cogs/link_fix.py +++ b/cogs/link_fix.py @@ -123,7 +123,8 @@ async def send_fixed_links(fixed_links: list[str], guild: Guild, original_messag :return: None """ - messages = group_join(fixed_links, 2000) + fixed_escaped_links = list(map(discore.utils.escape_markdown, fixed_links)) + messages = group_join(fixed_escaped_links, 2000) if guild.reply_to_message: await discore.fallback_reply(original_message, messages.pop(0), silent=guild.reply_silently) From 22e55cd9e53f7f9aa3c88358bbb04a9a14e9bfe9 Mon Sep 17 00:00:00 2001 From: Riley Ho Date: Sun, 12 Oct 2025 11:05:19 +0800 Subject: [PATCH 2/5] refactor: remove unnecessary list conversion in escape_markdown --- cogs/link_fix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/link_fix.py b/cogs/link_fix.py index 518f402..4147e59 100644 --- a/cogs/link_fix.py +++ b/cogs/link_fix.py @@ -123,7 +123,7 @@ async def send_fixed_links(fixed_links: list[str], guild: Guild, original_messag :return: None """ - fixed_escaped_links = list(map(discore.utils.escape_markdown, fixed_links)) + fixed_escaped_links = map(discore.utils.escape_markdown, fixed_links) messages = group_join(fixed_escaped_links, 2000) if guild.reply_to_message: From 811e10c7cc12ea3de70cfb3e6ce5744e5a7d359c Mon Sep 17 00:00:00 2001 From: Riley Ho Date: Mon, 13 Oct 2025 12:10:00 +0800 Subject: [PATCH 3/5] fix: only escape markdown in link labels, not URLs --- cogs/link_fix.py | 3 +-- src/websites.py | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cogs/link_fix.py b/cogs/link_fix.py index 4147e59..8f2c225 100644 --- a/cogs/link_fix.py +++ b/cogs/link_fix.py @@ -123,8 +123,7 @@ async def send_fixed_links(fixed_links: list[str], guild: Guild, original_messag :return: None """ - fixed_escaped_links = map(discore.utils.escape_markdown, fixed_links) - messages = group_join(fixed_escaped_links, 2000) + messages = group_join(fixed_links, 2000) if guild.reply_to_message: await discore.fallback_reply(original_message, messages.pop(0), silent=guild.reply_silently) diff --git a/src/websites.py b/src/websites.py index a108fe9..dbf2188 100644 --- a/src/websites.py +++ b/src/websites.py @@ -6,6 +6,7 @@ from typing import Optional, Self, Type, Iterable, Callable import aiohttp +import discore from database.models.Guild import * @@ -115,10 +116,10 @@ async def render(self) -> Optional[str]: return None author_url, author_label = await self.get_author_url() original_url, original_label = await self.get_original_url() - fixed_link = f"[{original_label}](<{original_url}>)" + fixed_link = markdown_link(original_label, original_url) if author_url: - fixed_link += f" • [{author_label}](<{author_url}>)" - fixed_link += f" • [{fixed_label}]({fixed_url})" + fixed_link += f" • {markdown_link(author_label, author_url)}" + fixed_link += f" • {markdown_link(fixed_label, fixed_url)}" return fixed_link @@ -270,6 +271,17 @@ async def get_original_url(self) -> tuple[Optional[str], Optional[str]]: return original_url, self.hypertext_label +def markdown_link(label: str, url: str) -> str: + """ + Create a markdown link with escaped label to prevent formatting issues. + + :param label: The label text for the link + :param url: The URL for the link + :return: A markdown formatted link with escaped label + """ + return f"[{discore.utils.escape_markdown(label)}](<{url}>)" + + def generate_regex(domain_names: str|list[str], route: str, params: Optional[list[str]] = None) -> re.Pattern[str]: """ Generate a regex for the corresponding route, with named groups. From ac500141788a257754c1ada5a2dd0ac41e6b47ba Mon Sep 17 00:00:00 2001 From: Riley Ho Date: Mon, 13 Oct 2025 14:32:13 +0800 Subject: [PATCH 4/5] fix: add embed parameter to markdown_link to control URL wrapping --- src/websites.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/websites.py b/src/websites.py index dbf2188..af3b310 100644 --- a/src/websites.py +++ b/src/websites.py @@ -116,7 +116,7 @@ async def render(self) -> Optional[str]: return None author_url, author_label = await self.get_author_url() original_url, original_label = await self.get_original_url() - fixed_link = markdown_link(original_label, original_url) + fixed_link = markdown_link(original_label, original_url, embed=False) if author_url: fixed_link += f" • {markdown_link(author_label, author_url)}" fixed_link += f" • {markdown_link(fixed_label, fixed_url)}" @@ -271,15 +271,16 @@ async def get_original_url(self) -> tuple[Optional[str], Optional[str]]: return original_url, self.hypertext_label -def markdown_link(label: str, url: str) -> str: +def markdown_link(label: str, url: str, embed=True) -> str: """ Create a markdown link with escaped label to prevent formatting issues. :param label: The label text for the link :param url: The URL for the link + :param embed: Whether the link is to be embedded (enclosed in <>) or not :return: A markdown formatted link with escaped label """ - return f"[{discore.utils.escape_markdown(label)}](<{url}>)" + return f"[{discore.utils.escape_markdown(label)}]({f'<{url}>' if embed else url})" def generate_regex(domain_names: str|list[str], route: str, params: Optional[list[str]] = None) -> re.Pattern[str]: From f5a00ccd565c606258b0a57bd0e220123104d8da Mon Sep 17 00:00:00 2001 From: Riley Ho Date: Mon, 13 Oct 2025 21:21:32 +0800 Subject: [PATCH 5/5] fix: invert embed logic to wrap non-embedded URLs in angle brackets --- src/websites.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/websites.py b/src/websites.py index af3b310..ecc6747 100644 --- a/src/websites.py +++ b/src/websites.py @@ -280,7 +280,7 @@ def markdown_link(label: str, url: str, embed=True) -> str: :param embed: Whether the link is to be embedded (enclosed in <>) or not :return: A markdown formatted link with escaped label """ - return f"[{discore.utils.escape_markdown(label)}]({f'<{url}>' if embed else url})" + return f"[{discore.utils.escape_markdown(label)}]({url if embed else f'<{url}>'})" def generate_regex(domain_names: str|list[str], route: str, params: Optional[list[str]] = None) -> re.Pattern[str]: