import fs from "node:fs/promises" import path from "node:path" import * as React from "react" import { highlightCode } from "@/lib/highlight-code" import { getRegistryItem } from "@/lib/registry" import { cn } from "@/lib/utils" import { CodeCollapsibleWrapper } from "@/components/code-collapsible-wrapper" import { CopyButton } from "@/components/copy-button" import { getIconForLanguageExtension } from "@/components/icons" import { type Style } from "@/registry/_legacy-styles" export async function ComponentSource({ name, src, title, language, collapsible = true, className, styleName = "new-york-v4", }: React.ComponentProps<"div"> & { name?: string src?: string title?: string language?: string collapsible?: boolean styleName?: Style["name"] }) { if (!name && !src) { return null } let code: string | undefined if (name) { const item = await getRegistryItem(name, styleName) code = item?.files?.[0]?.content } if (src) { const file = await fs.readFile(path.join(process.cwd(), src), "utf-8") code = file } if (!code) { return null } // Fix imports. // Replace @/registry/${style}/ with @/components/. code = code.replaceAll(`@/registry/${styleName}/`, "@/components/") // Replace export default with export. code = code.replaceAll("export default", "export") code = code.replaceAll("/* eslint-disable react/no-children-prop */\n", "") const lang = language ?? title?.split(".").pop() ?? "tsx" const highlightedCode = await highlightCode(code, lang) if (!collapsible) { return (
) } return ( ) } function ComponentCode({ code, highlightedCode, language, title, }: { code: string highlightedCode: string language: string title: string | undefined }) { return (
{title && (
{getIconForLanguageExtension(language)} {title}
)}
) }