Skip to content

Commit aa2577b

Browse files
authored
feat(llms): add LLM text processing and routing for MDX and TXT files (#470)
1 parent 726901c commit aa2577b

File tree

4 files changed

+57
-0
lines changed

4 files changed

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

apps/docs/app/llms.txt/route.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { getLLMText } from '@/lib/llms'
2+
import { source } from '@/lib/source'
3+
4+
// cached forever
5+
export const revalidate = false
6+
7+
export async function GET() {
8+
const scan = source.getPages().map(getLLMText)
9+
const scanned = await Promise.all(scan)
10+
11+
return new Response(scanned.join('\n\n'))
12+
}

apps/docs/lib/llms.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { InferPageType } from 'fumadocs-core/source'
2+
import { remarkInclude } from 'fumadocs-mdx/config'
3+
import { remark } from 'remark'
4+
import remarkGfm from 'remark-gfm'
5+
import remarkMdx from 'remark-mdx'
6+
import type { source } from '@/lib/source'
7+
8+
const processor = remark().use(remarkMdx).use(remarkInclude).use(remarkGfm)
9+
10+
export async function getLLMText(page: InferPageType<typeof source>) {
11+
const processed = await processor.process({
12+
path: page.data._file.absolutePath,
13+
value: page.data.content,
14+
})
15+
16+
return `# ${page.data.title}
17+
URL: ${page.url}
18+
19+
${page.data.description}
20+
21+
${processed.value}`
22+
}

apps/docs/next.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ const config = {
1212
destination: '/introduction',
1313
permanent: true,
1414
},
15+
{
16+
source: '/docs/:path*.mdx',
17+
destination: '/llms.mdx/:path*',
18+
permanent: true,
19+
},
1520
]
1621
},
1722
}

0 commit comments

Comments
 (0)