### Prism React Renderer Local Development Setup Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md These shell commands outline the steps to set up the local development environment for `prism-react-renderer`. They cover installing project dependencies, building the project, and running tests using pnpm. ```shell $ pnpm install $ pnpm build $ pnpm test ``` -------------------------------- ### Using a Custom Prism Instance with Highlight Component Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md This example illustrates how to provide your own `Prismjs` library instance to the `Highlight` component via the `prism` prop. This is useful for advanced setups where you want to control the Prism version or configuration, preventing conflicts with other `prismjs` installations. ```jsx // Whichever way you're retrieving Prism here: import Prism from 'prismjs/components/prism-core'; ``` -------------------------------- ### Installing PrismJS for Custom Language Support Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md These shell commands provide instructions for installing the `prismjs` package using npm, yarn, or pnpm. Installing `prismjs` is a prerequisite for adding and using custom language components with `prism-react-renderer`. ```shell # npm npm install --save prismjs # yarn yarn add prismjs # pnpm pnpm add prismjs ``` -------------------------------- ### Running Prism React Renderer Local Demo Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md These commands explain how to run the local demo site for `prism-react-renderer`. It requires building the project first, then starting the demo server to view changes in a browser. ```shell $ pnpm build $ pnpm start:demo ``` -------------------------------- ### Install Prism React Renderer Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md Commands to install the `prism-react-renderer` package as a dependency in your project using npm, yarn, or pnpm. ```shell # npm npm install --save prism-react-renderer # yarn yarn add prism-react-renderer # pnpm pnpm add prism-react-renderer ``` -------------------------------- ### Watching for Changes During Prism React Renderer Development Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md These commands describe how to set up a development workflow with hot reloading for `prism-react-renderer`. By running `pnpm build:watch` in one terminal and `pnpm start:demo` in another, changes to workspace projects are automatically reloaded. ```shell // terminal 1 $ pnpm build:watch ``` ```shell // terminal 2 $ pnpm start:demo ``` -------------------------------- ### Highlight Component API Reference Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md Comprehensive API documentation for the `Highlight` component, detailing its required and optional props, as well as the structure of the `highlight` object passed to its children render function. This includes properties for tokens, class names, and theming. ```APIDOC Highlight Component: Props: children: function({}) (required) - Description: A render prop function that receives an object containing highlighting utilities and state. - Parameters: - highlight: object - tokens: Token[][] (Type: Array>) - Description: A doubly nested array of tokens. The outer array represents lines, and the inner array contains tokens for each line. - Token structure: { types: string[], content: string } - className: string - Description: The CSS class to apply to the wrapping element, typically a `
` tag, for styling.

  language: string (required)
    - Description: Specifies the programming language for the code to be highlighted. Refer to `prism-react-renderer` documentation for supported languages.

  code: string (required)
    - Description: The actual code string that needs to be highlighted.

  theme: PrismTheme (optional; default is vsDark)
    - Description: An object defining the theme to be used for highlighting. This theme generates style props accessible via the children function's prop-getters.

  prism: prism (optional; default is the vendored version)
    - Description: Allows passing a custom `Prismjs` library instance. By default, `prism-react-renderer` uses its own vendored, slimmed-down version of Prism that doesn't pollute the global namespace.
```

--------------------------------

### Basic Usage of Highlight Component

Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md

Demonstrates how to use the `Highlight` component from `prism-react-renderer` to render syntax-highlighted code blocks in a React application. It shows importing the component and themes, passing code and language props, and using the render prop to iterate over lines and tokens for custom rendering.

```tsx
import React from "react"
import ReactDOM from "react-dom/client"
import { Highlight, themes } from "prism-react-renderer"
import styles from 'styles.module.css'

const codeBlock = `
const GroceryItem: React.FC = ({ item }) => {
  return (
    

{item.name}

Price: {item.price}

Quantity: {item.quantity}

); } ` export const App = () => ( {({ className, style, tokens, getLineProps, getTokenProps }) => (
        {tokens.map((line, i) => (
          
{i + 1} {line.map((token, key) => ( ))}
)}
)}
) ReactDOM .createRoot(document.getElementById("root") as HTMLElement) .render() ``` -------------------------------- ### Migrating Prism React Renderer Theme Imports (v1 to v2) Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md This diff illustrates the updated syntax for importing themes in `prism-react-renderer` when migrating from version 1.x to 2.x. Themes are now accessed directly from the `themes` property of the main `prism-react-renderer` export. ```diff - const theme = require('prism-react-renderer/themes/github') + const theme = require('prism-react-renderer').themes.github ``` -------------------------------- ### Migrating Prism React Renderer Module Imports (v1 to v2) Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md This diff highlights the necessary changes for module imports when upgrading `prism-react-renderer` from version 1.x to 2.x. It shows the transition from importing `Highlight` and `defaultProps` separately to a single destructured import for `Highlight`. ```diff - import Highlight, { defaultProps } from "prism-react-renderer"; + import { Highlight } from "prism-react-renderer" const Content = ( - + ``` -------------------------------- ### Importing Custom Prism Languages in JavaScript Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md This snippet demonstrates how to dynamically import additional language definitions for Prism, allowing `prism-react-renderer` to highlight languages not included in its default bundle. It shows both `await import()` and `require()` methods, emphasizing the need to ensure `window.Prism` is available. ```javascript 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") ``` -------------------------------- ### Prism React Renderer Utility Hook: useTokenize Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md A React hook that tokenizes code using the Prism library. It returns an array of token arrays, suitable for rendering with the built-in `` component or custom components. It internally uses `normalizeTokens` to format the output. ```ts type TokenizeOptions = { prism: PrismLib code: string grammar?: PrismGrammar language: Language } ``` ```APIDOC useTokenize(options: TokenizeOptions): Token[][] - Description: React hook to tokenize code using Prism. - Parameters: - options: TokenizeOptions - An object containing tokenization parameters. - prism: PrismLib - The Prism library instance to use for tokenization. Can be the vendored version or a custom one. - code: string - The string containing the code to tokenize. - grammar?: PrismGrammar - Optional. A Prism grammar object to use. If omitted, tokens are only normalized. Can be obtained from `Prism.languages` or by importing from `prismjs/components/`. - language: Language - The language to use for tokenization, must be supported by Prism. - Returns: Token[][] - An array of arrays, where each inner array represents a line of tokens. ``` -------------------------------- ### Using a Built-in Theme with Prism React Renderer Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md This snippet demonstrates how to apply a built-in theme, such as 'dracula' from `prism-react-renderer/themes`, to the `Highlight` component. Built-in themes are JSON-based and inspired by VSCode's theme format. ```jsx import { Highlight, themes } from 'prism-react-renderer'; ``` -------------------------------- ### Prism React Renderer Theme Type Definition Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md This TypeScript type definition outlines the structure of a `PrismTheme` object used by `prism-react-renderer`. It includes a `plain` property for base styles and a `styles` array for specific token type and language-based styling. ```ts export type PrismTheme = { plain: PrismThemeEntry styles: Array<{ types: string[] style: PrismThemeEntry languages?: Language[] }> } ``` -------------------------------- ### Adding Custom Language Components to Prism React Renderer Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md This JavaScript/TypeScript snippet demonstrates how to dynamically add support for additional Prism languages, such as 'applescript'. It involves setting `window.Prism` (or `global.Prism` in Node.js) and then importing the specific language component. ```ts 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") ``` -------------------------------- ### Prism React Renderer Prop Getters Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md Functions provided by `prism-react-renderer` to apply props to elements for rendering tokenized code, offering maximum flexibility. They correctly override and modify props, so passing props to them is advisable. ```APIDOC getLineProps(options: { line: Token[], className?: string, style?: object, children?: React.ReactNode, ...otherProps }): object - Description: Returns props to spread onto line elements (typically
s) containing tokens. - Parameters: - line: Token[] - Required. The array of tokens for the line. - className?: string - Optional. A CSS class name to append. The returned className will always contain `.token-line`. - style?: object - Optional. A style object to merge. - children?: React.ReactNode - Optional. Child elements. - ...otherProps: Any other props to pass through. - Returns: An object containing props (e.g., className, children, style) to apply to the line element. getTokenProps(options: { token: Token, className?: string, style?: object, children?: React.ReactNode, ...otherProps }): object - Description: Returns props to spread onto elements displaying individual tokens (typically s). - Parameters: - token: Token - Required. The token object to process. - className?: string - Optional. A CSS class name to append. The returned className will always contain `.token`. - style?: object - Optional. A style object to merge. - children?: React.ReactNode - Optional. Child elements. - ...otherProps: Any other props to pass through. - Returns: An object containing props (e.g., className, children, style) to apply to the token element. ``` -------------------------------- ### Rendering with Highlight Component's Children Function Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md This snippet shows the typical usage of the `Highlight` component's render prop (children function). The function receives a `highlight` object, which contains properties like `className` and `tokens` that are used to render the highlighted code within a `
` element.

```javascript
const ui = (
  
    {highlight => (
      // use utilities and prop getters here, like highlight.className, highlight.getTokenProps, etc.
      
{/* more jsx here */}
)}
); ``` -------------------------------- ### Disabling Built-in Theme in Prism React Renderer Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md To use a CSS-based theme instead of a built-in one, this snippet shows how to provide an `emptyTheme` object to the `Highlight` component. This effectively disables the default theme application, allowing external CSS to control styling. ```ts const emptyTheme = { plain: {}, styles: [] }; ``` -------------------------------- ### Prism React Renderer Utility Function: normalizeTokens Source: https://github.com/formidablelabs/prism-react-renderer/blob/master/README.md A utility function that takes an array of Prism's raw tokens and groups them by line, converting any plain strings into token objects. This function is crucial for preparing tokens for rendering, especially handling recursive token structures and ensuring consistent typing. ```APIDOC normalizeTokens(tokens: (PrismToken | string)[]): Token[][] - Description: Groups Prism's tokens by line and converts strings into token objects, handling recursive types. - Parameters: - tokens: (PrismToken | string)[] - An array of raw tokens from Prism, which can be `PrismToken` objects or plain strings. - Returns: Token[][] - An array of arrays, where each inner array represents a line of normalized tokens. - Notes: - `PrismToken` is an internal alias for `Token` exported by `prismjs`. - `Token` is an internal object representing a slice of tokenized content with properties: - `types: string[]`: An array of types indicating purpose and styling. - `content: string`: The content of the token. - `empty: boolean`: A flag indicating if the token is empty. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.