优化:按分析章节整合图表解读与完整报告导出
This commit is contained in:
+15
-10
@@ -1,15 +1,19 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { sanitizeAnalysisDisplayText } from './analysisDisplayText';
|
||||
|
||||
function inlineMarkdown(value: string, keyPrefix = 'inline'): ReactNode[] {
|
||||
function inlineMarkdown(value: string, keyPrefix = 'inline', evidenceLabels?: Record<string, string>, evidencePrefix = ''): ReactNode[] {
|
||||
const references = (part: string, key: string): ReactNode => !evidenceLabels ? part : part.split(/(\[?Q[A-F0-9]{8}\]?)/g).map((token, index) => {
|
||||
const ref = token.replace(/^\[|\]$/g, '');
|
||||
return evidenceLabels[ref] ? <a className="analysis-evidence-ref" key={`${key}-${index}`} href={`#${evidencePrefix}${ref}`} title={ref}>{evidenceLabels[ref]}</a> : token;
|
||||
});
|
||||
return value.split(/(\*\*.+?\*\*|`[^`]+`)/g).map((part, index) => {
|
||||
if (part.startsWith('**') && part.endsWith('**') && part.length >= 4) {
|
||||
return <strong key={`${keyPrefix}-${index}`}>{part.slice(2, -2)}</strong>;
|
||||
return <strong key={`${keyPrefix}-${index}`}>{references(part.slice(2, -2), `${keyPrefix}-${index}`)}</strong>;
|
||||
}
|
||||
if (part.startsWith('`') && part.endsWith('`') && part.length >= 2) {
|
||||
return <code key={`${keyPrefix}-${index}`}>{part.slice(1, -1)}</code>;
|
||||
}
|
||||
return part;
|
||||
return references(part, `${keyPrefix}-${index}`);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -33,7 +37,8 @@ function isBlockStart(lines: string[], index: number): boolean {
|
||||
}
|
||||
|
||||
/** Renders the small Markdown subset used by analysis answers with React text nodes only. */
|
||||
export function AiMarkdown({ content, className }: { content: string; className?: string }) {
|
||||
export function AiMarkdown({ content, className, evidenceLabels, evidencePrefix }: { content: string; className?: string; evidenceLabels?: Record<string, string>; evidencePrefix?: string }) {
|
||||
const inline = (value: string, key: string) => inlineMarkdown(value, key, evidenceLabels, evidencePrefix);
|
||||
const lines = sanitizeAnalysisDisplayText(content).replaceAll('\r\n', '\n').replaceAll('\r', '\n').split('\n');
|
||||
const blocks: ReactNode[] = [];
|
||||
let index = 0;
|
||||
@@ -56,7 +61,7 @@ export function AiMarkdown({ content, className }: { content: string; className?
|
||||
if (heading) {
|
||||
const level = heading[1].length;
|
||||
const key = `heading-${blocks.length}`;
|
||||
const children = inlineMarkdown(heading[2], key);
|
||||
const children = inline(heading[2], key);
|
||||
if (level === 1) blocks.push(<h1 key={key}>{children}</h1>);
|
||||
else if (level === 2) blocks.push(<h2 key={key}>{children}</h2>);
|
||||
else if (level === 3) blocks.push(<h3 key={key}>{children}</h3>);
|
||||
@@ -73,8 +78,8 @@ export function AiMarkdown({ content, className }: { content: string; className?
|
||||
const body: string[][] = [];
|
||||
while (index < lines.length && lines[index].trim() && lines[index].includes('|')) body.push(tableCells(lines[index++]));
|
||||
blocks.push(<div className="ai-markdown-table-wrap" key={`table-${blocks.length}`}><table>
|
||||
<thead><tr><th className="ai-markdown-index">序号</th>{headers.map((cell, cellIndex) => <th key={cellIndex}>{inlineMarkdown(cell, `th-${cellIndex}`)}</th>)}</tr></thead>
|
||||
<tbody>{body.map((row, rowIndex) => <tr key={rowIndex}><td className="ai-markdown-index">{rowIndex + 1}</td>{headers.map((_, cellIndex) => <td key={cellIndex}>{inlineMarkdown(row[cellIndex] || '', `td-${rowIndex}-${cellIndex}`)}</td>)}</tr>)}</tbody>
|
||||
<thead><tr><th className="ai-markdown-index">序号</th>{headers.map((cell, cellIndex) => <th key={cellIndex}>{inline(cell, `th-${cellIndex}`)}</th>)}</tr></thead>
|
||||
<tbody>{body.map((row, rowIndex) => <tr key={rowIndex}><td className="ai-markdown-index">{rowIndex + 1}</td>{headers.map((_, cellIndex) => <td key={cellIndex}>{inline(row[cellIndex] || '', `td-${rowIndex}-${cellIndex}`)}</td>)}</tr>)}</tbody>
|
||||
</table></div>);
|
||||
continue;
|
||||
}
|
||||
@@ -91,8 +96,8 @@ export function AiMarkdown({ content, className }: { content: string; className?
|
||||
index += 1;
|
||||
}
|
||||
blocks.push(ordered
|
||||
? <ol key={`list-${blocks.length}`} start={firstNumber}>{list.map((item, itemIndex) => <li key={itemIndex}>{inlineMarkdown(item, `ol-${blocks.length}-${itemIndex}`)}</li>)}</ol>
|
||||
: <ul key={`list-${blocks.length}`}>{list.map((item, itemIndex) => <li key={itemIndex}>{inlineMarkdown(item, `ul-${blocks.length}-${itemIndex}`)}</li>)}</ul>);
|
||||
? <ol key={`list-${blocks.length}`} start={firstNumber}>{list.map((item, itemIndex) => <li key={itemIndex}>{inline(item, `ol-${blocks.length}-${itemIndex}`)}</li>)}</ol>
|
||||
: <ul key={`list-${blocks.length}`}>{list.map((item, itemIndex) => <li key={itemIndex}>{inline(item, `ul-${blocks.length}-${itemIndex}`)}</li>)}</ul>);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -101,7 +106,7 @@ export function AiMarkdown({ content, className }: { content: string; className?
|
||||
if (!paragraph.length) { index += 1; continue; }
|
||||
blocks.push(<p key={`paragraph-${blocks.length}`}>{paragraph.flatMap((part, partIndex) => [
|
||||
...(partIndex ? [<br key={`break-${partIndex}`} />] : []),
|
||||
...inlineMarkdown(part, `paragraph-${blocks.length}-${partIndex}`),
|
||||
...inline(part, `paragraph-${blocks.length}-${partIndex}`),
|
||||
])}</p>);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user