-
Notifications
You must be signed in to change notification settings - Fork 5
Add /llm.txt endpoint for LLM consumption of MDX documentation #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import { fileURLToPath } from 'url'; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = path.dirname(__filename); | ||
|
|
||
| // Function to parse frontmatter from MDX content | ||
| function parseFrontmatter(content) { | ||
| const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---\n/); | ||
| if (!frontmatterMatch) return { data: {}, content }; | ||
|
|
||
| const frontmatter = frontmatterMatch[1]; | ||
| const data = {}; | ||
|
|
||
| frontmatter.split('\n').forEach(line => { | ||
| const [key, ...valueParts] = line.split(':'); | ||
| if (key && valueParts.length) { | ||
| const value = valueParts.join(':').trim(); | ||
| data[key.trim()] = value.replace(/^["']|["']$/g, ''); | ||
| } | ||
| }); | ||
|
|
||
| return { data, content: content.slice(frontmatterMatch[0].length) }; | ||
| } | ||
|
|
||
| // Function to recursively find all MDX files | ||
| function findMdxFiles(dir) { | ||
| const files = []; | ||
| const items = fs.readdirSync(dir, { withFileTypes: true }); | ||
|
|
||
| for (const item of items) { | ||
| const fullPath = path.join(dir, item.name); | ||
| if (item.isDirectory()) { | ||
| files.push(...findMdxFiles(fullPath)); | ||
| } else if (item.isFile() && item.name.endsWith('.mdx')) { | ||
| files.push(fullPath); | ||
| } | ||
| } | ||
|
|
||
| return files; | ||
| } | ||
|
|
||
| // Function to clean MDX content for LLM consumption | ||
| function cleanMdxContent(content) { | ||
| // Remove JSX components but keep their content | ||
| let cleaned = content | ||
| // Remove JSX opening/closing tags but keep content | ||
| .replace(/<(\w+)([^>]*?)>/g, '') | ||
| .replace(/<\/\w+>/g, '') | ||
| // Remove code block language specifiers | ||
| .replace(/```(\w+)/g, '```') | ||
| // Remove HTML comments | ||
| .replace(/<!--[\s\S]*?-->/g, '') | ||
| // Remove excessive whitespace | ||
| .replace(/\n\s*\n\s*\n/g, '\n\n') | ||
| // Remove leading/trailing whitespace | ||
| .trim(); | ||
|
|
||
| return cleaned; | ||
| } | ||
|
|
||
| // Main function to generate LLM text | ||
| async function generateLlmTxt() { | ||
| const docsDir = path.join(__dirname, '..', 'docs'); | ||
| const outputDir = path.join(__dirname, '..', 'dist'); | ||
|
|
||
| // Ensure output directory exists | ||
| if (!fs.existsSync(outputDir)) { | ||
| fs.mkdirSync(outputDir, { recursive: true }); | ||
| } | ||
|
|
||
| const mdxFiles = findMdxFiles(docsDir); | ||
| let aggregatedContent = ''; | ||
|
|
||
| // Sort files for consistent output | ||
| mdxFiles.sort(); | ||
|
|
||
| for (const filePath of mdxFiles) { | ||
| const rawContent = fs.readFileSync(filePath, 'utf-8'); | ||
| const { data, content } = parseFrontmatter(rawContent); | ||
| const cleanedContent = cleanMdxContent(content); | ||
|
|
||
| // Add file header | ||
| const relativePath = path.relative(docsDir, filePath); | ||
| aggregatedContent += `\n# ${relativePath}\n`; | ||
|
|
||
| // Add title if available | ||
| if (data.title) { | ||
| aggregatedContent += `## ${data.title}\n\n`; | ||
| } | ||
|
|
||
| // Add description if available | ||
| if (data.description) { | ||
| aggregatedContent += `${data.description}\n\n`; | ||
| } | ||
|
|
||
| // Add cleaned content | ||
| aggregatedContent += cleanedContent + '\n\n'; | ||
| aggregatedContent += '---\n\n'; | ||
| } | ||
|
|
||
| // Write to output file | ||
| const outputPath = path.join(outputDir, 'llm.txt'); | ||
| fs.writeFileSync(outputPath, aggregatedContent.trim()); | ||
|
|
||
| console.log(`✅ Generated LLM text file: ${outputPath}`); | ||
| console.log(`📄 Processed ${mdxFiles.length} MDX files`); | ||
| console.log(`📝 Total content size: ${aggregatedContent.length} characters`); | ||
| } | ||
|
|
||
| // Run the script | ||
| generateLlmTxt().catch(console.error); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Incomplete multi-character sanitization High