fix: 下载类回复保留Markdown链接格式

问题:
- _clean_markdown()把[文本](URL)转换成纯文本,丢失URL
- 用户问"登录插件下载地址"时,回复变成"官方下载 bbsmc分流"
- 实际应该是"[官方下载](https://...) [bbsmc分流](https://...)"

修复:
- 新增 _clean_markdown(keep_links=False) 参数
- 检测下载意图(含下载/地址关键词 + 含URL)
- 下载类回复保留原始Markdown链接格式

效果:
- 非下载类:仍清理Markdown(普通问答)
- 下载类:保留[文本](URL)格式(含可点击链接)

相关issue: #1542
This commit is contained in:
youpaishidifu
2026-08-16 22:37:07 +08:00
parent b3f1ecfd61
commit 3bfdbff40c
+23 -6
View File
@@ -377,8 +377,18 @@ class AutoReply:
answer = re.sub(r'</?br\s*/?>', '\n', answer, flags=re.IGNORECASE)
answer = answer.strip()
# 清理 Markdown 格式,返回纯文本
answer = self._clean_markdown(answer)
# ⭐ 下载类回复保留 Markdown 链接:检测是否含下载/地址关键词且含 URL
_is_download_intent = (
any(kw in answer for kw in ('下载', '地址', '链接'))
or any(kw in msg for kw in ('下载', '下载地址', '去哪下'))
)
_has_url = bool(re.search(r'https?://', answer))
_keep_links = _is_download_intent and _has_url
if _keep_links:
logger.debug(f"[{platform}] 下载意图,保留链接格式")
# 清理 Markdown 格式,返回纯文本(下载类保留链接)
answer = self._clean_markdown(answer, keep_links=_keep_links)
if not answer:
return None
@@ -440,16 +450,18 @@ class AutoReply:
return answer
@staticmethod
def _clean_markdown(text: str) -> str:
def _clean_markdown(text: str, keep_links: bool = False) -> str:
"""清理文本中的 Markdown 格式,返回纯文本。
处理:
- 标题语法:## 标题 → 标题
- 链接语法:[text](url) → text
- 链接语法:[text](url) → text(keep_links=False)或保留原样(keep_links=True)
- 列表语法:- item → item
- 加粗语法:**text** → text
- 引用语法:> text → text
- 编号列表:1. item → item
keep_links: 是否保留 Markdown 链接格式。用于下载类回复,需要保留URL。
"""
import re
if not text:
@@ -458,8 +470,13 @@ class AutoReply:
# 移除标题语法:## 标题 → 标题
text = re.sub(r'^#{1,6}\s+', '', text, flags=re.MULTILINE)
# 移除链接语法:[text](url) → text
text = re.sub(r'\[([^\]]*)\]\([^\)]+\)', r'\1', text)
# 链接处理:根据 keep_links 参数决定是否保留
if keep_links:
# 保留链接格式:[text](url) → [text](url)
pass
else:
# 移除链接语法:[text](url) → text
text = re.sub(r'\[([^\]]*)\]\([^\)]+\)', r'\1', text)
# 移除列表语法:- item → item
text = re.sub(r'^[\-\*]\s+', '', text, flags=re.MULTILINE)