Core: Support llms API in Loader API

This commit is contained in:
Fuma Nama
2026-03-06 01:36:54 +08:00
parent e96c0fb7cd
commit 9b5c2ddd63
9 changed files with 99 additions and 40 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"fumadocs-core": patch
---
Support `llms` API in Loader API
+3 -19
View File
@@ -1,24 +1,8 @@
import { source } from '@/lib/source';
import { getSection } from '@/lib/source/navigation';
import { llms } from 'fumadocs-core/source';
export const revalidate = false;
export async function GET() {
const scanned: string[] = [];
scanned.push('# Docs');
const map = new Map<string, string[]>();
for (const page of source.getPages()) {
const section = getSection(page.slugs[0]);
const list = map.get(section) ?? [];
list.push(`- [${page.data.title}](${page.url}): ${page.data.description}`);
map.set(section, list);
}
for (const [key, value] of map) {
scanned.push(`## ${key}`);
scanned.push(value.join('\n'));
}
return new Response(scanned.join('\n\n'));
export function GET() {
return new Response(llms(source).index());
}
+1 -1
View File
@@ -41,7 +41,7 @@
"fumadocs-typescript": "workspace:^",
"fumadocs-ui": "workspace:*",
"hast-util-to-jsx-runtime": "^2.3.6",
"katex": "^0.16.34",
"katex": "0.16.33",
"lucide-react": "^0.577.0",
"mermaid": "^11.12.3",
"motion": "^12.35.0",
+1 -1
View File
@@ -27,7 +27,7 @@
"@types/node": "^25.3.3",
"@types/react": "^19.2.14",
"@types/react-dom": "^19.2.3",
"tailwindcss": "^4.2.1",
"tailwindcss": "^4 .2.1",
"typescript": "^5.9.3",
"vite": "^7.3.1",
"vite-tsconfig-paths": "^6.1.1"
+12 -1
View File
@@ -1,12 +1,23 @@
import { defineConfig, defineDocs } from 'fumadocs-mdx/config';
import { metaSchema, pageSchema } from 'fumadocs-core/source/schema';
// You can customise Zod schemas for frontmatter and `meta.json` here
// see https://fumadocs.dev/docs/mdx/collections
export const docs = defineDocs({
dir: 'content/docs',
docs: {
schema: pageSchema,
postprocess: {
includeProcessedMarkdown: true,
},
},
meta: {
schema: metaSchema,
},
});
export default defineConfig();
export default defineConfig({
mdxOptions: {
// MDX options
},
});
@@ -2,9 +2,3 @@ import { createFromSource } from 'fumadocs-core/search/server';
import { source } from '@/lib/source';
export const { GET } = createFromSource(source);
export async function getConfig() {
return {
render: 'static' as const,
} as const;
}
+65
View File
@@ -539,6 +539,71 @@ function buildPlugins(plugins: LoaderPluginOption[], sort = true): LoaderPlugin[
return flatten;
}
export interface LLMsConfig {
TAB?: string;
renderName?: (item: PageTree.Node | PageTree.Root) => string;
renderDescription?: (item: PageTree.Item | PageTree.Folder) => string;
}
export function llms<C extends LoaderConfig>(loader: LoaderOutput<C>, config: LLMsConfig = {}) {
const {
TAB = ' ',
renderName = (node) => (node.name ? String(node.name) : ''),
renderDescription = (node) => (node.description ? String(node.description) : ''),
} = config;
function index(lang?: string): string {
if (loader._i18n && lang === undefined) {
const { languages } = loader._i18n;
return languages.map((lang) => index(lang)).join('\n\n');
}
const pageTree = loader.getPageTree(lang);
const out: string[] = [];
out.push(`# ${renderName(pageTree)}`);
out.push('');
function item(name: string, description: string, indent: number) {
const prefix = TAB.repeat(indent);
description = description.trim();
if (description.length > 0) return `${prefix}- ${name}: ${description}`;
return `${prefix}- ${name}`;
}
function onNode(node: PageTree.Node, indent: number) {
switch (node.type) {
case 'page': {
out.push(item(`[${renderName(node)}](${node.url})`, renderDescription(node), indent));
break;
}
case 'folder': {
out.push(item(renderName(node), renderDescription(node), indent));
if (node.index) onNode(node.index, indent + 1);
for (const child of node.children) onNode(child, indent + 1);
break;
}
case 'separator': {
if (node.name) out.push(item(`**${renderName(node)}**`, '', indent));
out.push('');
break;
}
}
}
for (const child of pageTree.children) onNode(child, 0);
return out.join('\n');
}
return {
/**
* generate `llms.txt` content in Markdown format.
*/
index,
};
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- infer types
export type InferPageType<Utils extends LoaderOutput<any>> =
Utils extends LoaderOutput<infer Config> ? Page<Config['source']['pageData']> : never;
+1 -1
View File
@@ -61,7 +61,7 @@
"hast-util-to-jsx-runtime": "^2.3.6",
"hono": "^4.12.5",
"ignore": "^7.0.5",
"katex": "^0.16.34",
"katex": "0.16.33",
"lucide-react": "^0.577.0",
"react": "^19.2.4",
"react-dom": "^19.2.4",
+11 -11
View File
@@ -138,8 +138,8 @@ importers:
specifier: ^2.3.6
version: 2.3.6
katex:
specifier: ^0.16.34
version: 0.16.34
specifier: 0.16.33
version: 0.16.33
lucide-react:
specifier: ^0.577.0
version: 0.577.0(react@19.2.4)
@@ -1568,7 +1568,7 @@ importers:
specifier: ^19.2.3
version: 19.2.3(@types/react@19.2.14)
tailwindcss:
specifier: ^4.2.1
specifier: ^4 .2.1
version: 4.2.1
typescript:
specifier: ^5.9.3
@@ -2536,8 +2536,8 @@ importers:
specifier: ^7.0.5
version: 7.0.5
katex:
specifier: ^0.16.34
version: 0.16.34
specifier: 0.16.33
version: 0.16.33
lucide-react:
specifier: ^0.577.0
version: 0.577.0(react@19.2.4)
@@ -9521,8 +9521,8 @@ packages:
resolution: {integrity: sha512-4+5mNNf4vZDSwPhKprKwz3330iisPrb08JyMgbsdFrimBCKNHecua/WBwvVg3n7vwx0C1ARjfhwIpbrbd9n5wg==}
engines: {node: '>=12'}
katex@0.16.34:
resolution: {integrity: sha512-z1RSvOYzV7dykg9Jwl3EfGhIfo9mrotTx1uuEXWpXRCBbX9eKVywxa60mvFYipjOSus5hzj+F7Kh2fmnjFwA5w==}
katex@0.16.33:
resolution: {integrity: sha512-q3N5u+1sY9Bu7T4nlXoiRBXWfwSefNGoKeOwekV+gw0cAXQlz2Ww6BLcmBxVDeXBMUDQv6fK5bcNaJLxob3ZQA==}
hasBin: true
keyv@4.5.4:
@@ -19595,7 +19595,7 @@ snapshots:
dependencies:
lodash-es: 4.17.23
katex@0.16.34:
katex@0.16.33:
dependencies:
commander: 8.3.0
@@ -20019,7 +20019,7 @@ snapshots:
dagre-d3-es: 7.0.13
dayjs: 1.11.19
dompurify: 3.3.1
katex: 0.16.34
katex: 0.16.33
khroma: 2.1.0
lodash-es: 4.17.23
marked: 16.4.2
@@ -20132,7 +20132,7 @@ snapshots:
dependencies:
'@types/katex': 0.16.7
devlop: 1.1.0
katex: 0.16.34
katex: 0.16.33
micromark-factory-space: 2.0.1
micromark-util-character: 2.1.1
micromark-util-symbol: 2.0.1
@@ -21447,7 +21447,7 @@ snapshots:
'@types/katex': 0.16.7
hast-util-from-html-isomorphic: 2.0.0
hast-util-to-text: 4.0.2
katex: 0.16.34
katex: 0.16.33
unist-util-visit-parents: 6.0.2
vfile: 6.0.3