forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.tsx
More file actions
217 lines (208 loc) · 6.47 KB
/
Copy pathpage.tsx
File metadata and controls
217 lines (208 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import type { Metadata } from "next";
import type React from "react";
import Link from "next/link";
import { notFound } from "next/navigation";
import { MDXRemote } from "next-mdx-remote/rsc";
import matter from "gray-matter";
import { LinkIcon } from "lucide-react";
import remarkGfm from "remark-gfm";
import {
rehypeCode,
rehypeCodeDefaultOptions,
} from "fumadocs-core/mdx-plugins";
import { PropertyReference } from "@/components/property-reference";
import { MdxCodeBlock } from "@/components/mdx-code-block";
import { transformerMeta } from "@/lib/rehype-code-meta";
import {
Callout,
Cards,
Card,
Accordions,
Accordion,
} from "@/components/mdx-components";
import { OpsPlatformCTA } from "@/components/react/ops-platform-cta";
import {
DocsPage,
DocsBody,
DocsTitle,
DocsDescription,
} from "fumadocs-ui/page";
import { ShellDocsLayout } from "@/components/shell-docs-layout";
import { ReferenceVersionSelector } from "@/components/reference-version-selector";
import {
REFERENCE_VERSIONS,
buildReferencePageTree,
referenceHref,
referenceStaticParams,
referenceVersionHref,
resolveReferencePage,
} from "@/lib/reference-items";
import { stripLeadingImports } from "@/lib/docs-render";
import { buildDocMetadata } from "@/lib/seo-metadata";
// Self-canonical for /reference/<slug>. Reference pages are not
// per-framework, but we still emit a canonical so the production URL
// is unambiguous and any future host aliases can't fragment indexing.
// Title/description come from the page's MDX frontmatter so each API
// reference page emits its own social card and SEO description rather
// than inheriting the layout's generic site-wide values.
export async function generateMetadata({
params,
}: {
params: Promise<{ slug: string[] }>;
}): Promise<Metadata> {
const { slug } = await params;
const resolved = resolveReferencePage(slug);
const raw = resolved?.raw ?? null;
let title: string | undefined;
let description: string | undefined;
if (raw !== null) {
try {
const { data } = matter(raw);
if (typeof data.title === "string" && data.title.length > 0) {
title = data.title;
}
if (typeof data.description === "string" && data.description.length > 0) {
description = data.description;
}
} catch {
// Malformed frontmatter — fall back to slug-derived title.
}
}
return buildDocMetadata({
title: title ?? slug[slug.length - 1],
description,
canonicalPath: resolved
? referenceHref(resolved.version, resolved.pageSlug)
: `/reference/${slug.join("/")}`,
});
}
// next-mdx-remote components map
const mdxComponents = {
PropertyReference,
// Render fenced code blocks through the same Shiki + Fumadocs CodeBlock
// chrome the main docs use (syntax highlighting + copy button), paired with
// the rehypeCode plugin wired into the MDXRemote options below.
pre: MdxCodeBlock,
Callout,
Cards,
Card,
Accordions,
Accordion,
OpsPlatformCTA,
LinkIcon,
Frame: ({ children }: { children: React.ReactNode }) => (
<div className="shell-docs-radius-surface my-6 border border-[var(--border)] bg-[var(--bg-surface)] p-4 shadow-[var(--shadow-control)]">
{children}
</div>
),
// Strip unknown imports — MDX import statements become no-ops in next-mdx-remote
};
export function generateStaticParams() {
return referenceStaticParams();
}
export default async function ReferenceSlugPage({
params,
}: {
params: Promise<{ slug: string[] }>;
}) {
const { slug } = await params;
const resolved = resolveReferencePage(slug);
if (resolved === null) {
notFound();
}
const { version, pageSlug, contentSlug, raw } = resolved;
let content = "";
let data: Record<string, unknown> = {};
try {
const parsed = matter(raw);
content = parsed.content;
data = parsed.data;
} catch (err) {
console.error(
`[reference] Failed to parse frontmatter in ${contentSlug}.mdx:`,
err,
);
notFound();
}
const cleanedContent = stripLeadingImports(content);
const title =
typeof data.title === "string" && data.title.length > 0
? data.title
: slug[slug.length - 1];
const description =
typeof data.description === "string" ? data.description : undefined;
const pageTree = buildReferencePageTree(version);
const versionOptions = REFERENCE_VERSIONS.map((referenceVersion) => ({
version: referenceVersion,
href: referenceVersionHref(referenceVersion, pageSlug),
}));
return (
<ShellDocsLayout
tree={pageTree}
banner={
<ReferenceVersionSelector
activeVersion={version}
options={versionOptions}
/>
}
>
<DocsPage
toc={[]}
tableOfContent={{ enabled: false }}
tableOfContentPopover={{ enabled: false }}
breadcrumb={{ enabled: false }}
footer={{ enabled: false }}
>
<div className="px-6 py-10 max-w-3xl mx-auto">
<div className="mb-8">
<div className="text-xs text-[var(--text-muted)] mb-2">
<Link
href="/reference"
className="hover:text-[var(--text-secondary)]"
>
Reference
</Link>
{" / "}
<span>{version}</span>
{pageSlug && (
<>
{" / "}
<span className="capitalize">{pageSlug.split("/")[0]}</span>
</>
)}
</div>
<DocsTitle className="text-2xl font-bold">{title}</DocsTitle>
{description && (
<DocsDescription className="text-sm mt-1">
{description}
</DocsDescription>
)}
</div>
<DocsBody className="reference-content prose-sm">
<MDXRemote
source={cleanedContent}
components={mdxComponents}
options={{
mdxOptions: {
remarkPlugins: [remarkGfm],
rehypePlugins: [
[
rehypeCode,
{
fallbackLanguage: "plaintext",
transformers: [
...(rehypeCodeDefaultOptions.transformers ?? []),
transformerMeta(),
],
},
],
],
},
}}
/>
</DocsBody>
</div>
</DocsPage>
</ShellDocsLayout>
);
}