Skip to content

Commit f8eae73

Browse files
Merge pull request #237 from FormidableLabs/task/export-util-fns
Export `normalizeTokens` and `useTokenize` utility functions.
2 parents e6d3233 + 0c20178 commit f8eae73

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

.changeset/tasty-cats-check.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"prism-react-renderer": patch
3+
---
4+
5+
Export `normalizeTokens` and `useTokenize` utility functions.

README.md

+40
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ _(If you just want to use your Prism CSS-file themes, that's also no problem)_
5454
- [prop getters](#prop-getters)
5555
- [`getLineProps`](#getlineprops)
5656
- [`getTokenProps`](#gettokenprops)
57+
- [Utility Functions](#utility-functions)
58+
- [`normalizeTokens`](#normalizetokens)
59+
- [`useTokenize`](#usetokenize)
5760
- [Theming](#theming)
5861
- [LICENSE](#license)
5962
- [Maintenance Status](#maintenance-status)
@@ -277,6 +280,43 @@ to the input.
277280
The `className` will always contain `.token`. This also provides full compatibility with
278281
your old Prism CSS-file themes.
279282

283+
## Utility Functions
284+
285+
### `useTokenize`
286+
287+
> `(options: TokenizeOptions) => Token[][]`
288+
289+
```ts
290+
type TokenizeOptions = {
291+
prism: PrismLib
292+
code: string
293+
grammar?: PrismGrammar
294+
language: Language
295+
}
296+
297+
```
298+
299+
This is a React hook that tokenizes code using Prism. It returns an array of tokens that can be rendered using the built-in `<Highlight />` component or your own custom component. It uses `normalizeTokens` internally to convert the tokens into a shape that can be rendered.
300+
301+
- `prism: PrismLib`: the Prism library to use for tokenization. This can be the vendored version of Prism that is included with `prism-react-renderer` or a custom version of Prism that you have configured.
302+
303+
- `code: string`: a string containing the code to tokenize.
304+
- `grammar?: PrismGrammar`: a Prism grammar object to use for tokenization. If this is omitted, the tokens will just be normalized. A grammar can be obtained from `Prism.languages` or by importing a language from `prismjs/components/`.
305+
- `language: Language`: the language to use for tokenization. This should be a language that Prism supports.
306+
307+
### `normalizeTokens`
308+
309+
> `(tokens: (PrismToken | string)[]) => Token[][]`
310+
311+
Takes an array of Prism’s tokens and groups them by line, converting strings into tokens. Tokens can become recursive in some cases which means that their types are concatenated. Plain-string tokens however are always of type `plain`.
312+
313+
- `PrismToken` is an internal alias for `Token` exported by `prismjs` and is defined [here](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/prismjs/index.d.ts#L347).
314+
315+
- `Token` is an internal object that represents a slice of tokenized content for Prism with three properties:
316+
- `types: string[]`: an array of types that indicate the purpose and styling of a piece of text
317+
- `content: string`: the content of the token
318+
- `empty: boolean`: a flag indicating whether the token is empty or not.
319+
280320
## Theming
281321

282322
The `defaultProps` you'd typically apply in a basic use-case, contain a default theme.

packages/prism-react-renderer/src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import * as themes from "./themes"
33
import { createElement } from "react"
44
import { Highlight as InternalHighlight } from "./components/highlight"
55
import { HighlightProps, PrismLib } from "./types"
6+
import normalizeTokens from "./utils/normalizeTokens"
7+
import { useTokenize } from "./components/useTokenize"
68
export * from "./types"
79

810
/**
@@ -17,4 +19,5 @@ const Highlight = (props: HighlightProps) =>
1719
code: props.code,
1820
language: props.language,
1921
})
20-
export { Highlight, Prism, themes }
22+
23+
export { Highlight, Prism, themes, normalizeTokens, useTokenize }

0 commit comments

Comments
 (0)