// Copyright 2024, Command Line Inc. // SPDX-License-Identifier: Apache-2.0 import { CopyButton } from "@/app/element/copybutton"; import { clsx } from "clsx"; import React from "react"; import ReactMarkdown from "react-markdown"; import rehypeRaw from "rehype-raw"; import remarkGfm from "remark-gfm"; import "./markdown.less"; const Link = ({ href, children }: { href: string; children: React.ReactNode }) => { const newUrl = "https://extern?" + encodeURIComponent(href); return ( {children} ); }; const Header = ({ children, hnum }: { children: React.ReactNode; hnum: number }) => { return
{children}
; }; const Code = ({ children }: { children: React.ReactNode }) => { return {children}; }; type CodeBlockProps = { children: React.ReactNode; onClickExecute?: (cmd: string) => void; }; const CodeBlock = ({ children, onClickExecute }: CodeBlockProps) => { const getTextContent = (children: any): string => { if (typeof children === "string") { return children; } else if (Array.isArray(children)) { return children.map(getTextContent).join(""); } else if (children.props && children.props.children) { return getTextContent(children.props.children); } return ""; }; const handleCopy = async (e: React.MouseEvent) => { let textToCopy = getTextContent(children); textToCopy = textToCopy.replace(/\n$/, ""); // remove trailing newline await navigator.clipboard.writeText(textToCopy); }; const handleExecute = (e: React.MouseEvent) => { let textToCopy = getTextContent(children); textToCopy = textToCopy.replace(/\n$/, ""); // remove trailing newline if (onClickExecute) { onClickExecute(textToCopy); return; } }; return (
            {children}
            
{onClickExecute && }
); }; type MarkdownProps = { text: string; style?: React.CSSProperties; className?: string; onClickExecute?: (cmd: string) => void; }; const Markdown = ({ text, style, className, onClickExecute }: MarkdownProps) => { const markdownComponents = { a: Link, h1: (props: any) =>
, h2: (props: any) =>
, h3: (props: any) =>
, h4: (props: any) =>
, h5: (props: any) =>
, h6: (props: any) =>
, code: Code, pre: (props: any) => , }; return (
{text}
); }; export { Markdown };