-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
ShikiHihlighter
component and use it in CodeHighlight
`ShikiHihlighter` uses the `useShiki` hook to highlight code and provides a component to easily use syntax highlighting The `ShikiHihlighter` component is then used in `CodeHighlight`, where `isInline` is used to determine what to render.
- Loading branch information
1 parent
f9dffe6
commit 33bda0e
Showing
2 changed files
with
41 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type React from 'react'; | ||
import { useShikiHighlighter } from '@/hooks/useShiki'; | ||
import type { BundledTheme, BundledLanguage } from 'shiki'; | ||
|
||
interface ShikiHighlighterProps { | ||
language: BundledLanguage; | ||
children: string; | ||
theme: BundledTheme; | ||
PreTag?: keyof JSX.IntrinsicElements; | ||
} | ||
|
||
export const ShikiHighlighter: React.FC<ShikiHighlighterProps> = ({ | ||
language, | ||
children, | ||
theme, | ||
PreTag = 'pre', | ||
}) => { | ||
const highlightedCode = useShikiHighlighter( | ||
children, | ||
language, | ||
theme, | ||
); | ||
|
||
return ( | ||
<PreTag 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} | ||
</PreTag> | ||
); | ||
}; |