import React from 'react';
import { Typography } from 'antd';
import '../../styles/components/chat-with-llm/markdown.css';
const { Paragraph, Text, Title } = Typography;
interface MarkdownProps {
content: string;
className?: string;
}
/**
* Markdown 渲染组件
* 简化版本,支持基本的 Markdown 语法
*/
export default function Markdown({ content, className = '' }: MarkdownProps) {
if (!content) return null;
/**
* 简单的 Markdown 解析器
*/
const parseMarkdown = (text: string): React.ReactNode => {
// 分割成行
const lines = text.split('\n');
const elements: React.ReactNode[] = [];
let currentParagraph: string[] = [];
let inCodeBlock = false;
let codeBlockContent: string[] = [];
let codeBlockLanguage = '';
const flushParagraph = () => {
if (currentParagraph.length > 0) {
const paragraphText = currentParagraph.join('\n');
elements.push(
{codeBlockContent.join('\n')}
);
codeBlockContent = [];
codeBlockLanguage = '';
}
};
lines.forEach((line, index) => {
// 处理代码块
if (line.startsWith('```')) {
if (inCodeBlock) {
// 结束代码块
flushCodeBlock();
inCodeBlock = false;
} else {
// 开始代码块
flushParagraph();
inCodeBlock = true;
codeBlockLanguage = line.slice(3).trim();
}
return;
}
if (inCodeBlock) {
codeBlockContent.push(line);
return;
}
// 处理标题
if (line.startsWith('# ')) {
flushParagraph();
elements.push(