11"use client" ;
22
33import type { JSX } from "react" ;
4- import {
5- bundledLanguages ,
6- codeToHast ,
7- type BundledLanguage ,
8- } from "shiki/bundle/web" ;
9- import { Fragment , useLayoutEffect , useState } from "react" ;
10- import { jsx , jsxs } from "react/jsx-runtime" ;
11- import { toJsxRuntime } from "hast-util-to-jsx-runtime" ;
12- import { safe } from "ts-safe" ;
134import { cn } from "lib/utils" ;
14- import { useTheme } from "next-themes" ;
155import { Button } from "ui/button" ;
166import { CheckIcon , CopyIcon } from "lucide-react" ;
177import JsonView from "ui/json-view" ;
@@ -76,15 +66,7 @@ const PurePre = ({
7666 ) ;
7767} ;
7868
79- export async function Highlight (
80- code : string ,
81- lang : BundledLanguage | ( string & { } ) ,
82- theme : string ,
83- ) {
84- const parsed : BundledLanguage = (
85- bundledLanguages [ lang ] ? lang : "md"
86- ) as BundledLanguage ;
87-
69+ export function Highlight ( code : string , lang : string ) : JSX . Element {
8870 if ( lang === "json" ) {
8971 return (
9072 < PurePre code = { code } lang = { lang } >
@@ -101,50 +83,22 @@ export async function Highlight(
10183 ) ;
10284 }
10385
104- const out = await codeToHast ( code , {
105- lang : parsed ,
106- theme,
107- } ) ;
108-
109- return toJsxRuntime ( out , {
110- Fragment,
111- jsx,
112- jsxs,
113- components : {
114- pre : ( props ) => < PurePre { ...props } code = { code } lang = { lang } /> ,
115- } ,
116- } ) as JSX . Element ;
86+ return (
87+ < PurePre code = { code } lang = { lang } >
88+ < code className = { `language-${ lang } ` } > { code } </ code >
89+ </ PurePre >
90+ ) ;
11791}
11892
11993export function PreBlock ( { children } : { children : any } ) {
12094 const code = children . props . children ;
121- const { theme } = useTheme ( ) ;
12295 const language = children . props . className ?. split ( "-" ) ?. [ 1 ] || "bash" ;
123- const [ loading , setLoading ] = useState ( true ) ;
124- const [ component , setComponent ] = useState < JSX . Element | null > (
125- < PurePre className = "animate-pulse" code = { code } lang = { language } >
126- { children }
127- </ PurePre > ,
128- ) ;
129-
130- useLayoutEffect ( ( ) => {
131- safe ( )
132- . map ( ( ) =>
133- Highlight (
134- code ,
135- language ,
136- theme == "dark" ? "dark-plus" : "github-light" ,
137- ) ,
138- )
139- . ifOk ( setComponent )
140- . watch ( ( ) => setLoading ( false ) ) ;
141- } , [ theme , language , code ] ) ;
96+ const component = Highlight ( code , language ) ;
14297
14398 // For other code blocks, render as before
14499 return (
145100 < div
146101 className = { cn (
147- loading && "animate-pulse" ,
148102 "text-sm flex bg-secondary/40 shadow border flex-col rounded relative my-4 overflow-hidden" ,
149103 ) }
150104 >
0 commit comments