fix: 自动回复清理Markdown格式,防止返回MD格式内容
新增_clean_markdown函数,清理答案中的Markdown格式(标题、链接、列表、加粗等), 返回纯文本。修复KB搜索返回Markdown格式内容(如下载地址链接)的问题。 同步dist副本。
This commit is contained in:
@@ -198,6 +198,11 @@ class AutoReply:
|
|||||||
answer = re.sub(r'</?br\s*/?>', '\n', answer, flags=re.IGNORECASE)
|
answer = re.sub(r'</?br\s*/?>', '\n', answer, flags=re.IGNORECASE)
|
||||||
answer = answer.strip()
|
answer = answer.strip()
|
||||||
|
|
||||||
|
# 清理 Markdown 格式,返回纯文本
|
||||||
|
answer = self._clean_markdown(answer)
|
||||||
|
if not answer:
|
||||||
|
return None
|
||||||
|
|
||||||
# 拒绝把纯标题/免责声明当作答案发送
|
# 拒绝把纯标题/免责声明当作答案发送
|
||||||
if answer.startswith("# ") and len(answer) < 60:
|
if answer.startswith("# ") and len(answer) < 60:
|
||||||
logger.debug(f"[{platform}] 答案为纯标题,跳过: {answer[:60]!r}")
|
logger.debug(f"[{platform}] 答案为纯标题,跳过: {answer[:60]!r}")
|
||||||
@@ -223,6 +228,45 @@ class AutoReply:
|
|||||||
)
|
)
|
||||||
return answer
|
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
|
@staticmethod
|
||||||
def _extract_table_answer(text: str, max_chars: int = 800) -> str:
|
def _extract_table_answer(text: str, max_chars: int = 800) -> str:
|
||||||
"""从含表格的文本中提取表格 + 紧邻上下文,避免返回整段 section。
|
"""从含表格的文本中提取表格 + 紧邻上下文,避免返回整段 section。
|
||||||
|
|||||||
Reference in New Issue
Block a user