Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/afraid-geese-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"prism-react-renderer": major
---

v2 release with updated API
87 changes: 14 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ _(If you just want to use your Prism CSS-file themes, that's also no problem)_

- [Installation](#installation)
- [Usage](#usage)
- [Custom Language Support](#custom-language-support)
- [Basic Props](#basic-props)
- [children](#children)
- [language](#language)
Expand All @@ -53,7 +54,6 @@ _(If you just want to use your Prism CSS-file themes, that's also no problem)_
- [`getLineProps`](#getlineprops)
- [`getTokenProps`](#gettokenprops)
- [Theming](#theming)
- [FAQ](#faq)
- [LICENSE](#license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->
Expand All @@ -74,7 +74,7 @@ pnpm add prism-react-renderer

> Prism React Renderer has a peer dependency on `react`

### How to use Prism React Renderer
### Usage
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.

```tsx
Expand Down Expand Up @@ -121,6 +121,18 @@ ReactDOM.createRoot(document.getElementById("root") as HTMLElement)

```

### Custom Language Support
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @ryan-roemer I took your suggestion to bring this section to the forefront and verified the methods worked via our demo app


By default `prism-react-renderer` only includes a [base set of languages](https://github.com/FormidableLabs/prism-react-renderer/blob/c914fdea48625ba59c8022174bb3df1ed802ce4d/packages/generate-prism-languages/index.ts#L9-L23) that Prism supports. **Depending on your app's build system you may need to `await` the `import` or use `require` to ensure `window.Prism` exists before importing the custom languages.** You can add support for more by including their definitions from the main `prismjs` package:

```js
import { Highlight, Prism } from "prism-react-renderer";

(typeof global !== "undefined" ? global : window).Prism = Prism
await import("prismjs/components/prism-applescript")
/** or **/
require("prismjs/components/prism-applescript")
```


## Basic Props
Expand Down Expand Up @@ -306,77 +318,6 @@ property limits styles to highlighted languages.
When converting a Prism CSS theme it's mostly just necessary to use classes as
`types` and convert the declarations to object-style-syntax and put them on `style`.

## FAQ

<details>

<summary>How do I add more language highlighting support?</summary>

By default `prism-react-renderer` only includes an [arbitrary subset of the languages](https://github.com/FormidableLabs/prism-react-renderer/blob/master/src/vendor/prism/includeLangs.js) that Prism supports. You can add support for more by including their definitions from the main `prismjs` package:

```js
import Prism from "prism-react-renderer/prism";

(typeof global !== "undefined" ? global : window).Prism = Prism;

require("prismjs/components/prism-kotlin");
require("prismjs/components/prism-csharp");
```
</details>

<details>

<summary>How do I use my old Prism css themes?</summary>

`prism-react-renderer` still returns you all proper `className`s via the prop getters,
when you use it. By default however it uses its new theming system, which output a
couple of `style` props as well.

If you don't pass `theme` to the `<Highlight />` component it will default to not
outputting any `style` props, while still returning you the `className` props, like
so:

```js
<Highlight
{...defaultProps}
code={exampleCode}
language="jsx"
theme={undefined}
>
{highlight => null /* ... */}
</Highlight>
```

</details>

<details>

<summary>How do I prevent a theme and the vendored Prism to be bundled?</summary>

Since the default theme and the vendored Prism library in `prism-react-renderer`
come from `defaultProps`, if you wish to pass your own Prism library in, and not
use the built-in theming, you simply need to leave it out to allow your bundler
to tree-shake those:

```js
import Highlight from "prism-react-renderer";
import Prism from "prismjs"; // Different source

<Highlight Prism={Prism} code={exampleCode} language="jsx">
{highlight => null /* ... */}
</Highlight>;
```

You can also import the vendored Prism library on its own:

```js
import { Prism } from "prism-react-renderer";
// or
import Prism from "prism-react-renderer/prism";
```

</details>

## LICENSE

MIT
Expand Down
2 changes: 2 additions & 0 deletions packages/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "^18.15.11",
"@types/prismjs": "^1.26.0",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@vitejs/plugin-react": "^3.1.0",
Expand Down
8 changes: 7 additions & 1 deletion packages/demo/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Highlight, themes } from "prism-react-renderer"
import { Highlight, Prism, themes } from "prism-react-renderer"
import styles from "./app.module.css"
import clsx from "clsx"
import { ProjectBadge } from "formidable-oss-badges"
import { useState } from "react"
import { sampleCode } from "./sample-code"

// Example of importing a custom language directly from Prism
;(typeof global !== "undefined" ? global : window).Prism = Prism
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
await import("prismjs/components/prism-applescript")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👇🏻 example of AppleScript highlighting. As a note TypeScript doesn't love importing languages from prismjs/components so we need to @ts-ignore it, despite the exports existing.

Screenshot 2023-04-21 at 8 07 37 AM

type SampleCodeType = keyof typeof sampleCode
type ThemeType = keyof typeof themes

Expand Down
28 changes: 28 additions & 0 deletions packages/demo/src/sample-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,34 @@ const GroceryItem = new Proxy({}, {
`,
},

["HTML"]: {
language: "html",
code: `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Formidable</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
`,
},

["AppleScript"]: {
language: "applescript",
code: `
display alert "Do you wish to buy groceries?" buttons {"No", "Yes"}
set theAnswer to button returned of the result
if theAnswer is "Yes" then
beep 5
end if
`,
},

["Rust"]: {
language: "rust",
code: `
Expand Down
1 change: 1 addition & 0 deletions packages/generate-prism-languages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const languagesToBundle = <const>[
"go",
"cpp",
"markdown",
"html",
]

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/prism-react-renderer/src/components/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { useTokenize } from "./useTokenize"

export const Highlight = ({
children,
language,
language: _language,
code,
theme,
prism,
}: InternalHighlightProps) => {
const language = _language.toLowerCase()
const themeDictionary = useThemeDictionary(language, theme)
const getLineProps = useGetLineProps(themeDictionary)
const getTokenProps = useGetTokenProps(themeDictionary)
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.