import { useId, useMemo, useRef, useState, type ReactNode } from "react"; import type { ComponentProps } from "@ant-design/x-markdown"; import katex from "katex"; import "katex/dist/katex.min.css"; import { Check, Code2, Copy, Download, Eye } from "lucide-react"; import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; import { oneDark, oneLight, } from "react-syntax-highlighter/dist/esm/styles/prism"; import { useTranslation } from "react-i18next"; import { useTheme } from "../../contexts/ThemeContext"; import { MermaidCodeBlock } from "../MermaidCodeBlock"; import styles from "./index.module.less"; type ViewMode = "preview" | "raw"; type RenderableLanguage = "latex" | "mermaid"; type RenderableCodeBlockProps = Partial & { children?: ReactNode; }; const LANGUAGE_ALIASES: Record = { latex: "latex", math: "latex", mermaid: "mermaid", tex: "latex", }; const LANGUAGE_EXTENSIONS: Record = { bash: "sh", csharp: "cs", javascript: "js", kotlin: "kt", markdown: "md", mermaid: "mmd", python: "py", ruby: "rb", rust: "rs", shell: "sh", typescript: "ts", yaml: "yml", zsh: "sh", }; function extractText(children: ReactNode): string { if (typeof children === "string") return children; if (typeof children === "number") return String(children); if (Array.isArray(children)) return children.map(extractText).join(""); if (children && typeof children === "object" && "props" in children) { return extractText( (children as { props: { children?: ReactNode } }).props.children, ); } return ""; } function resolveLanguage(lang?: string, className?: string): string { const explicit = lang?.trim().split(/\s+/, 1)[0]; const fromClassName = className?.match(/(?:^|\s)language-([^\s]+)/)?.[1]; return (explicit || fromClassName || "").toLowerCase(); } function downloadSource(source: string, language: string) { const extension = language === "latex" ? "tex" : LANGUAGE_EXTENSIONS[language] || language; const blob = new Blob([source], { type: "text/plain;charset=utf-8" }); const url = URL.createObjectURL(blob); const anchor = document.createElement("a"); anchor.href = url; anchor.download = `block.${extension}`; document.body.appendChild(anchor); anchor.click(); anchor.remove(); window.setTimeout(() => URL.revokeObjectURL(url), 0); } function BlockActions({ source, language, }: { source: string; language: string; }) { const { t } = useTranslation(); const [copied, setCopied] = useState(false); const copyTimer = useRef(); const handleCopy = async () => { try { if (window.isSecureContext && navigator.clipboard) { await navigator.clipboard.writeText(source); } else { const textarea = document.createElement("textarea"); textarea.value = source; textarea.style.cssText = "position:fixed;left:-9999px"; document.body.appendChild(textarea); textarea.select(); document.execCommand("copy"); textarea.remove(); } setCopied(true); window.clearTimeout(copyTimer.current); copyTimer.current = window.setTimeout(() => setCopied(false), 1200); } catch { setCopied(false); } }; return (
); } function SourceCode({ source, language, }: { source: string; language: string; }) { const { isDark } = useTheme(); return ( {source.replace(/\n$/, "")} ); } function SourceOnlyBlock({ source, language, }: { source: string; language: string; }) { return (
{language || "text"}
); } function LatexPreview({ source }: { source: string }) { const result = useMemo(() => { try { return { html: katex.renderToString(source.trim(), { displayMode: true, output: "htmlAndMathml", throwOnError: true, }), error: "", }; } catch (error) { return { html: "", error: error instanceof Error ? error.message : String(error), }; } }, [source]); if (result.error) { return (
); } return (
); } function RenderableBlock({ source, language, }: { source: string; language: RenderableLanguage; }) { const { t } = useTranslation(); const [view, setView] = useState("preview"); const tabGroupId = useId(); const tabRefs = useRef>({ preview: null, raw: null, }); const previewTabId = `${tabGroupId}-preview-tab`; const rawTabId = `${tabGroupId}-raw-tab`; const panelId = `${tabGroupId}-panel`; const selectTab = (nextView: ViewMode) => { setView(nextView); tabRefs.current[nextView]?.focus(); }; const handleTabKeyDown = ( event: React.KeyboardEvent, currentView: ViewMode, ) => { let nextView: ViewMode | undefined; if (event.key === "ArrowLeft" || event.key === "ArrowRight") { nextView = currentView === "preview" ? "raw" : "preview"; } else if (event.key === "Home") { nextView = "preview"; } else if (event.key === "End") { nextView = "raw"; } if (nextView) { event.preventDefault(); selectTab(nextView); } }; return (
{language}
{view === "preview" ? ( language === "mermaid" ? ( ) : ( ) ) : ( )}
); } export function RenderableCodeBlock(props: RenderableCodeBlockProps) { const { children, lang, block, className, domNode, streamStatus, ...htmlProps } = props; const language = resolveLanguage(lang, className); const renderableLanguage = LANGUAGE_ALIASES[language]; void domNode; void streamStatus; if (!block && !className?.includes("language-")) { return ( {children} ); } if (!renderableLanguage) { return ( ); } return ( ); }