Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
12 changes: 12 additions & 0 deletions src/components/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ export const componentManifest = [
"<Code language=\"go\">\n func handle(w http.ResponseWriter, r *http.Request) { ... }\n</Code>"
]
},
{
"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": [
"<CodeDiff>\n\n```diff\ndiff --git a/server.go b/server.go\n--- a/server.go\n+++ b/server.go"
]
},
{
"kind": "mdx-component",
"name": "Compare",
Expand Down
40 changes: 40 additions & 0 deletions src/components/ui/code-diff.tsx
Original file line number Diff line number Diff line change
@@ -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
* <CodeDiff>
*
* ```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")
* }
* ```
*
* </CodeDiff>
*/
function CodeDiff({ children, ...props }: React.ComponentProps<"div">) {
return (
<div data-slot="code-diff" {...props}>
{children}
</div>
);
}

export { CodeDiff };
11 changes: 11 additions & 0 deletions test/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ describe("compileMDX", () => {
expect(html).toContain("Heads up");
});

it("marks a CodeDiff for host-side diff rendering", () => {
const { html } = compileMDX(
"<CodeDiff>\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</CodeDiff>",
);
// 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(
`<KPIGroup><KPI value="42" label="users" /><KPI value="3" label="errors" tone="err" /></KPIGroup>`,
Expand Down
10 changes: 8 additions & 2 deletions test/manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Loading