-
{
- copyStr(copiedContent);
- setCopied(true);
- }}
- onMouseLeave={() => setCopied(false)}
- >
-
-
-
+
+
);
};
+export const CopyButton = ({
+ content,
+ className,
+}: {
+ content: string;
+ className?: string;
+}) => {
+ const [copied, setCopied] = useState(false);
+ return (
+
+ );
+};
+
+/**
+ * This injects the "button" element before each "pre" element.
+ * The actual button will be replaced with a react component in the MarkdownDisplay.
+ * We don't replace "pre" node directly because it will cause the node to re-render, which causes this bug: https://github.com/ggerganov/llama.cpp/issues/9608
+ */
+function rehypeCustomCopyButton() {
+ return function (tree: Root) {
+ visit(tree, 'element', function (node) {
+ if (node.tagName === 'pre' && !node.properties.visited) {
+ const preNode = { ...node };
+ // replace current node
+ preNode.properties.visited = 'true';
+ node.tagName = 'div';
+ node.properties = {
+ className: 'relative my-4',
+ };
+ // add node for button
+ const btnNode: ElementContent = {
+ type: 'element',
+ tagName: 'button',
+ properties: {},
+ children: [],
+ position: node.position,
+ };
+ node.children = [btnNode, preNode];
+ }
+ });
+ };
+}
+
/**
* The part below is copied and adapted from:
* https://github.com/danny-avila/LibreChat/blob/main/client/src/utils/latex.ts