Skip to content

Commit 8951b38

Browse files
committed
Bump to Prism 1.29, update README
1 parent 0b6746a commit 8951b38

File tree

5 files changed

+116
-279
lines changed

5 files changed

+116
-279
lines changed

README.md

Lines changed: 43 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -68,141 +68,60 @@ should be installed as one of your project's `dependencies`:
6868
npm install --save prism-react-renderer
6969
# yarn
7070
yarn 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
240159
If a theme is passed, it is used to generate style props which can be retrieved
241160
via the prop-getters which are described in "[Children Function](#children-function)".
242161

243-
A default theme is provided by the `defaultProps` object.
244-
245162
Read more about how to theme `prism-react-renderer` in
246163
the 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
252169
This is the [Prismjs](https://github.com/PrismJS/prism) library itself.
253170
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:
263180
// Whichever way you're retrieving Prism here:
264181
import 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

351268
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).
353270

354271
While all `className`s are provided with `<Highlight />`, so that you could use your good
355272
old 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

363280
These themes are JSON-based and are heavily inspired by VSCode's theme format.

package.json

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
"build:languages": "pnpm --filter generate-prism-languages build",
1414
"build:watch": "pnpm --filter generate-prism-languages build && pnpm --filter prism-react-renderer build:watch",
1515
"changeset": "changeset",
16-
"version": "pnpm changeset version && pnpm install"
16+
"version": "pnpm changeset version"
1717
},
1818
"devDependencies": {
19-
"@changesets/cli": "^2.26.0",
20-
"@svitejs/changesets-changelog-github-compact": "^1.1.0",
2119
"@babel/cli": "^7.10.4",
2220
"@babel/core": "^7.21.4",
2321
"@babel/plugin-proposal-object-rest-spread": "^7.10.4",
@@ -26,25 +24,31 @@
2624
"@babel/preset-env": "^7.10.4",
2725
"@babel/preset-react": "^7.10.4",
2826
"@babel/preset-typescript": "^7.16.0",
27+
"@changesets/cli": "^2.26.0",
28+
"@svitejs/changesets-changelog-github-compact": "^1.1.0",
2929
"@testing-library/react": "^11.2.5",
3030
"@types/react": "^18.0.35",
3131
"@typescript-eslint/eslint-plugin": "^5.58.0",
3232
"@typescript-eslint/parser": "^5.58.0",
33+
"babel-jest": "^26.6.3",
34+
"babel-plugin-macros": "^3.0.1",
35+
"codegen.macro": "^4.1.0",
3336
"eslint": "^8.14.0",
3437
"eslint-config-prettier": "^8.5.0",
3538
"eslint-plugin-cypress": "^2.12.1",
3639
"eslint-plugin-prettier": "^4.2.1",
3740
"eslint-plugin-react": "^7.29.4",
3841
"eslint-plugin-react-hooks": "^4.5.0",
39-
"babel-jest": "^26.6.3",
40-
"babel-plugin-macros": "^3.0.1",
41-
"codegen.macro": "^4.1.0",
4242
"globby": "^11.0.2",
43-
"prismjs": "1.26.0",
44-
"patch-package": "^6.4.7",
4543
"prettier": "^2.8.7",
44+
"prismjs": "1.29.0",
4645
"react": "^18.2.0",
4746
"react-dom": "^18.2.0",
4847
"typescript": "^5.0.4"
48+
},
49+
"pnpm": {
50+
"patchedDependencies": {
51+
52+
}
4953
}
5054
}

packages/prism-react-renderer/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"dist"
1414
],
1515
"scripts": {
16-
"build": "patch-package && tsup",
17-
"build:watch": "patch-package && tsup --watch",
16+
"build": "tsup",
17+
"build:watch": "tsup --watch",
1818
"test": "vitest",
1919
"typecheck": "tsc --noEmit",
2020
"lint": "eslint 'src/**/*.{js,jsx,ts,tsx}'",

0 commit comments

Comments
 (0)