import { IconCheck, IconChevronDown, IconCopy, } from "@tabler/icons-react" import { useAtom } from "jotai" import hljs from "highlight.js/lib/core" import json from "highlight.js/lib/languages/json" import { type ComponentProps, type CSSProperties, type ReactNode, useState, } from "react" import { useTranslation } from "react-i18next" import { useCopyToClipboard } from "@/hooks/use-copy-to-clipboard" import { cn } from "@/lib/utils" import { codeBlockWrapAtom } from "@/store/code-block" import { extractCodeBlockFromPreNode, extractCodeBlockRenderState, type MarkdownNode, splitCodeIntoLines, splitHighlightedHtmlIntoLines, splitRenderedCodeContentIntoLines, trimTrailingEmptyRenderedCodeLine, trimTrailingEmptyStringLine, } from "./message-code-block.utils" import { Button } from "@/components/ui/button" const CODE_LABEL_FONT_FAMILY = 'ui-monospace, "SFMono-Regular", Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", monospace' hljs.registerLanguage("json", json) interface MessageCodeBlockProps { code: string language?: string | null label?: string className?: string bodyClassName?: string children?: ReactNode trimTrailingEmptyLine?: boolean } interface MarkdownCodeBlockProps extends ComponentProps<"pre"> { node?: MarkdownNode } function getHighlightedHtml(code: string, language?: string | null) { if (!language) { return null } try { return hljs.highlight(code, { language }).value } catch { return null } } export function MessageCodeBlock({ code, language = null, label, className, bodyClassName, children, trimTrailingEmptyLine = false, }: MessageCodeBlockProps) { const { t } = useTranslation() const { copy, isCopied } = useCopyToClipboard() const [wrapLongLines, setWrapLongLines] = useAtom(codeBlockWrapAtom) const [isExpanded, setIsExpanded] = useState(true) const blockLabel = label ?? (language ? language.toLocaleLowerCase() : t("chat.codeLabel").toLocaleLowerCase()) const copyLabel = isCopied ? t("chat.copiedLabel") : t("chat.copyCode") const expandLabel = isExpanded ? t("chat.collapseCode") : t("chat.expandCode") const wrapLabel = wrapLongLines ? t("chat.disableCodeWrap") : t("chat.enableCodeWrap") const renderedCodeState = children ? extractCodeBlockRenderState(children) : { renderedContent: null, className: undefined, } const highlightedHtml = !children ? getHighlightedHtml(code, language) : null const highlightedLines = highlightedHtml ? splitHighlightedHtmlIntoLines(highlightedHtml) : null const codeLines = children ? (trimTrailingEmptyLine ? trimTrailingEmptyRenderedCodeLine( splitRenderedCodeContentIntoLines(renderedCodeState.renderedContent), ) : splitRenderedCodeContentIntoLines(renderedCodeState.renderedContent)) : (trimTrailingEmptyLine ? trimTrailingEmptyStringLine( highlightedLines ?? splitCodeIntoLines(code), ) : (highlightedLines ?? splitCodeIntoLines(code))) const lineNumberWidth = `${String(codeLines.length).length + 1}ch` return (
{blockLabel}
{isExpanded && (
          
            {codeLines.map((line, index) => (
              
                
                  {index + 1}
                
                {!children && highlightedLines ? (
                  
                ) : (
                  
                    {line}
                  
                )}
              
            ))}
          
        
)}
) } export function MarkdownCodeBlock({ children, className, node, }: MarkdownCodeBlockProps) { const { code, language } = extractCodeBlockFromPreNode(node) return ( {children} ) }