@@ -68,141 +68,60 @@ should be installed as one of your project's `dependencies`:
68
68
npm install --save prism-react-renderer
69
69
# yarn
70
70
yarn add prism-react-renderer
71
+ # pnpm
72
+ pnpm add prism-react-renderer
71
73
```
72
74
73
- > This package also depends on ` react ` . Please make sure you
74
- > have those installed as well.
75
+ > Prism React Renderer has a peer dependency on ` react `
75
76
76
- ## Usage
77
+ ### How to use Prism React Renderer
78
+ Prism React Renderer has a named export for the ` <Highlight /> ` component along with ` themes ` . To see Prism React Render in action with base styling check out ` packages/demo ` or run ` pnpm run start:demo ` from the root of this repository.
77
79
78
- > [ Try it out in the browser] ( https://codesandbox.io/s/prism-react-renderer-example-u6vhk )
80
+ ``` tsx
81
+ import React from " react"
82
+ import ReactDOM from " react-dom/client"
83
+ import { Highlight , themes } from " prism-react-renderer"
84
+ import styles from ' styles.module.css'
79
85
80
- <img src =" ./.readme/basic.png " width =" 237 " alt =" Screenshot showing highlighted code block " />
81
-
82
- ``` jsx
83
- import React from " react" ;
84
- import { render } from " react-dom" ;
85
- // import { createRoot } from "react-dom/client";
86
- import Highlight , { defaultProps } from " prism-react-renderer" ;
87
-
88
- const exampleCode = `
89
- (function someDemo() {
90
- var test = "Hello World!";
91
- console.log(test);
92
- })();
93
-
94
- return () => <App />;
95
- ` ;
96
-
97
- const Content = (
98
- < Highlight {... defaultProps} code= {exampleCode} language= " jsx" >
86
+ const codeBlock = `
87
+ const GroceryItem: React.FC<GroceryItemProps> = ({ item }) => {
88
+ return (
89
+ <div>
90
+ <h2>{item.name}</h2>
91
+ <p>Price: {item.price}</p>
92
+ <p>Quantity: {item.quantity}</p>
93
+ </div>
94
+ );
95
+ }
96
+ `
97
+
98
+ export const App = () => (
99
+ <Highlight
100
+ theme = { themes .shadesOfPurple }
101
+ code = { codeBlock }
102
+ language = " tsx"
103
+ >
99
104
{ ({ className , style , tokens , getLineProps , getTokenProps }) => (
100
- < pre className = {className} style= {style}>
105
+ <pre style = { style } >
101
106
{ tokens .map ((line , i ) => (
102
- < div {... getLineProps ({ line, key: i })}>
107
+ <div key = { i } { ... getLineProps ({ line })} >
108
+ <span >{ i + 1 } </span >
103
109
{ line .map ((token , key ) => (
104
- < span { ... getTokenProps ({ token, key })} / >
110
+ <span key = { key } { ... getTokenProps ({ token })} />
105
111
))}
106
112
</div >
107
113
))}
108
114
</pre >
109
115
)}
110
116
</Highlight >
111
- );
112
- render (Content, document .getElementById (' root' ));
113
-
114
- // If you are using React 18
115
- // const root = createRoot(document.getElementById('root'));
116
- // root.render(Content);
117
- ```
118
-
119
- ` <Highlight /> ` is the only component exposed by this package, as inspired by
120
- [ downshift] ( https://github.com/paypal/downshift ) .
121
-
122
- It also exports a ` defaultProps ` object which for basic usage can simply be spread
123
- onto the ` <Highlight /> ` component. It also provides some default theming.
124
-
125
- It doesn't render anything itself, it just calls the render function and renders that.
126
- [ "Use a render prop!"] ( https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce ) !
127
- ` <Highlight>{highlight => <pre>/* your JSX here! */</pre>}</Highlight> `
128
-
129
- ### Line Numbers
130
-
131
- Add line numbers to your highlighted code blocks using the ` index ` parameter in your ` line.map ` function:
132
-
133
- <img src =" ./.readme/line-numbers.png " width =" 453 " alt =" Screenshot showing line numbers in highlighted code block " />
134
-
135
- For example, if you were using ` styled-components ` , it could look like the following demo.
117
+ )
136
118
137
- > [ Demo with ` styled-components ` ] ( https://codesandbox.io/s/prism-react-renderer-example-u6vhk?file=/src/WithLineNumbers.js )
119
+ ReactDOM .createRoot (document .getElementById (" root" ) as HTMLElement )
120
+ .render (<App />)
138
121
139
- ``` js
140
- import React from " react" ;
141
- import styled from " styled-components" ;
142
- import Highlight , { defaultProps } from " prism-react-renderer" ;
143
- import theme from " prism-react-renderer/themes/nightOwl" ;
122
+ ```
144
123
145
- const exampleCode = `
146
- import React, { useState } from "react";
147
124
148
- function Example() {
149
- const [count, setCount] = useState(0);
150
-
151
- return (
152
- <div>
153
- <p>You clicked {count} times</p>
154
- <button onClick={() => setCount(count + 1)}>
155
- Click me
156
- </button>
157
- </div>
158
- );
159
- }
160
- ` .trim ();
161
-
162
- const Pre = styled .pre `
163
- text-align: left;
164
- margin: 1em 0;
165
- padding: 0.5em;
166
- overflow: scroll;
167
- ` ;
168
-
169
- const Line = styled .div `
170
- display: table-row;
171
- ` ;
172
-
173
- const LineNo = styled .span `
174
- display: table-cell;
175
- text-align: right;
176
- padding-right: 1em;
177
- user-select: none;
178
- opacity: 0.5;
179
- ` ;
180
-
181
- const LineContent = styled .span `
182
- display: table-cell;
183
- ` ;
184
-
185
- const WithLineNumbers = () => (
186
- < Highlight {... defaultProps} theme= {theme} code= {exampleCode} language= " jsx" >
187
- {({ className, style, tokens, getLineProps, getTokenProps }) => (
188
- < Pre className= {className} style= {style}>
189
- {tokens .map ((line , i ) => (
190
- < Line key= {i} {... getLineProps ({ line, key: i })}>
191
- < LineNo> {i + 1 }< / LineNo>
192
- < LineContent>
193
- {line .map ((token , key ) => (
194
- < span key= {key} {... getTokenProps ({ token, key })} / >
195
- ))}
196
- < / LineContent>
197
- < / Line>
198
- ))}
199
- < / Pre>
200
- )}
201
- < / Highlight>
202
- );
203
-
204
- export default WithLineNumbers ;
205
- ```
206
125
207
126
## Basic Props
208
127
@@ -235,19 +154,17 @@ This is the code that will be highlighted.
235
154
236
155
### theme
237
156
238
- > ` PrismTheme ` | _ required ; default is provided in ` defaultProps ` export _
157
+ > ` PrismTheme ` | _ optional ; default is vsDark _
239
158
240
159
If a theme is passed, it is used to generate style props which can be retrieved
241
160
via the prop-getters which are described in "[ Children Function] ( #children-function ) ".
242
161
243
- A default theme is provided by the ` defaultProps ` object.
244
-
245
162
Read more about how to theme ` prism-react-renderer ` in
246
163
the section "[ Theming] ( #theming ) ".
247
164
248
- ### Prism
165
+ ### prism
249
166
250
- > ` PrismLib ` | _ required ; default is provided in ` defaultProps ` export _
167
+ > ` prism ` | _ optional ; default is the vendored version _
251
168
252
169
This is the [ Prismjs] ( https://github.com/PrismJS/prism ) library itself.
253
170
A vendored version of Prism is provided (and also exported) as part of this library.
@@ -263,7 +180,7 @@ But if you choose to use your own Prism setup, simply pass Prism as a prop:
263
180
// Whichever way you're retrieving Prism here:
264
181
import Prism from ' prismjs/components/prism-core' ;
265
182
266
- < Highlight Prism = {Prism } {/* ... */ } / >
183
+ < Highlight prism = {prism } {/* ... */ } / >
267
184
```
268
185
269
186
## Children Function
@@ -349,15 +266,15 @@ your old Prism CSS-file themes.
349
266
## Theming
350
267
351
268
The ` defaultProps ` you'd typically apply in a basic use-case, contain a default theme.
352
- This theme is [ duotoneDark ] ( ./src/themes/duotoneDark.js ) .
269
+ This theme is [ vsDark ] ( ./packages/prism-react-renderer/ src/themes/vsDark.ts ) .
353
270
354
271
While all ` className ` s are provided with ` <Highlight /> ` , so that you could use your good
355
272
old Prism CSS-file themes, you can also choose to use ` prism-react-renderer ` 's themes like so:
356
273
357
274
``` jsx
358
- import dracula from ' prism-react-renderer/themes/dracula ' ;
275
+ import { Highlight , themes } from ' prism-react-renderer' ;
359
276
360
- < Highlight theme= {dracula} {/* ... */ } / >
277
+ < Highlight theme= {themes . dracula } {/* ... */ } / >
361
278
```
362
279
363
280
These themes are JSON-based and are heavily inspired by VSCode's theme format.
0 commit comments