fix: 修复图片提取逻辑避免重复添加

1. 只提取原始图片 URL,不提取缩略图
2. 有 markdown 格式时不再匹配普通 URL

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Your Name 2026-02-01 12:47:55 +08:00
parent e0366c43fd
commit 3bac27265b

View File

@ -33,24 +33,31 @@ interface CanvasChatPanelProps {
// 建议卡片数据 - 已移除
const SUGGESTIONS: { title: string; desc: string; image: string }[] = [];
// 从消息中提取图片 URL
// 从消息中提取图片 URL(只提取原始图片,避免重复)
function extractImageUrls(text: string): string[] {
const urls: string[] = [];
// 匹配 markdown 图片格式: ![alt|||originalUrl|||size](thumbnailUrl)
const markdownRegex = /!\[(?:thumb)?\|\|\|([^|]+)\|\|\|[^\]]*\]\([^)]+\)/g;
// 匹配 markdown 图片格式: ![thumb|||originalUrl|||size](thumbnailUrl)
// 只提取原始图片 URL(第一个 ||| 和第二个 ||| 之间的 URL)
const markdownRegex = /!\[(?:thumb)?\|\|\|([^|]+)\|\|\|[^\]]*\]\(([^)]+)\)/g;
let match;
let hasMarkdownImages = false;
while ((match = markdownRegex.exec(text)) !== null) {
if (match[1]) {
urls.push(match[1]);
urls.push(match[1]); // 原始图片 URL
hasMarkdownImages = true;
}
}
// 匹配普通图片 URL
const urlRegex = /https?:\/\/[^\s"'<>]+\.(?:jpg|jpeg|png|gif|webp)/gi;
const urlMatches = text.match(urlRegex);
if (urlMatches) {
urls.push(...urlMatches.filter(url => !urls.includes(url)));
// 只有在没有 markdown 格式图片时,才匹配普通图片 URL
// 这样可以避免重复匹配缩略图 URL
if (!hasMarkdownImages) {
const urlRegex = /https?:\/\/[^\s"'<>]+\.(?:jpg|jpeg|png|gif|webp)/gi;
const urlMatches = text.match(urlRegex);
if (urlMatches) {
urls.push(...urlMatches.filter(url => !urls.includes(url)));
}
}
return urls;