初始化:质量数据分析平台源码

This commit is contained in:
2026-10-22 15:58:57 +08:00
commit 1e8a655adc
280 changed files with 43922 additions and 0 deletions
+109
View File
@@ -0,0 +1,109 @@
import type { ReactNode } from 'react';
import { sanitizeAnalysisDisplayText } from './analysisDisplayText';
function inlineMarkdown(value: string, keyPrefix = 'inline'): ReactNode[] {
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>;
}
if (part.startsWith('`') && part.endsWith('`') && part.length >= 2) {
return <code key={`${keyPrefix}-${index}`}>{part.slice(1, -1)}</code>;
}
return part;
});
}
function tableCells(line: string): string[] {
const source = line.trim().replace(/^\|/, '').replace(/\|$/, '');
return source.split('|').map(cell => cell.trim());
}
function isTableStart(lines: string[], index: number): boolean {
if (index + 1 >= lines.length || !lines[index].includes('|')) return false;
const separators = tableCells(lines[index + 1]);
return separators.length > 0 && separators.every(cell => /^:?-{3,}:?$/.test(cell));
}
function isBlockStart(lines: string[], index: number): boolean {
const line = lines[index];
return /^\s*#{1,6}\s+/.test(line)
|| /^\s*```/.test(line)
|| /^\s*(?:[-*+]\s+|\d+[.)]\s+)/.test(line)
|| isTableStart(lines, index);
}
/** Renders the small Markdown subset used by analysis answers with React text nodes only. */
export function AiMarkdown({ content, className }: { content: string; className?: string }) {
const lines = sanitizeAnalysisDisplayText(content).replaceAll('\r\n', '\n').replaceAll('\r', '\n').split('\n');
const blocks: ReactNode[] = [];
let index = 0;
while (index < lines.length) {
const line = lines[index];
if (!line.trim()) { index += 1; continue; }
const fence = line.match(/^\s*```([^`]*)$/);
if (fence) {
const code: string[] = [];
index += 1;
while (index < lines.length && !/^\s*```\s*$/.test(lines[index])) code.push(lines[index++]);
if (index < lines.length) index += 1;
blocks.push(<pre key={`code-${blocks.length}`}><code>{code.join('\n')}</code></pre>);
continue;
}
const heading = line.match(/^\s*(#{1,6})\s+(.+?)\s*#*\s*$/);
if (heading) {
const level = heading[1].length;
const key = `heading-${blocks.length}`;
const children = inlineMarkdown(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>);
else if (level === 4) blocks.push(<h4 key={key}>{children}</h4>);
else if (level === 5) blocks.push(<h5 key={key}>{children}</h5>);
else blocks.push(<h6 key={key}>{children}</h6>);
index += 1;
continue;
}
if (isTableStart(lines, index)) {
const headers = tableCells(lines[index]);
index += 2;
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>{headers.map((cell, cellIndex) => <th key={cellIndex}>{inlineMarkdown(cell, `th-${cellIndex}`)}</th>)}</tr></thead>
<tbody>{body.map((row, rowIndex) => <tr key={rowIndex}>{headers.map((_, cellIndex) => <td key={cellIndex}>{inlineMarkdown(row[cellIndex] || '', `td-${rowIndex}-${cellIndex}`)}</td>)}</tr>)}</tbody>
</table></div>);
continue;
}
const listItem = line.match(/^\s*(?:([-*+])\s+|(\d+)[.)]\s+)(.*)$/);
if (listItem) {
const ordered = Boolean(listItem[2]);
const list: string[] = [];
const firstNumber = ordered ? Number(listItem[2]) : undefined;
while (index < lines.length) {
const current = lines[index].match(/^\s*(?:([-*+])\s+|(\d+)[.)]\s+)(.*)$/);
if (!current || Boolean(current[2]) !== ordered) break;
list.push(current[3]);
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>);
continue;
}
const paragraph: string[] = [];
while (index < lines.length && lines[index].trim() && !isBlockStart(lines, index)) paragraph.push(lines[index++]);
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}`),
])}</p>);
}
return <div className={className ? `ai-markdown ${className}` : 'ai-markdown'}>{blocks}</div>;
}