Skip to content

Commit cea9384

Browse files
committed
feat: llms.mdx routes
1 parent 644dfd8 commit cea9384

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { source } from "@/lib/source/docs";
2+
import { type NextRequest, NextResponse } from "next/server";
3+
import { getLLMText } from "@/lib/get-llm-text";
4+
import { notFound } from "next/navigation";
5+
6+
export const revalidate = false;
7+
8+
export async function GET(
9+
_req: NextRequest,
10+
{ params }: { params: Promise<{ slug: string[] }> },
11+
) {
12+
const slug = (await params).slug;
13+
slug.shift();
14+
const page = source.getPage(slug);
15+
if (!page) {
16+
notFound();
17+
}
18+
19+
return new NextResponse(await getLLMText(page));
20+
}
21+
22+
export function generateStaticParams() {
23+
return source.generateParams();
24+
}

docs/lib/get-llm-text.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { remarkInstall } from "fumadocs-docgen";
44
import remarkMdx from "remark-mdx";
55
import { remarkAutoTypeTable } from "fumadocs-typescript";
66
import { remarkInclude } from "fumadocs-mdx/config";
7-
import { type Page } from "./source/docs";
87

98
const processor = remark()
109
.use(remarkMdx)
@@ -26,6 +25,20 @@ export type LLMData = {
2625
file: {
2726
path: string;
2827
};
28+
};
29+
30+
// Function to rewrite links in markdown content
31+
function rewriteLinks(content: string): string {
32+
return (
33+
content
34+
// Rewrite absolute docs links: /docs/something -> /llms.mdx/docs/something
35+
.replace(/\[([^\]]+)\]\(\/docs\/([^)]+)\)/g, "[$1](/llms.mdx/docs/$2)")
36+
// Rewrite relative docs links: docs/something -> /llms.mdx/docs/something
37+
.replace(/\[([^\]]+)\]\(docs\/([^)]+)\)/g, "[$1](/llms.mdx/docs/$2)")
38+
// Handle links without text: /docs/something -> /llms.mdx/docs/something
39+
.replace(/\(\/docs\/([^)]+)\)/g, "(/llms.mdx/docs/$1)")
40+
.replace(/\(docs\/([^)]+)\)/g, "(/llms.mdx/docs/$1)")
41+
);
2942
}
3043

3144
export async function getLLMText(page: LLMData) {
@@ -34,11 +47,14 @@ export async function getLLMText(page: LLMData) {
3447
value: page.data.content,
3548
});
3649

50+
// Rewrite links in the processed content
51+
const contentWithRewrittenLinks = rewriteLinks(processed.value.toString());
52+
3753
return `# ${page.data.title}
3854
URL: ${page.url}
39-
Source: https://raw.githubusercontent.com/fuma-nama/fumadocs/refs/heads/main/apps/docs/content/docs/${page.file.path}
55+
Source: https://raw.githubusercontent.com/TypeCellOS/BlockNote/refs/heads/main/docs/content/docs/${page.file.path}
4056
4157
${page.data.description}
4258
43-
${processed.value}`;
59+
${contentWithRewrittenLinks}`;
4460
}

docs/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@
9696
"remark-mdx": "^3.1.0",
9797
"require-in-the-middle": "^7.5.2",
9898
"shiki": "^3.7.0",
99+
"ts-morph": "26.0.0",
99100
"twoslash": "^0.3.2",
100101
"y-partykit": "^0.0.25",
101102
"yjs": "^13.6.27",
102103
"zod": "^3.25.67",
103104
"zustand": "^5.0.3"
104105
},
105106
"devDependencies": {
106-
"next-validate-link": "^1.5.2",
107107
"@blocknote/ariakit": "workspace:^",
108108
"@blocknote/code-block": "workspace:^",
109109
"@blocknote/core": "workspace:^",
@@ -129,6 +129,7 @@
129129
"@types/react-dom": "^19.1.6",
130130
"@y-sweet/react": "^0.6.4",
131131
"next-themes": "0.4.6",
132+
"next-validate-link": "^1.5.2",
132133
"postcss": "^8.5.6",
133134
"react-email": "^4.0.16",
134135
"react-icons": "^5.5.0",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)