### Install react-shiki Source: https://github.com/avgvstvs96/react-shiki/blob/main/README.md Install the package via npm. ```bash npm i react-shiki ``` -------------------------------- ### Core Bundle Setup and Usage Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/migration-guide.md The Core bundle requires manual initialization of the highlighter instance and explicit imports for themes and languages. ```typescript import ShikiHighlighter from 'react-shiki'; {code} ``` ```typescript import { useShikiHighlighter, createHighlighterCore, createOnigurumaEngine, } from 'react-shiki/core'; // Create once (typically at app initialization or in a Context) const highlighter = await createHighlighterCore({ themes: [import('@shikijs/themes/github-dark')], langs: [import('@shikijs/langs/typescript')], engine: createOnigurumaEngine(import('shiki/wasm')), }); // Use with hook const highlighted = useShikiHighlighter(code, 'typescript', 'github-dark', { highlighter, }); // Or with component import { ShikiHighlighter } from 'react-shiki/core'; {code} ``` -------------------------------- ### Usage Example with ShikiHighlighter Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/api-reference/transformers.md Demonstrates how to integrate line highlighting within a React component using the ShikiHighlighter. ```typescript import ShikiHighlighter from 'react-shiki'; function CodeBlock({ code }) { return ( {code} ); } ``` -------------------------------- ### Implement Multi-Theme Configurations Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/types.md Examples of defining theme maps using standard strings or custom theme objects. ```typescript // Dual theme const themes: Themes = { light: 'github-light', dark: 'github-dark' }; // Multiple themes const themes: Themes = { light: 'github-light', dark: 'github-dark', dim: 'github-dark-dimmed', highContrast: 'dracula' }; // Custom theme objects const themes: Themes = { light: customLightTheme, dark: customDarkTheme }; ``` -------------------------------- ### Implement Multi-Bundle Setup Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/bundles.md Demonstrates importing the web bundle for standard pages and the core bundle for specialized, language-heavy pages to optimize performance. ```typescript // Use web bundle for most pages import ShikiHighlighter from 'react-shiki/web'; // Use core bundle for heavy-language pages import { useShikiHighlighter, createHighlighterCore, createOnigurumaEngine } from 'react-shiki/core'; // In a Rust/Go heavy page const heavyHighlighter = await createHighlighterCore({ themes: [import('@shikijs/themes/github-dark')], langs: [ import('@shikijs/langs/rust'), import('@shikijs/langs/go'), import('@shikijs/langs/cpp') ], engine: createOnigurumaEngine(import('shiki/wasm')) }); // In typical web pages, use web bundle {code} ``` -------------------------------- ### Example usage of useShikiHighlighter Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/api-reference/utilities.md Demonstrates passing a theme object structure that benefits from value stabilization. ```typescript useShikiHighlighter(code, 'typescript', { light: 'github-light', dark: 'github-dark' }) ``` -------------------------------- ### Theme Usage Examples Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/types.md Demonstrates assigning built-in theme strings or custom theme objects to the Theme type. ```typescript // Built-in theme const theme: Theme = 'github-dark'; // Custom theme object const theme: Theme = { name: 'my-theme', type: 'dark', colors: { 'editor.background': '#1e1e1e', 'editor.foreground': '#d4d4d4', }, tokenColors: [ { scope: ['keyword'], settings: { foreground: '#569cd6' } } ] }; ``` -------------------------------- ### Implement full syntax highlighting with inline detection Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/api-reference/plugins.md A comprehensive example combining syntax highlighting, language detection, and inline code handling. ```typescript import ReactMarkdown from 'react-markdown'; import ShikiHighlighter, { isInlineCode, rehypeInlineCodeProperty } from 'react-shiki'; interface CodeHighlightProps { inline?: boolean; className?: string; children: string; node?: any; } function CodeHighlight({ inline, className, children, node, ...props }: CodeHighlightProps) { const code = String(children).trim(); const match = className?.match(/language-(\w+)/); const language = match ? match[1] : undefined; // Determine if inline (fallback to isInlineCode if inline prop not set) const isCodeInline = inline ?? (node ? isInlineCode(node) : false); return !isCodeInline ? ( {code} ) : ( {code} ); } export default function DocumentationPage({ markdown }) { return ( {markdown} ); } ``` -------------------------------- ### Stabilize hook dependencies Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/api-reference/utilities.md Example of stabilizing multiple inputs before passing them to useMemo. ```typescript const stableLang = useStableValue(lang); const stableTheme = useStableValue(themeInput); const stableOpts = useStableValue(options); const resolved = useMemo(() => { // ... }, [stableLang, stableTheme, stableOpts]); ``` -------------------------------- ### Configure ShikiHighlighter with delay Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/api-reference/utilities.md Example of passing a delay prop to the ShikiHighlighter component to enable throttling. ```jsx {realTimeCode} ``` -------------------------------- ### Configuring Starting Line Numbers Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/api-reference/transformers.md Adjust the starting index for line numbering using the startingLineNumber prop. ```typescript // Start numbering from 0 {code} ``` ```typescript // Start numbering from 10 {code} ``` -------------------------------- ### Access Theme CSS Variables Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/styling.md Example of applying theme-generated CSS variables to a container element. ```css .code-block { background: var(--shiki-background); color: var(--shiki-foreground); border: 1px solid var(--shiki-color-3); } ``` -------------------------------- ### Language Usage Examples Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/types.md Demonstrates assigning string identifiers, custom grammar objects, or undefined to the Language type. ```typescript // String identifier const lang: Language = 'typescript'; // Custom grammar object const lang: Language = { name: 'mcfunction', scopeName: 'source.mcfunction', // ... other grammar properties }; // Undefined const lang: Language = undefined; ``` -------------------------------- ### Access Custom Theme CSS Variables Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/styling.md Example of using custom-prefixed CSS variables in a stylesheet. ```css background: var(--my-code-background); color: var(--my-code-foreground); ``` -------------------------------- ### Set Custom Theme CSS Variable Prefix Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/styling.md Example of configuring a custom prefix to avoid CSS variable naming conflicts. ```typescript {code} ``` -------------------------------- ### Set Default Theme CSS Variable Prefix Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/styling.md Example of using the default CSS variable prefix for theme colors in the ShikiHighlighter component. ```typescript {code} ``` -------------------------------- ### Initialize Minimal Bundle with Custom Highlighter Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/configuration.md Use createHighlighterCore to define specific themes and languages for a lightweight bundle. Requires importing the engine and language/theme modules. ```typescript import { useShikiHighlighter, createHighlighterCore, createJavaScriptRegexEngine } from 'react-shiki/core'; const highlighter = await createHighlighterCore({ themes: [import('@shikijs/themes/nord')], langs: [import('@shikijs/langs/typescript'), import('@shikijs/langs/javascript')], engine: createJavaScriptRegexEngine() }); const highlighted = useShikiHighlighter(code, 'typescript', 'nord', { highlighter, showLineNumbers: true }); ``` -------------------------------- ### Configure Minimal Bundle Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/INDEX.md Initialize a core highlighter instance to reduce bundle size by importing only necessary themes and languages. ```typescript const highlighter = await createHighlighterCore({ themes: [import('@shikijs/themes/nord')], langs: [import('@shikijs/langs/typescript')], engine: createJavaScriptRegexEngine() }); {code} ``` -------------------------------- ### Library Entry Points Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/INDEX.md Import paths for different bundle sizes and configurations. ```typescript // Full bundle (all languages & themes) import ShikiHighlighter from 'react-shiki'; import { useShikiHighlighter } from 'react-shiki'; // Web bundle (web-focused languages) import ShikiHighlighter from 'react-shiki/web'; import { useShikiHighlighter } from 'react-shiki/web'; // Core bundle (minimal, custom configuration) import ShikiHighlighter from 'react-shiki/core'; import { useShikiHighlighter, createHighlighterCore, createOnigurumaEngine, createJavaScriptRegexEngine } from 'react-shiki/core'; // CSS (for line numbers) import 'react-shiki/css'; ``` -------------------------------- ### Import and Configure Core Bundle Source: https://github.com/avgvstvs96/react-shiki/blob/main/package/README.md Uses the minimal core bundle to create a custom highlighter with dynamic imports for optimized bundle size. ```tsx import ShikiHighlighter, { createHighlighterCore, // re-exported from shiki/core createOnigurumaEngine, // re-exported from shiki/engine/oniguruma createJavaScriptRegexEngine, // re-exported from shiki/engine/javascript } from 'react-shiki/core'; // Create custom highlighter with dynamic imports to optimize client-side bundle size const highlighter = await createHighlighterCore({ themes: [import('@shikijs/themes/nord')], langs: [import('@shikijs/langs/typescript')], engine: createOnigurumaEngine(import('shiki/wasm')) // or createJavaScriptRegexEngine() }); {code} ``` -------------------------------- ### Apply Inline Styles to Component Source: https://github.com/avgvstvs96/react-shiki/blob/main/README.md Example of overriding CSS variables directly on the component using the style prop. ```tsx {code} ``` -------------------------------- ### Migrate to Core Bundle Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/bundles.md Manual highlighter initialization using the core bundle for granular control over themes, languages, and engines. ```typescript // Before import ShikiHighlighter from 'react-shiki'; // After import { useShikiHighlighter, createHighlighterCore, createJavaScriptRegexEngine } from 'react-shiki/core'; // Requires creating a highlighter const highlighter = await createHighlighterCore({ themes: [import('@shikijs/themes/github-dark')], langs: [import('@shikijs/langs/typescript')], engine: createJavaScriptRegexEngine() }); // Pass to hook/component const highlighted = useShikiHighlighter(code, 'typescript', 'github-dark', { highlighter }); ``` -------------------------------- ### Configure Custom Highlighter Bundles Source: https://github.com/avgvstvs96/react-shiki/blob/main/playground/src/Demo.mdx Optimizes bundle size by using dynamic imports for themes, languages, and engines. ```tsx import ShikiHighlighter, { createHighlighterCore, // re-exported from shiki/core createOnigurumaEngine, // re-exported from shiki/engine/oniguruma createJavaScriptRegexEngine, // re-exported from shiki/engine/javascript } from 'react-shiki/core'; // Create custom highlighter with dynamic imports to optimize client-side bundle size const highlighter = await createHighlighterCore({ themes: [import('@shikijs/themes/ayu-dark')], langs: [import('@shikijs/langs/typescript')], engine: createOnigurumaEngine(import('shiki/wasm')) // or createJavaScriptRegexEngine() }); {code.trim()} ``` -------------------------------- ### Configure Core Highlighter Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/bundles.md Initialize a custom highlighter instance by explicitly importing required themes and languages. ```typescript const highlighter = await createHighlighterCore({ themes: [ import('@shikijs/themes/github-light'), import('@shikijs/themes/github-dark'), ], langs: [ import('@shikijs/langs/javascript'), import('@shikijs/langs/typescript'), import('@shikijs/langs/python'), ], engine: createJavaScriptRegexEngine() }); ``` -------------------------------- ### Handle inline prop in CodeHighlight Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/api-reference/plugins.md Example of a CodeHighlight component consuming the inline property injected by the rehype plugin. ```typescript interface CodeHighlightProps { inline?: boolean; className?: string; children: ReactNode; node?: Element; [key: string]: any; } const CodeHighlight = ({ inline, className, children, node, ...props }: CodeHighlightProps): JSX.Element => { const match = className?.match(/language-(\w+)/); const language = match ? match[1] : undefined; const code = String(children).trim(); return !inline ? ( {code} ) : ( {code} ); }; ``` -------------------------------- ### Update TypeScript definitions Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/migration-guide.md Install the latest version of React type definitions to resolve potential TypeScript compatibility errors. ```bash npm install --save-dev @types/react@latest ``` -------------------------------- ### Implement throttleHighlighting in a component Source: https://github.com/avgvstvs96/react-shiki/blob/main/_autodocs/api-reference/utilities.md Example of using throttleHighlighting within a React component to manage expensive operations during input changes. ```typescript import { throttleHighlighting } from './lib/utils'; import type { TimeoutState } from './lib/types'; function LiveEditor({ code }) { const timeoutControl = useRef({ nextAllowedTime: 0, timeoutId: undefined }); const highlight = async () => { // Expensive highlighting operation }; const handleCodeChange = () => { throttleHighlighting(highlight, timeoutControl, 150); }; return (