
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
react-shiki
Advanced tools
[!NOTE] This package is still a work in progress, fully functional but not extensively tested.
Performant client side syntax highlighting component + hook for react using Shiki
See the demo page with highlighted code blocks showcasing several Shiki themes!
ShikiHighlighter
component for highlighting code as children,
as well as a useShikiHighlighter
hook for more flexibilitydangerouslySetInnerHTML
, output from Shiki is parsed using html-react-parser
ShikiHighlighter
component displays a language label for each code block
when showLanguage
is set to true
(default)style
object or a className
[pnpm|bun|yarn|npm] install react-shiki
You can use the ShikiHighlighter
component, or the useShikiHighlighter
hook
to highlight code.
useShikiHighlighter
is a hook that takes in the code to be highlighted, the
language, and the theme, and returns the highlighted code as React elements.
It's useful for users who want full control over the rendering of highlighted
code.
const highlightedCode = useShikiHighlighter(code, language, theme, options);
The ShikiHighlighter
component is imported in your project, with the code to
be highlighted passed as it's children.
Shiki automatically handles dynamically importing only the languages and themes used on the page.
function CodeBlock() {
return (
<ShikiHighlighter language="jsx" theme="ayu-dark">
{code.trim()}
</ShikiHighlighter>
);
}
The ShikiHighlighter
component will follow a similar API to react-syntax-highlighter
, but uses Shiki and is optimized for performant sequential highlighting. As of now, not all of react-syntax-highlighter
functionality is supported, but the goal of this component is to eventually act as a drop in replacement for react-syntax-highlighter
.
The component accepts several props in addition to language and theme:
showLanguage: boolean
- Shows the language name in the top right corner of
the code blockaddDefaultStyles
: boolean - Adds default styles (padding, overflow handling,
and border-radius) to the code blockas: string
- The component to be rendered. Defaults to 'pre'delay: number
- Delay between highlights in milliseconds, useful for throttling
rapid highlighting on the clientclassName: string
- Class name to be passed to the componentstyle: object
- Inline style object to be passed to the componentlangStyle: object
- Inline style object to be passed to the language labelfunction Houston() {
return (
<ShikiHighlighter
language="jsx"
className="code-block"
theme="houston"
showLanguage={false}
addDefaultStyles={true}
as="div"
style={{
textAlign: "left",
}}
>
{code.trim()}
</ShikiHighlighter>
);
}
react-shiki
exports isInlineCode
to check if a code block is inline:
const isInline: boolean | undefined = node ? isInlineCode(node) : undefined;
return !isInline ? (
<ShikiHighlighter language={language} theme={"houston"} {...props}>
{String(children)}
</ShikiHighlighter>
) : (
<code className={className} {...props}>
{children}
</code>
);
react-markdown
import type { ReactNode } from "react";
import ShikiHighlighter, { type Element } from "react-shiki";
interface CodeHighlightProps {
className?: string | undefined;
children?: ReactNode | undefined;
node?: Element | undefined;
}
export const CodeHighlight = ({
className,
children,
node,
...props
}: CodeHighlightProps): JSX.Element => {
const match = className?.match(/language-(\w+)/);
const language = match ? match[1] : undefined;
<ShikiHighlighter language={language} theme={"houston"} {...props}>
{String(children)}
</ShikiHighlighter>;
};
Pass CodeHighlight
to react-markdown
as a code component:
import ReactMarkdown from "react-markdown";
import { CodeHighlight } from "./CodeHighlight";
<ReactMarkdown
components={{
code: CodeHighlight,
}}
>
{markdown}
</ReactMarkdown>;
import tokyoNight from '@styles/tokyo-night.mjs';
<ShikiHighlighter language="tsx" theme={tokyoNight}>
{String(code)}
</ShikiHighlighter>;
react-shiki
supports performance-optimized highlighting on the client.
Throttling real-time highlighting on the client is possible with the
delay
option.
const highlightedCode = useShikiHighlighter(code, language, theme, {
delay: 150,
});
react-shiki
can be used to highlight streamed code from LLM responses in real-time.
I use it for an LLM chatbot UI, it renders markdown and highlights code in memoized chat messages.
Using useShikiHighlighter
hook:
import type { ReactNode } from "react";
import { isInlineCode, useShikiHighlighter, type Element } from "react-shiki";
import tokyoNight from "@styles/tokyo-night.mjs";
interface CodeHighlightProps {
className?: string | undefined;
children?: ReactNode | undefined;
node?: Element | undefined;
}
export const CodeHighlight = ({
className,
children,
node,
...props
}: CodeHighlightProps) => {
const code = String(children);
const language = className?.match(/language-(\w+)/)?.[1];
const isInline = node ? isInlineCode(node) : false;
const highlightedCode = useShikiHighlighter(language, code, tokyoNight, {
delay: 150,
});
return !isInline ? (
<div
className="shiki not-prose relative [&_pre]:overflow-auto
[&_pre]:rounded-lg [&_pre]:px-6 [&_pre]:py-5"
>
{language ? (
<span
className="absolute right-3 top-2 text-xs tracking-tighter
text-muted-foreground/85"
>
{language}
</span>
) : null}
{highlightedCode}
</div>
) : (
<code className={className} {...props}>
{children}
</code>
);
};
Or using the ShikiHighlighter
component:
import type { ReactNode } from "react";
import ShikiHighlighter, { isInlineCode, type Element } from "react-shiki";
interface CodeHighlightProps {
className?: string | undefined;
children?: ReactNode | undefined;
node?: Element | undefined;
}
export const CodeHighlight = ({
className,
children,
node,
...props
}: CodeHighlightProps): JSX.Element => {
const match = className?.match(/language-(\w+)/);
const language = match ? match[1] : undefined;
const isInline: boolean | undefined = node ? isInlineCode(node) : undefined;
return !isInline ? (
<ShikiHighlighter
language={language}
theme={"houston"}
delay={150}
{...props}
>
{String(children)}
</ShikiHighlighter>
) : (
<code className={className} {...props}>
{children}
</code>
);
};
Passed to react-markdown
as a code
component in memo-ized chat messages:
const RenderedMessage = React.memo(({ message }: { message: Message }) => (
<div className={cn(messageStyles[message.role])}>
<ReactMarkdown components={{ code: CodeHighlight }}>
{message.content}
</ReactMarkdown>
</div>
));
export const ChatMessages = ({ messages }: { messages: Message[] }) => {
return (
<div className="space-y-4">
{messages.map((message) => (
<RenderedMessage key={message.id} message={message} />
))}
</div>
);
};
FAQs
Syntax highlighter component for react using shiki
The npm package react-shiki receives a total of 4,943 weekly downloads. As such, react-shiki popularity was classified as popular.
We found that react-shiki demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.