fix: 修复图片提取逻辑避免重复添加
1. 只提取原始图片 URL,不提取缩略图 2. 有 markdown 格式时不再匹配普通 URL Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
e0366c43fd
commit
3bac27265b
@ -33,24 +33,31 @@ interface CanvasChatPanelProps {
|
|||||||
// 建议卡片数据 - 已移除
|
// 建议卡片数据 - 已移除
|
||||||
const SUGGESTIONS: { title: string; desc: string; image: string }[] = [];
|
const SUGGESTIONS: { title: string; desc: string; image: string }[] = [];
|
||||||
|
|
||||||
// 从消息中提取图片 URL
|
// 从消息中提取图片 URL(只提取原始图片,避免重复)
|
||||||
function extractImageUrls(text: string): string[] {
|
function extractImageUrls(text: string): string[] {
|
||||||
const urls: string[] = [];
|
const urls: string[] = [];
|
||||||
|
|
||||||
// 匹配 markdown 图片格式: 
|
// 匹配 markdown 图片格式: 
|
||||||
const markdownRegex = /!\[(?:thumb)?\|\|\|([^|]+)\|\|\|[^\]]*\]\([^)]+\)/g;
|
// 只提取原始图片 URL(第一个 ||| 和第二个 ||| 之间的 URL)
|
||||||
|
const markdownRegex = /!\[(?:thumb)?\|\|\|([^|]+)\|\|\|[^\]]*\]\(([^)]+)\)/g;
|
||||||
let match;
|
let match;
|
||||||
|
let hasMarkdownImages = false;
|
||||||
|
|
||||||
while ((match = markdownRegex.exec(text)) !== null) {
|
while ((match = markdownRegex.exec(text)) !== null) {
|
||||||
if (match[1]) {
|
if (match[1]) {
|
||||||
urls.push(match[1]);
|
urls.push(match[1]); // 原始图片 URL
|
||||||
|
hasMarkdownImages = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 匹配普通图片 URL
|
// 只有在没有 markdown 格式图片时,才匹配普通图片 URL
|
||||||
const urlRegex = /https?:\/\/[^\s"'<>]+\.(?:jpg|jpeg|png|gif|webp)/gi;
|
// 这样可以避免重复匹配缩略图 URL
|
||||||
const urlMatches = text.match(urlRegex);
|
if (!hasMarkdownImages) {
|
||||||
if (urlMatches) {
|
const urlRegex = /https?:\/\/[^\s"'<>]+\.(?:jpg|jpeg|png|gif|webp)/gi;
|
||||||
urls.push(...urlMatches.filter(url => !urls.includes(url)));
|
const urlMatches = text.match(urlRegex);
|
||||||
|
if (urlMatches) {
|
||||||
|
urls.push(...urlMatches.filter(url => !urls.includes(url)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return urls;
|
return urls;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user