Open Vallerian opened 1 month ago
Hi Vallerian, Getting the same error. Any luck?
Unfortunately, no, but if you find a way to extract the content of code blocks within React components using Rehype or Remark plugins, like remark-code-blocks, I'd be happy if you let me know.
unfortunately the copy button transformer currently doesn't work with Next.js unless you use a Server Component
Example implementation: https://github.com/rehype-pretty/rehype-pretty-code/blob/master/examples/next/src/app/code.tsx
You can easily add a custom pre component with a copy button.
'use client';
import { Check, Clipboard } from 'lucide-react'; import { DetailedHTMLProps, HTMLAttributes, useRef, useState } from 'react';
export default function Pre({
children,
...props
}: DetailedHTMLProps<HTMLAttributes
const handleClickCopy = async () => { const code = preRef.current?.textContent;
if (code) {
await navigator.clipboard.writeText(code);
setIsCopied(true);
setTimeout(() => {
setIsCopied(false);
}, 3000);
}
};
return ( <pre ref={preRef} {...props} className='relative'> <button disabled={isCopied} onClick={handleClickCopy} className='absolute right-4 size-6'
{isCopied ?
: } {children} ); } - `mdx-component.tsx` (if you are using @next/mdx) ```tsx import type { MDXComponents } from 'mdx/types'; import Pre from './components/mdx/Pre';
export function useMDXComponents(components: MDXComponents): MDXComponents { return { pre: (props) => <Pre {...props} />, ...components, };
- Result
<img width="785" alt="image" src="https://github.com/user-attachments/assets/223d7549-516f-4b04-a71b-1ad11ad12e37">
I am encountering an error while using the
rehype-pretty-code
package in a Next.js project, specifically when utilizing thetransformerCopyButton
feature. The error message I receive is:This error seems to occur when the
transformerCopyButton
attempts to render the copy button for code blocks. It appears that the onClick event listener is being assigned a string value instead of a function.Next.js version: 14.2.5 rehype-pretty version: 0.13.2
next.config.mjs