AstrBot-Desktop/src/features/chat/MessageContent.tsx
YokiiroBW ea332257ac
一些检测仍在等待运行
ci / Portable quality checks (push) Waiting to run
ci / Windows Tauri build (push) Waiting to run
feat(chat): advance Q10-Q15 experience
2026-07-21 15:25:11 +08:00

122 行
3.2 KiB
TypeScript

import { Fragment, type ReactNode } from "react";
type MessageContentProps = {
content: string;
};
function renderInline(text: string, keyPrefix: string): ReactNode[] {
const tokens = text.split(/(`[^`]+`|\*\*[^*]+\*\*|\[[^\]]+\]\([^)]+\))/g);
return tokens.map((token, index) => {
const key = `${keyPrefix}-${index}`;
if (token.startsWith("`") && token.endsWith("`")) {
return <code key={key}>{token.slice(1, -1)}</code>;
}
if (token.startsWith("**") && token.endsWith("**")) {
return <strong key={key}>{token.slice(2, -2)}</strong>;
}
const link = token.match(/^\[([^\]]+)\]\(([^)]+)\)$/);
if (link) {
return (
<a key={key} href={link[2]} target="_blank" rel="noreferrer">
{link[1]}
</a>
);
}
return <Fragment key={key}>{token}</Fragment>;
});
}
export function MessageContent({ content }: MessageContentProps) {
const lines = content.replaceAll("\r\n", "\n").split("\n");
const blocks: ReactNode[] = [];
let codeLines: string[] | null = null;
let codeLanguage = "";
let paragraph: string[] = [];
let list: string[] = [];
const flushParagraph = () => {
if (!paragraph.length) return;
blocks.push(
<p key={`paragraph-${blocks.length}`}>
{renderInline(paragraph.join(" "), `paragraph-${blocks.length}`)}
</p>,
);
paragraph = [];
};
const flushList = () => {
if (!list.length) return;
blocks.push(
<ul key={`list-${blocks.length}`}>
{list.map((item, index) => (
<li key={`list-${blocks.length}-${index}`}>
{renderInline(item, `list-${blocks.length}-${index}`)}
</li>
))}
</ul>,
);
list = [];
};
lines.forEach((line, index) => {
if (line.trimStart().startsWith("```") && codeLines === null) {
flushParagraph();
flushList();
codeLines = [];
codeLanguage = line.trim().slice(3).trim();
return;
}
if (line.trimStart().startsWith("```") && codeLines !== null) {
blocks.push(
<pre key={`code-${index}`} data-language={codeLanguage || undefined}>
<code>{codeLines.join("\n")}</code>
</pre>,
);
codeLines = null;
codeLanguage = "";
return;
}
if (codeLines !== null) {
codeLines.push(line);
return;
}
if (!line.trim()) {
flushParagraph();
flushList();
return;
}
const heading = line.match(/^(#{1,3})\s+(.+)$/);
if (heading) {
flushParagraph();
flushList();
const level = heading[1].length;
const Heading = `h${level}` as "h1" | "h2" | "h3";
blocks.push(
<Heading key={`heading-${index}`}>
{renderInline(heading[2], `heading-${index}`)}
</Heading>,
);
return;
}
const item = line.match(/^\s*[-*]\s+(.+)$/);
if (item) {
flushParagraph();
list.push(item[1]);
return;
}
flushList();
paragraph.push(line.trim());
});
const finalCodeText = (codeLines as string[] | null)?.join("\n");
if (finalCodeText !== undefined) {
blocks.push(
<pre key="code-final">
<code>{finalCodeText}</code>
</pre>,
);
}
flushParagraph();
flushList();
return <div className="message-content">{blocks}</div>;
}