@@ -27,6 +27,7 @@ You can use this as a [`shiki` transformer](https://shiki.style/guide/transforme
27
27
- ` feedbackDuration ` : ` number ` (default: ` 3_000 ` )
28
28
- ` copyIcon ` : ` string ` (default: an inline SVG of a copy icon)
29
29
- ` successIcon ` : ` string ` (default: an inline SVG of a green checkmark icon)
30
+ - ` jsx ` : ` boolean ` (default: ` false ` ) (required as ` true ` for React-based usage)
30
31
31
32
### ` transformerLineNumbers `
32
33
@@ -38,15 +39,15 @@ You can use this as a [`shiki` transformer](https://shiki.style/guide/transforme
38
39
39
40
#### Examples
40
41
41
- ##### with ` rehype-pretty-code `
42
+ ##### direct
42
43
43
44
``` ts
44
45
import { unified } from ' unified'
45
46
import remarkParse from ' remark-parse'
46
47
import remarkRehype from ' remark-rehype'
47
48
import rehypeStringify from ' rehype-stringify'
48
- import rehypePrettyCode from ' rehype-pretty-code'
49
- import { transformerCopyButton , transformerLineNumbers } from ' @rehype-pretty/transformers'
49
+ import { rehypePrettyCode } from ' rehype-pretty-code'
50
+ import { transformerCopyButton } from ' @rehype-pretty/transformers'
50
51
51
52
const file = await unified ()
52
53
.use (remarkParse )
@@ -57,7 +58,7 @@ You can use this as a [`shiki` transformer](https://shiki.style/guide/transforme
57
58
visibility: ' always' ,
58
59
feedbackDuration: 3_000 ,
59
60
}),
60
- transformerLineNumbers ({ autoApply: true }),
61
+ transformerLineNumbers ({ autoApply: false }),
61
62
],
62
63
})
63
64
.use (rehypeStringify )
@@ -66,21 +67,66 @@ You can use this as a [`shiki` transformer](https://shiki.style/guide/transforme
66
67
console .log (String (file ))
67
68
```
68
69
69
- ##### with ` shiki `
70
+ ##### In React / Next.js
71
+
72
+ In Next.js you st it up in ` next.config.js ` as you'd expect with ` jsx: true `
73
+
74
+ ``` js
75
+ // next.config.js
76
+
77
+ import nextMDX from ' @next/mdx' ;
78
+ import rehypeSlug from ' rehype-slug' ;
79
+ import { rehypePrettyCode } from ' rehype-pretty-code' ;
80
+ import { transformerCopyButton } from ' @rehype-pretty/transformers' ;
81
+
82
+ const plugins = [];
83
+
84
+ const nextConfig = {
85
+ output: ' export' ,
86
+ pageExtensions: [' md' , ' mdx' , ' tsx' , ' ts' , ' jsx' , ' js' ],
87
+ };
88
+
89
+ const const rehypePrettyCodeOptions = {
90
+ theme: ' github-dark' ,
91
+ keepBackground: false ,
92
+ transformers: [
93
+ transformerCopyButton ({
94
+ jsx: true , // required for React
95
+ visibility: ' always' ,
96
+ feedbackDuration: 2_500 ,
97
+ }),
98
+ ],
99
+ };
100
+
101
+ plugins .push (
102
+ nextMDX ({
103
+ extension: / \. (md| mdx)$ / ,
104
+ options: {
105
+ remarkPlugins: [],
106
+ rehypePlugins: [[rehypePrettyCode, rehypePrettyCodeOptions], rehypeSlug],
107
+ },
108
+ }),
109
+ );
110
+
111
+ export default () => plugins .reduce ((_ , plugin ) => plugin (_), nextConfig);
112
+ ```
70
113
71
- ``` ts
72
- import { codeToHtml } from ' shiki'
73
- import { transformerCopyButton , transformerLineNumbers } from ' @rehype-pretty/transformers'
74
-
75
- const code = await codeToHtml (' console.log("Hello World")' , {
76
- lang: ' ts' ,
77
- theme: ' vitesse-light' ,
78
- transformers: [
79
- transformerCopyButton ({
80
- visibility: ' always' ,
81
- feedbackDuration: 3_000 ,
82
- }),
83
- transformerLineNumbers ({ autoApply: true }),
84
- ]
85
- })
86
- ```
114
+ Then in your client component, import the ` registerCopyButton ` function and call it in your the outermost ** client** component.
115
+
116
+ ``` tsx
117
+ ' use client' ;
118
+
119
+ import { registerCopyButton } from ' @rehype-pretty/transformers' ;
120
+
121
+ export default function Home() {
122
+ React .useEffect (() => {
123
+ registerCopyButton ();
124
+ }, []);
125
+
126
+ return (
127
+ <MDXProvider disableParentContext = { false } >
128
+ <Index />
129
+ </MDXProvider >
130
+ );
131
+ }
132
+ ```
0 commit comments