{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) => Hello, how may I help you with this command?
+(Cmd-Shift-Space: open/close, Ctrl+L: clear chat buffer, Up/Down: select code blocks, Enter: to copy a selected code block to the command input)