You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+40
Original file line number
Diff line number
Diff line change
@@ -54,6 +54,9 @@ _(If you just want to use your Prism CSS-file themes, that's also no problem)_
54
54
-[prop getters](#prop-getters)
55
55
-[`getLineProps`](#getlineprops)
56
56
-[`getTokenProps`](#gettokenprops)
57
+
-[Utility Functions](#utility-functions)
58
+
-[`normalizeTokens`](#normalizetokens)
59
+
-[`useTokenize`](#usetokenize)
57
60
-[Theming](#theming)
58
61
-[LICENSE](#license)
59
62
-[Maintenance Status](#maintenance-status)
@@ -277,6 +280,43 @@ to the input.
277
280
The `className` will always contain `.token`. This also provides full compatibility with
278
281
your old Prism CSS-file themes.
279
282
283
+
## Utility Functions
284
+
285
+
### `useTokenize`
286
+
287
+
> `(options: TokenizeOptions) => Token[][]`
288
+
289
+
```ts
290
+
typeTokenizeOptions= {
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.
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
+
280
320
## Theming
281
321
282
322
The `defaultProps` you'd typically apply in a basic use-case, contain a default theme.
0 commit comments