stack/docs/lib/get-llm-text.ts
2025-07-09 18:27:41 -05:00

49 lines
1.5 KiB
TypeScript

import type { InferPageType } from 'fumadocs-core/source';
import { remarkInclude } from 'fumadocs-mdx/config';
import { remark } from 'remark';
import remarkGfm from 'remark-gfm';
import remarkMdx from 'remark-mdx';
import { apiSource, source } from './source';
const processor = remark()
.use(remarkMdx)
// needed for Fumadocs MDX
.use(remarkInclude)
.use(remarkGfm);
export async function getLLMText(page: InferPageType<typeof source> | InferPageType<typeof apiSource>) {
try {
// Remove the Fumadocs generated comment before processing
let content = page.data.content;
// Remove the specific Fumadocs comment that appears in generated API docs
content = content.replace(
/\{\s*\/\*\s*This file was generated by Fumadocs\. Do not edit this file directly\. Any changes should be made by running the generation command again\.\s*\*\/\s*\}/g,
''
);
const processed = await processor.process({
path: page.data._file.absolutePath,
value: content,
});
return `# ${page.data.title}
URL: ${page.url}
Source: ${page.data._file.absolutePath}
${page.data.description || ''}
${processed.value}`;
} catch (error) {
console.error('Error processing LLM text for page:', page.url, error);
// Return a basic fallback content instead of throwing
return `# ${page.data.title}
URL: ${page.url}
Source: ${page.data._file.absolutePath}
${page.data.description || ''}
Error: Could not process content for this page.`;
}
}