fix: label citation links by their actual target
This commit is contained in:
+15
-2
@@ -84,6 +84,17 @@ const safeUrl = (raw: unknown) => {
|
||||
try { const url = new URL(raw, window.location.origin); return ['http:', 'https:'].includes(url.protocol) ? url.href : undefined; }
|
||||
catch { return undefined; }
|
||||
};
|
||||
const isImageUrl = (href: string) => {
|
||||
const path = new URL(href).pathname;
|
||||
return /^\/api\/assets\/\d+\/\d+\/?$/.test(path) || /\.(?:png|jpe?g|gif|webp|avif|svg)$/i.test(path);
|
||||
};
|
||||
const sourceLabel = (href: string) => {
|
||||
const url = new URL(href);
|
||||
if (isImageUrl(href)) return '查看图像';
|
||||
if (/^\/api\/knowledge\/documents\/\d+\/pages\/\d+\/?$/.test(url.pathname)) return '查看页面文字';
|
||||
if (url.pathname === '/' && url.searchParams.get('page') === 'scores') return '查看评分';
|
||||
return '打开来源';
|
||||
};
|
||||
function Answer({ text }: { text: string }) {
|
||||
return <div className="chat-markdown"><ReactMarkdown remarkPlugins={[remarkGfm]} urlTransform={url => safeUrl(url) || ''}
|
||||
components={{ a: ({ href, children }) => safeUrl(href) ? <a href={safeUrl(href)} target="_blank" rel="noopener noreferrer">{children}</a> : <span>{children}</span>,
|
||||
@@ -94,10 +105,12 @@ function CitationCards({ items }: { items: any[] }) {
|
||||
return <div className="citation-grid">{items.map((item: any, index) => { const name = String(item.title || item.document || '未命名资料');
|
||||
const page = item.page ?? item.slide; const place = page == null ? '' : `第 ${page} ${/\.pptx?$/i.test(name) ? '张幻灯片' : '页'}`;
|
||||
const quote = typeof item.quote === 'string' ? item.quote.trim() : '';
|
||||
const sourceHref = safeUrl(item.url);
|
||||
const imageHref = safeUrl(item.image_url);
|
||||
return <div className="citation-card" key={`${name}-${page}-${index}`}><div className="citation-heading"><Text strong>{name}</Text>{place && <Tag>{place}</Tag>}</div>
|
||||
{quote && (quote.length > 320 ? <details><summary>{quote.slice(0, 240)}… 展开原文</summary><p>{quote}</p></details> : <p>{quote}</p>)}
|
||||
<Space wrap size="small">{safeUrl(item.url) && <a href={safeUrl(item.url)} target="_blank" rel="noopener noreferrer">打开页面</a>}
|
||||
{safeUrl(item.image_url) && <a href={safeUrl(item.image_url)} target="_blank" rel="noopener noreferrer">查看图像</a>}</Space></div>;
|
||||
<Space wrap size="small">{sourceHref && <a href={sourceHref} target="_blank" rel="noopener noreferrer">{sourceLabel(sourceHref)}</a>}
|
||||
{imageHref && imageHref !== sourceHref && <a href={imageHref} target="_blank" rel="noopener noreferrer">查看图像</a>}</Space></div>;
|
||||
})}</div>;
|
||||
}
|
||||
function Citations({ items }: { items: any[] }) {
|
||||
|
||||
Reference in New Issue
Block a user