@@ -68,141 +68,60 @@ should be installed as one of your project's `dependencies`:
6868npm install --save prism-react-renderer
6969# yarn
7070yarn add prism-react-renderer
71+ # pnpm
72+ pnpm add prism-react-renderer
7173```
7274
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 `
7576
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.
7779
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'
7985
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+ >
99104 { ({ className , style , tokens , getLineProps , getTokenProps }) => (
100- < pre className = {className} style= {style}>
105+ <pre style = { style } >
101106 { tokens .map ((line , i ) => (
102- < div {... getLineProps ({ line, key: i })}>
107+ <div key = { i } { ... getLineProps ({ line })} >
108+ <span >{ i + 1 } </span >
103109 { line .map ((token , key ) => (
104- < span { ... getTokenProps ({ token, key })} / >
110+ <span key = { key } { ... getTokenProps ({ token })} />
105111 ))}
106112 </div >
107113 ))}
108114 </pre >
109115 )}
110116 </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+ )
136118
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 />)
138121
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+ ```
144123
145- const exampleCode = `
146- import React, { useState } from "react";
147124
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- ```
206125
207126## Basic Props
208127
@@ -235,19 +154,17 @@ This is the code that will be highlighted.
235154
236155### theme
237156
238- > ` PrismTheme ` | _ required ; default is provided in ` defaultProps ` export _
157+ > ` PrismTheme ` | _ optional ; default is vsDark _
239158
240159If a theme is passed, it is used to generate style props which can be retrieved
241160via the prop-getters which are described in "[ Children Function] ( #children-function ) ".
242161
243- A default theme is provided by the ` defaultProps ` object.
244-
245162Read more about how to theme ` prism-react-renderer ` in
246163the section "[ Theming] ( #theming ) ".
247164
248- ### Prism
165+ ### prism
249166
250- > ` PrismLib ` | _ required ; default is provided in ` defaultProps ` export _
167+ > ` prism ` | _ optional ; default is the vendored version _
251168
252169This is the [ Prismjs] ( https://github.com/PrismJS/prism ) library itself.
253170A 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:
263180// Whichever way you're retrieving Prism here:
264181import Prism from ' prismjs/components/prism-core' ;
265182
266- < Highlight Prism = {Prism } {/* ... */ } / >
183+ < Highlight prism = {prism } {/* ... */ } / >
267184```
268185
269186## Children Function
@@ -349,15 +266,15 @@ your old Prism CSS-file themes.
349266## Theming
350267
351268The ` 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 ) .
353270
354271While all ` className ` s are provided with ` <Highlight /> ` , so that you could use your good
355272old Prism CSS-file themes, you can also choose to use ` prism-react-renderer ` 's themes like so:
356273
357274``` jsx
358- import dracula from ' prism-react-renderer/themes/dracula ' ;
275+ import { Highlight , themes } from ' prism-react-renderer' ;
359276
360- < Highlight theme= {dracula} {/* ... */ } / >
277+ < Highlight theme= {themes . dracula } {/* ... */ } / >
361278```
362279
363280These themes are JSON-based and are heavily inspired by VSCode's theme format.
0 commit comments