diff --git a/app/modules/telegram/telegram.py b/app/modules/telegram/telegram.py index cefe793f..0baf64ed 100644 --- a/app/modules/telegram/telegram.py +++ b/app/modules/telegram/telegram.py @@ -633,52 +633,68 @@ class Telegram: if not any(char in self._escape_chars for char in text): return text - # 标记受保护的区域(Markdown标记内的内容不转义) + # 标记受保护的位置(只保护Markdown分隔符本身,不保护内容区域) protected = [False] * len(text) # 按优先级匹配Markdown标记(从最复杂到最简单) - # 1. 链接:[text](url) - 必须最先匹配 + # 1. 链接:[text](url) - 必须最先匹配,只保护分隔符 [ ] ( ) link_pattern = r'\[([^\]]*)\]\(([^)]*)\)' for match in re.finditer(link_pattern, text): - for i in range(match.start(), match.end()): - protected[i] = True + # 只保护分隔符:[, ], (, ) + protected[match.start()] = True # [ + # match.end(1) 是第一个捕获组结束位置,即 ] 的位置 + protected[match.end(1)] = True # ] + # ( 在 ] 之后一个字符 + if match.end(1) + 1 < len(text): + protected[match.end(1) + 1] = True # ( + # ) 在匹配结束前一个字符 + if match.end() > 0: + protected[match.end() - 1] = True # ) - # 2. 粗体:*text*(单个*,不是**) + # 2. 粗体:*text*(单个*,不是**),只保护分隔符 * bold_pattern = r'(?