Skip to content

Commit dc1b948

Browse files
AVGVSTVS96claude
andcommitted
docs: add tokens output format to README
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 93a2b97 commit dc1b948

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

package/README.md

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ A performant client-side syntax highlighting component and hook for React, built
4040
## Features
4141

4242
- 🖼️ Provides both a `ShikiHighlighter` component and a `useShikiHighlighter` hook for more flexibility
43-
- 🔐 Flexible output: Choose between React elements (no `dangerouslySetInnerHTML`) or HTML strings for better performance
43+
- 🔐 Flexible output: React elements (safe), HTML strings (fast), or raw tokens (custom rendering)
4444
- 📦 Multiple bundle options: Full bundle (~1.2MB gz), web bundle (~695KB gz), or minimal core bundle for fine-grained bundle control
4545
- 🖌️ Full support for custom TextMate themes and languages
4646
- 🔧 Supports passing custom Shiki transformers to the highlighter, in addition to all other options supported by `codeToHast`
@@ -219,7 +219,7 @@ See [Shiki - RegExp Engines](https://shiki.style/guide/regex-engines) for more i
219219
| `transformers` | `array` | `[]` | Custom Shiki transformers for modifying the highlighting output |
220220
| `cssVariablePrefix` | `string` | `'--shiki'` | Prefix for CSS variables storing theme colors |
221221
| `defaultColor` | `string \| false` | `'light'` | Default theme mode when using multiple themes, can also disable default theme |
222-
| `outputFormat` | `string` | `'react'` | Output format: 'react' for React nodes, 'html' for HTML string |
222+
| `outputFormat` | `string` | `'react'` | Output format: `'react'`, `'html'`, or `'tokens'` (hook only) |
223223
| `tabindex` | `number` | `0` | Tab index for the code block |
224224
| `decorations` | `array` | `[]` | Custom decorations to wrap the highlighted tokens with |
225225
| `structure` | `string` | `classic` | The structure of the generated HAST and HTML - `classic` or `inline` |
@@ -623,33 +623,44 @@ const highlightedCode = useShikiHighlighter(code, "tsx", "github-dark", {
623623

624624
### Output Format Optimization
625625

626-
`react-shiki` provides two output formats to balance safety and performance:
626+
`react-shiki` provides three output formats:
627627

628628
**React Nodes (Default)** - Safer, no `dangerouslySetInnerHTML` required
629629
```tsx
630-
// Hook
631630
const highlightedCode = useShikiHighlighter(code, "tsx", "github-dark");
632-
633-
// Component
634-
<ShikiHighlighter language="tsx" theme="github-dark">
635-
{code}
636-
</ShikiHighlighter>
631+
// Returns: ReactNode
637632
```
638633

639-
**HTML String** - 15-45% faster performance
634+
**HTML String** - Faster, requires `dangerouslySetInnerHTML`
640635
```tsx
641-
// Hook (returns HTML string, use dangerouslySetInnerHTML to render)
642-
const highlightedCode = useShikiHighlighter(code, "tsx", "github-dark", {
636+
const html = useShikiHighlighter(code, "tsx", "github-dark", {
643637
outputFormat: 'html'
644638
});
639+
// Returns: string
640+
```
645641

646-
// Component (automatically uses dangerouslySetInnerHTML when outputFormat is 'html')
647-
<ShikiHighlighter language="tsx" theme="github-dark" outputFormat="html">
648-
{code}
649-
</ShikiHighlighter>
642+
**Tokens** - Raw token data for custom rendering (hook only)
643+
```tsx
644+
import { useShikiHighlighter, type TokensResult } from "react-shiki";
645+
646+
const result = useShikiHighlighter(code, "tsx", "github-dark", {
647+
outputFormat: 'tokens'
648+
});
649+
// Returns: TokensResult { tokens, fg, bg, themeName, rootStyle }
650+
651+
// Build your own rendering
652+
result?.tokens.map((line, i) => (
653+
<div key={i}>
654+
{line.map((token, j) => (
655+
<span key={j} style={{ color: token.color }}>{token.content}</span>
656+
))}
657+
</div>
658+
));
650659
```
651660

652-
Choose HTML output when performance is critical and you trust the code source. Use the default React output when handling untrusted content or when security is the primary concern.
661+
The component supports `'react'` and `'html'` formats. For `'tokens'`, use the hook directly.
662+
663+
Choose HTML when performance is critical and you trust the source. Use tokens when you need full control over rendering.
653664

654665
---
655666

0 commit comments

Comments
 (0)