diff --git a/app/docs/[[...slug]]/page.tsx b/app/docs/[[...slug]]/page.tsx index c529d34a..67cae38c 100644 --- a/app/docs/[[...slug]]/page.tsx +++ b/app/docs/[[...slug]]/page.tsx @@ -250,9 +250,7 @@ export default async function Page(props: {
- }> - - +
- {children} + }> + {children} +
); diff --git a/mdx-components.test.tsx b/mdx-components.test.tsx new file mode 100644 index 00000000..32bee299 --- /dev/null +++ b/mdx-components.test.tsx @@ -0,0 +1,46 @@ +import type { CSSProperties } from "react"; +import { renderToStaticMarkup } from "react-dom/server"; +import { describe, expect, it, vi } from "vitest"; + +vi.mock("@/components/code-snippet", () => ({ + default: () =>
, +})); + +import { mdxComponents } from "@/mdx-components"; + +const installCommand = "pnpm dlx shadcn@latest add @8bitcn/button"; +type ShikiStyle = CSSProperties & { "--shiki-light": string }; + +const commandTokenStyle: ShikiStyle = { "--shiki-light": "#6f42c1" }; +const argumentTokenStyle: ShikiStyle = { "--shiki-light": "#032f62" }; + +describe("mdxComponents.code", () => { + it("includes pre-highlighted fenced code in the server-rendered markup", () => { + const Code = mdxComponents.code; + const markup = renderToStaticMarkup( + + + pnpm + + {" dlx shadcn@latest add @8bitcn/button"} + + + + ); + const renderedText = new DOMParser().parseFromString(markup, "text/html") + .body.textContent; + + expect(renderedText).toContain(installCommand); + expect(markup).toContain('class="line"'); + expect(markup).toContain("--shiki-light:#6f42c1"); + expect(markup).not.toContain("client-code-snippet"); + }); + + it("keeps inline code styling", () => { + const Code = mdxComponents.code; + const markup = renderToStaticMarkup(components.json); + + expect(markup).toContain("components.json"); + expect(markup).toContain("bg-muted"); + }); +}); diff --git a/mdx-components.tsx b/mdx-components.tsx index 797eb591..dc7f27b1 100644 --- a/mdx-components.tsx +++ b/mdx-components.tsx @@ -1,13 +1,9 @@ import { icons } from "@tabler/icons-react"; +import { CodeBlock, Pre } from "fumadocs-ui/components/codeblock"; import Image from "next/image"; import Link from "next/link"; -import { - type ComponentProps, - type HTMLAttributes, - isValidElement, - type ReactNode, -} from "react"; +import type { ComponentProps, HTMLAttributes } from "react"; import { Kbd } from "@/components/ui/8bit/kbd"; import { Accordion, @@ -21,8 +17,6 @@ import { Button } from "@/components/ui/button"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { cn } from "@/lib/utils"; -import CodeSnippet from "./components/code-snippet"; - export const mdxComponents = { h1: ({ className, ...props }: ComponentProps<"h1">) => (

), pre: ({ className, children, ...props }: ComponentProps<"pre">) => ( -
-      {children}
-    
+
{children}
+ ), figure: ({ className, ...props }: ComponentProps<"figure">) => (
@@ -191,24 +183,7 @@ export const mdxComponents = { ); }, - code: ({ - className, - __raw__, - __src__, - __npm__, - __yarn__, - __pnpm__, - __bun__, - children, - ...props - }: ComponentProps<"code"> & { - __raw__?: string; - __src__?: string; - __npm__?: string; - __yarn__?: string; - __pnpm__?: string; - __bun__?: string; - }) => { + code: ({ className, children, ...props }: ComponentProps<"code">) => { // Inline Code. if (typeof children === "string") { return ( @@ -224,29 +199,11 @@ export const mdxComponents = { ); } - // Extract text content from React children if __raw__ is not available - const getTextContent = (node: ReactNode): string => { - if (typeof node === "string") { - return node; - } - if (typeof node === "number") { - return String(node); - } - if (Array.isArray(node)) { - return node.map(getTextContent).join(""); - } - if (isValidElement(node)) { - const nodeProps = node.props as { children?: ReactNode }; - if (nodeProps.children) { - return getTextContent(nodeProps.children); - } - } - return ""; - }; - - const codeContent = __raw__ || getTextContent(children); - - return {codeContent}; + return ( + + {children} + + ); }, Step: ({ className, ...props }: ComponentProps<"h3">) => (