Skip to content

Commit 9c72aa3

Browse files
committed
feat: reorganize imports and enhance code structure across multiple files
1 parent aa4a21b commit 9c72aa3

8 files changed

Lines changed: 129 additions & 102 deletions

File tree

docs/source.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { defineConfig, defineDocs } from "fumadocs-mdx/config";
21
import { metaSchema, pageSchema } from "fumadocs-core/source/schema";
2+
import { defineConfig, defineDocs } from "fumadocs-mdx/config";
33

44
// You can customise Zod schemas for frontmatter and `meta.json` here
55
// see https://fumadocs.dev/docs/mdx/collections

docs/src/app/(home)/page.tsx

Lines changed: 111 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import Link from "next/link";
1+
import { buttonVariants } from "fumadocs-ui/components/ui/button";
2+
import type { LucideProps } from "lucide-react";
23
import {
34
Activity,
45
ArrowRight,
@@ -15,33 +16,123 @@ import {
1516
Terminal,
1617
Zap,
1718
} from "lucide-react";
18-
import { cn } from "@/lib/cn";
19-
import { buttonVariants } from "fumadocs-ui/components/ui/button";
20-
import type { LucideProps } from "lucide-react";
19+
import Link from "next/link";
2120
import type { ComponentType } from "react";
21+
import { cn } from "@/lib/cn";
2222

2323
/* ─── Syntax highlight helpers ─── */
2424

25-
function Kw({ children }: { children: React.ReactNode }) {
26-
return <span className="font-semibold text-fd-primary">{children}</span>;
27-
}
25+
const tokenStyles: Record<string, string> = {
26+
keyword: "font-semibold text-fd-primary",
27+
string: "text-emerald-600 dark:text-emerald-400",
28+
comment: "italic text-fd-muted-foreground/60",
29+
function: "text-violet-600 dark:text-violet-400",
30+
};
2831

29-
function Str({ children }: { children: React.ReactNode }) {
30-
return (
31-
<span className="text-emerald-600 dark:text-emerald-400">{children}</span>
32-
);
33-
}
32+
const goRules: [RegExp, string][] = [
33+
[/(\/\/.*)$/gm, "comment"],
34+
[/("(?:[^"\\]|\\.)*")/g, "string"],
35+
[/\b(package|import|func|var|const|type|return|if|else|for|range|defer|go|chan|map|struct|interface)\b/g, "keyword"],
36+
[/\b([A-Z]\w*)\s*[({]/g, "function"],
37+
[/\.([A-Z]\w*)\s*\(/g, "function"],
38+
];
3439

35-
function Cm({ children }: { children: React.ReactNode }) {
36-
return <span className="italic text-fd-muted-foreground/60">{children}</span>;
37-
}
40+
function highlightGo(code: string): React.ReactNode[] {
41+
const lines = code.split("\n");
3842

39-
function Fn({ children }: { children: React.ReactNode }) {
40-
return (
41-
<span className="text-violet-600 dark:text-violet-400">{children}</span>
42-
);
43+
return lines.map((line, i) => {
44+
const segments: { start: number; end: number; style: string; group: number }[] = [];
45+
46+
for (const [re, style] of goRules) {
47+
re.lastIndex = 0;
48+
let m: RegExpExecArray | null = null;
49+
50+
while ((m = re.exec(line)) !== null) {
51+
const group = style === "function" ? 1 : 1;
52+
const text = m[group] ?? m[0];
53+
const start = m.index + (m[0].indexOf(text));
54+
55+
segments.push({ start, end: start + text.length, style, group });
56+
}
57+
}
58+
59+
segments.sort((a, b) => a.start - b.start);
60+
61+
// Remove overlaps — earlier rules win
62+
const filtered: typeof segments = [];
63+
64+
for (const seg of segments) {
65+
if (filtered.every((f) => seg.start >= f.end || seg.end <= f.start)) {
66+
filtered.push(seg);
67+
}
68+
}
69+
70+
filtered.sort((a, b) => a.start - b.start);
71+
72+
const parts: React.ReactNode[] = [];
73+
let cursor = 0;
74+
75+
for (const seg of filtered) {
76+
if (seg.start > cursor) {
77+
parts.push(line.slice(cursor, seg.start));
78+
}
79+
80+
parts.push(
81+
<span key={`${i}-${seg.start}`} className={tokenStyles[seg.style]}>
82+
{line.slice(seg.start, seg.end)}
83+
</span>,
84+
);
85+
cursor = seg.end;
86+
}
87+
88+
if (cursor < line.length) {
89+
parts.push(line.slice(cursor));
90+
}
91+
92+
return (
93+
<span key={i}>
94+
{parts}
95+
{i < lines.length - 1 ? "\n" : null}
96+
</span>
97+
);
98+
});
4399
}
44100

101+
const codeExample = `package main
102+
103+
import (
104+
"log"
105+
106+
"github.com/xraph/forge"
107+
108+
"github.com/xraph/ctrlplane/app"
109+
"github.com/xraph/ctrlplane/extension"
110+
"github.com/xraph/ctrlplane/provider/docker"
111+
"github.com/xraph/ctrlplane/store/memory"
112+
)
113+
114+
func main() {
115+
// Create a Forge app with OpenAPI docs
116+
forgeApp := forge.New(
117+
forge.WithAppName("ctrlplane"),
118+
forge.WithAppVersion("0.1.0"),
119+
)
120+
121+
// Register Ctrl Plane as an extension
122+
cpExt := extension.New(
123+
extension.WithStore(
124+
app.WithStore(memory.New()),
125+
),
126+
extension.WithProvider(
127+
"docker",
128+
docker.New(docker.Config{}),
129+
),
130+
)
131+
132+
forgeApp.RegisterExtension(cpExt)
133+
log.Fatal(forgeApp.Run())
134+
}`;
135+
45136
/* ─── Data ─── */
46137

47138
const features: {
@@ -278,75 +369,7 @@ export default function HomePage() {
278369
</div>
279370
<div className="overflow-x-auto p-5">
280371
<pre className="font-mono text-[13px] leading-relaxed text-fd-foreground/90">
281-
<code>
282-
<Kw>package</Kw> main{"\n"}
283-
{"\n"}
284-
<Kw>import</Kw> ({"\n"}
285-
{" "}
286-
<Str>&quot;log&quot;</Str>
287-
{"\n"}
288-
{"\n"}
289-
{" "}
290-
<Str>&quot;github.com/xraph/forge&quot;</Str>
291-
{"\n"}
292-
{"\n"}
293-
{" "}
294-
<Str>&quot;github.com/xraph/ctrlplane/app&quot;</Str>
295-
{"\n"}
296-
{" "}
297-
<Str>&quot;github.com/xraph/ctrlplane/extension&quot;</Str>
298-
{"\n"}
299-
{" "}
300-
<Str>
301-
&quot;github.com/xraph/ctrlplane/provider/docker&quot;
302-
</Str>
303-
{"\n"}
304-
{" "}
305-
<Str>&quot;github.com/xraph/ctrlplane/store/memory&quot;</Str>
306-
{"\n"}){"\n"}
307-
{"\n"}
308-
<Kw>func</Kw> <Fn>main</Fn>() {"{"}
309-
{"\n"}
310-
{" "}
311-
<Cm>// Create a Forge app with OpenAPI docs</Cm>
312-
{"\n"}
313-
{" "}forgeApp := forge.
314-
<Fn>New</Fn>({"\n"}
315-
{" "}forge.
316-
<Fn>WithAppName</Fn>(<Str>&quot;ctrlplane&quot;</Str>),{"\n"}
317-
{" "}forge.
318-
<Fn>WithAppVersion</Fn>(<Str>&quot;0.1.0&quot;</Str>),{"\n"}
319-
{" "}){"\n"}
320-
{"\n"}
321-
{" "}
322-
<Cm>// Register Ctrl Plane as an extension</Cm>
323-
{"\n"}
324-
{" "}cpExt := extension.
325-
<Fn>New</Fn>({"\n"}
326-
{" "}extension.
327-
<Fn>WithStore</Fn>({"\n"}
328-
{" "}app.
329-
<Fn>WithStore</Fn>(memory.
330-
<Fn>New</Fn>()),{"\n"}
331-
{" "}),{"\n"}
332-
{" "}extension.
333-
<Fn>WithProvider</Fn>({"\n"}
334-
{" "}
335-
<Str>&quot;docker&quot;</Str>,{"\n"}
336-
{" "}docker.
337-
<Fn>New</Fn>(docker.Config{"{}"}) ,{"\n"}
338-
{" "}),{"\n"}
339-
{" "}){"\n"}
340-
{"\n"}
341-
{" "}forgeApp.
342-
<Fn>RegisterExtension</Fn>(cpExt)
343-
{"\n"}
344-
{" "}log.
345-
<Fn>Fatal</Fn>(forgeApp.
346-
<Fn>Run</Fn>())
347-
{"\n"}
348-
{"}"}
349-
</code>
372+
<code>{highlightGo(codeExample)}</code>
350373
</pre>
351374
</div>
352375
</div>

docs/src/app/api/search/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { source } from "@/lib/source";
21
import { createFromSource } from "fumadocs-core/search/server";
2+
import { source } from "@/lib/source";
33

44
export const { GET } = createFromSource(source, {
55
// https://docs.orama.com/docs/orama-js/supported-languages

docs/src/app/docs/[[...slug]]/page.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { getPageImage, source } from "@/lib/source";
21
import {
32
DocsBody,
43
DocsDescription,
54
DocsPage,
65
DocsTitle,
76
} from "fumadocs-ui/layouts/docs/page";
8-
import { notFound } from "next/navigation";
9-
import { getMDXComponents } from "@/mdx-components";
10-
import type { Metadata } from "next";
117
import { createRelativeLink } from "fumadocs-ui/mdx";
8+
import type { Metadata } from "next";
9+
import { notFound } from "next/navigation";
1210
import { LLMCopyButton, ViewOptions } from "@/components/ai/page-actions";
1311
import { gitConfig } from "@/lib/layout.shared";
12+
import { getPageImage, source } from "@/lib/source";
13+
import { getMDXComponents } from "@/mdx-components";
1414

1515
export default async function Page(props: PageProps<"/docs/[[...slug]]">) {
1616
const params = await props.params;

docs/src/app/docs/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { source } from "@/lib/source";
21
import { DocsLayout } from "fumadocs-ui/layouts/docs";
32
import { baseOptions } from "@/lib/layout.shared";
3+
import { source } from "@/lib/source";
44

55
export default function Layout({ children }: LayoutProps<"/docs">) {
66
return (

docs/src/app/llms.mdx/docs/[[...slug]]/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { getLLMText, source } from "@/lib/source";
21
import { notFound } from "next/navigation";
2+
import { getLLMText, source } from "@/lib/source";
33

44
export const revalidate = false;
55

docs/src/app/og/docs/[...slug]/route.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { getPageImage, source } from "@/lib/source";
1+
import { generate as DefaultImage } from "fumadocs-ui/og";
22
import { notFound } from "next/navigation";
33
import { ImageResponse } from "next/og";
4-
import { generate as DefaultImage } from "fumadocs-ui/og";
4+
5+
import { getPageImage, source } from "@/lib/source";
56

67
export const revalidate = false;
78

docs/src/components/ai/page-actions.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
"use client";
2-
import { useMemo, useState } from "react";
3-
import { Check, ChevronDown, Copy, ExternalLinkIcon } from "lucide-react";
4-
import { cn } from "@/lib/cn";
5-
import { useCopyButton } from "fumadocs-ui/utils/use-copy-button";
2+
63
import { buttonVariants } from "fumadocs-ui/components/ui/button";
74
import {
85
Popover,
96
PopoverContent,
107
PopoverTrigger,
118
} from "fumadocs-ui/components/ui/popover";
9+
import { useCopyButton } from "fumadocs-ui/utils/use-copy-button";
10+
import { Check, ChevronDown, Copy, ExternalLinkIcon } from "lucide-react";
11+
import { useMemo, useState } from "react";
12+
13+
import { cn } from "@/lib/cn";
1214

1315
const cache = new Map<string, string>();
1416

@@ -46,6 +48,7 @@ export function LLMCopyButton({
4648
return (
4749
<button
4850
disabled={isLoading}
51+
type="button"
4952
className={cn(
5053
buttonVariants({
5154
color: "secondary",

0 commit comments

Comments
 (0)