From 1370ba0920f4ff562482ed90e07b674931a6a5e3 Mon Sep 17 00:00:00 2001 From: Matt Schweitz Date: Mon, 29 Jun 2026 21:45:52 -0400 Subject: [PATCH] Add component for embedding rendered diffs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit wraps a ```diff fence (a real unified patch) and emits a data-slot="code-diff" marker around it. The component does no diff rendering itself — it only marks the region so the host (pin) can replace it with a full, syntax-highlighted diff view server-side, keeping the heavy lifting (and Shiki/chroma) out of the bundle and the output static. Distinct from the existing prose comparison. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/components/index.tsx | 1 + src/components/manifest.ts | 12 ++++++++++ src/components/ui/code-diff.tsx | 40 +++++++++++++++++++++++++++++++++ test/compile.test.ts | 11 +++++++++ test/manifest.test.ts | 10 +++++++-- 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/components/ui/code-diff.tsx diff --git a/src/components/index.tsx b/src/components/index.tsx index 1d77200..7595742 100644 --- a/src/components/index.tsx +++ b/src/components/index.tsx @@ -53,6 +53,7 @@ export { Approval } from "./ui/approval.js"; export { Timeline, Event } from "./ui/timeline.js"; export { Tag, Status } from "./ui/tag.js"; export { Diff } from "./ui/diff.js"; +export { CodeDiff } from "./ui/code-diff.js"; export { Section } from "./ui/section.js"; export { Quote } from "./ui/quote.js"; export { Code } from "./ui/code.js"; diff --git a/src/components/manifest.ts b/src/components/manifest.ts index 73a8f74..70d1cc5 100644 --- a/src/components/manifest.ts +++ b/src/components/manifest.ts @@ -94,6 +94,18 @@ export const componentManifest = [ "\n func handle(w http.ResponseWriter, r *http.Request) { ... }\n" ] }, + { + "kind": "mdx-component", + "name": "CodeDiff", + "summary": "Embeds a real code diff inside a document. Wrap a ```diff fenced code block containing a unified patch (e.g. the output of `gh pr diff`), and the host renders it as a full, syntax-highlighted diff view in place of the plain fence.", + "category": "data", + "spec": { + "props": [] + }, + "examples": [ + "\n\n```diff\ndiff --git a/server.go b/server.go\n--- a/server.go\n+++ b/server.go" + ] + }, { "kind": "mdx-component", "name": "Compare", diff --git a/src/components/ui/code-diff.tsx b/src/components/ui/code-diff.tsx new file mode 100644 index 0000000..e676049 --- /dev/null +++ b/src/components/ui/code-diff.tsx @@ -0,0 +1,40 @@ +import * as React from "react"; + +/** + * Embeds a real code diff inside a document. Wrap a ```diff fenced code + * block containing a unified patch (e.g. the output of `gh pr diff`), and + * the host renders it as a full, syntax-highlighted diff view in place of + * the plain fence. + * + * This component only emits a marker (`data-slot="code-diff"`) around the + * fence — the host (pin) does the diff rendering server-side, so the heavy + * lifting stays out of the bundle and the output is static. A plain ```diff + * fence without this wrapper is left as ordinary highlighted text. + * + * @category data + * @example + * + * + * ```diff + * diff --git a/server.go b/server.go + * --- a/server.go + * +++ b/server.go + * @@ -1,3 +1,4 @@ + * func main() { + * - serve() + * + serveTLS() + * + log.Println("listening") + * } + * ``` + * + * + */ +function CodeDiff({ children, ...props }: React.ComponentProps<"div">) { + return ( +
+ {children} +
+ ); +} + +export { CodeDiff }; diff --git a/test/compile.test.ts b/test/compile.test.ts index eff19ca..95afe7a 100644 --- a/test/compile.test.ts +++ b/test/compile.test.ts @@ -17,6 +17,17 @@ describe("compileMDX", () => { expect(html).toContain("Heads up"); }); + it("marks a CodeDiff for host-side diff rendering", () => { + const { html } = compileMDX( + "\n\n```diff\ndiff --git a/x b/x\n--- a/x\n+++ b/x\n@@ -1 +1 @@\n-old\n+new\n```\n\n", + ); + // Emits the marker the host keys off, wrapping the language-diff fence + // with the patch preserved for the host to render. + expect(html).toContain(`data-slot="code-diff"`); + expect(html).toContain(`class="language-diff"`); + expect(html).toContain("diff --git a/x b/x"); + }); + it("renders nested components", () => { const { html } = compileMDX( ``, diff --git a/test/manifest.test.ts b/test/manifest.test.ts index a46088d..c1e369f 100644 --- a/test/manifest.test.ts +++ b/test/manifest.test.ts @@ -2,8 +2,14 @@ import { describe, it, expect } from "vitest"; import { componentManifest } from "../src/components/manifest.js"; describe("componentManifest", () => { - it("contains all 29 user-facing components", () => { - expect(componentManifest.length).toBe(29); + it("contains all 30 user-facing components", () => { + expect(componentManifest.length).toBe(30); + }); + + it("includes CodeDiff in the data category", () => { + const c = componentManifest.find((e) => e.name === "CodeDiff"); + expect(c).toBeDefined(); + expect(c!.category).toBe("data"); }); it("includes Subagent in the agent category", () => {