From 0a7b3305326380328fbfa4e00e9b96669b052b88 Mon Sep 17 00:00:00 2001 From: youpaishidifu Date: Tue, 11 Aug 2026 16:27:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=87=AA=E5=8A=A8=E5=9B=9E=E5=A4=8D?= =?UTF-8?q?=E6=B8=85=E7=90=86Markdown=E6=A0=BC=E5=BC=8F=EF=BC=8C=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E8=BF=94=E5=9B=9EMD=E6=A0=BC=E5=BC=8F=E5=86=85?= =?UTF-8?q?=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增_clean_markdown函数,清理答案中的Markdown格式(标题、链接、列表、加粗等), 返回纯文本。修复KB搜索返回Markdown格式内容(如下载地址链接)的问题。 同步dist副本。 --- auto_reply.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/auto_reply.py b/auto_reply.py index 9f2ea7e..10f2621 100644 --- a/auto_reply.py +++ b/auto_reply.py @@ -198,6 +198,11 @@ class AutoReply: answer = re.sub(r'', '\n', answer, flags=re.IGNORECASE) answer = answer.strip() + # 清理 Markdown 格式,返回纯文本 + answer = self._clean_markdown(answer) + if not answer: + return None + # 拒绝把纯标题/免责声明当作答案发送 if answer.startswith("# ") and len(answer) < 60: logger.debug(f"[{platform}] 答案为纯标题,跳过: {answer[:60]!r}") @@ -223,6 +228,45 @@ class AutoReply: ) return answer + @staticmethod + def _clean_markdown(text: str) -> str: + """清理文本中的 Markdown 格式,返回纯文本。 + + 处理: + - 标题语法:## 标题 → 标题 + - 链接语法:[text](url) → text + - 列表语法:- item → item + - 加粗语法:**text** → text + - 引用语法:> text → text + - 编号列表:1. item → item + """ + import re + if not text: + return text + + # 移除标题语法:## 标题 → 标题 + text = re.sub(r'^#{1,6}\s+', '', text, flags=re.MULTILINE) + + # 移除链接语法:[text](url) → text + text = re.sub(r'\[([^\]]*)\]\([^\)]+\)', r'\1', text) + + # 移除列表语法:- item → item + text = re.sub(r'^[\-\*]\s+', '', text, flags=re.MULTILINE) + + # 移除加粗语法:**text** → text + text = re.sub(r'\*\*(.+?)\*\*', r'\1', text) + + # 移除引用语法:> text → text + text = re.sub(r'^>\s+', '', text, flags=re.MULTILINE) + + # 移除编号列表:1. item → item + text = re.sub(r'^\d{1,3}[\.\)、]\s+', '', text, flags=re.MULTILINE) + + # 移除多余空行 + text = re.sub(r'\n\s*\n', '\n', text) + + return text.strip() + @staticmethod def _extract_table_answer(text: str, max_chars: int = 800) -> str: """从含表格的文本中提取表格 + 紧邻上下文,避免返回整段 section。