"use client"; import type { BundledLanguage } from "shiki/bundle/web"; import { codeToHast } from "shiki/bundle/web"; import { cn } from "@/lib/utils"; import { ComponentProps, type ReactNode, useEffect, useState } from "react"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; import { toJsxRuntime } from "hast-util-to-jsx-runtime"; async function highlight(code: string, lang: BundledLanguage) { const hast = await codeToHast(code, { lang, themes: { light: "github-light", dark: "github-dark", }, defaultColor: false, }); return toJsxRuntime(hast, { Fragment, jsx, jsxs, components: { pre: ({ className, style, ...props }: ComponentProps<"pre">) => (
), code: (props: ComponentProps<"code">) => (
),
},
}) as ReactNode;
}
interface CodeBlockProps {
code: string;
lang?: string;
className?: string;
}
export function CodeBlock({ code, lang = "json", className }: CodeBlockProps) {
const [nodes, setNodes] = useState
{code}
)}