Skip to content

Commit 07b8ce9

Browse files
committed
fix: llms.txt generator
1 parent f994200 commit 07b8ce9

File tree

3 files changed

+41
-43
lines changed

3 files changed

+41
-43
lines changed

apps/website/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@docusaurus/theme-search-algolia": "^3.7.0",
2424
"@mdx-js/react": "^3.0.0",
2525
"clsx": "^2.0.0",
26+
"gray-matter": "^4.0.3",
2627
"prism-react-renderer": "^2.3.0",
2728
"react": "^19.0.0",
2829
"react-dom": "^19.0.0"

apps/website/src/plugins/llms-txt.js

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,75 @@
11
const path = require('path');
22
const fs = require('fs');
3+
const matter = require('gray-matter');
4+
5+
const DOCS_URL = 'https://commandkit.dev/docs/next/guide';
36

47
module.exports = function (context) {
58
return {
69
name: 'llms-txt-plugin',
710
loadContent: async () => {
811
const { siteDir } = context;
912
const docsDir = path.join(siteDir, 'docs');
13+
const guideDir = path.join(docsDir, 'guide');
1014
const versionedDocsDir = path.join(siteDir, 'versioned_docs');
11-
const allMdx = [];
15+
const allDocs = [];
1216

13-
// Recursive function to get all mdx and md files (excluding API reference)
1417
const getMdxFiles = async (dir, versionPrefix = '') => {
1518
if (!fs.existsSync(dir)) return;
1619

20+
// skip versioned docs
21+
if (dir.includes(versionedDocsDir)) return;
22+
1723
const entries = await fs.promises.readdir(dir, { withFileTypes: true });
1824

1925
for (const entry of entries) {
2026
const fullPath = path.join(dir, entry.name);
2127
if (entry.isDirectory()) {
22-
// Skip api-reference directories
23-
if (entry.name === 'api-reference') {
24-
continue;
25-
}
2628
await getMdxFiles(fullPath, versionPrefix);
27-
} else if (
28-
entry.name.endsWith('.mdx') ||
29-
entry.name.endsWith('.md')
30-
) {
31-
// Skip files in api-reference paths
32-
if (fullPath.includes('api-reference')) {
33-
continue;
34-
}
29+
} else if (entry.name.endsWith('.mdx') || entry.name.endsWith('.md')) {
3530
const content = await fs.promises.readFile(fullPath, 'utf8');
36-
allMdx.push({
37-
content,
38-
path: fullPath,
31+
const { data: frontmatter } = matter(content);
32+
33+
// Get relative path from guide directory
34+
const relativePath = path.relative(guideDir, fullPath)
35+
.replace(/\.mdx?$/, '')
36+
.replace(/\\/g, '/');
37+
38+
allDocs.push({
39+
title: frontmatter.title,
40+
description: frontmatter.description,
41+
path: relativePath,
3942
version: versionPrefix,
4043
});
4144
}
4245
}
4346
};
4447

45-
// Get current/latest docs
46-
await getMdxFiles(docsDir, 'latest');
47-
48-
// Get versioned docs
49-
if (fs.existsSync(versionedDocsDir)) {
50-
const versionDirs = await fs.promises.readdir(versionedDocsDir, {
51-
withFileTypes: true,
52-
});
53-
for (const versionDir of versionDirs) {
54-
if (versionDir.isDirectory()) {
55-
const versionName = versionDir.name.replace('version-', '');
56-
await getMdxFiles(
57-
path.join(versionedDocsDir, versionDir.name),
58-
versionName,
59-
);
60-
}
61-
}
48+
// Only process guide directory
49+
if (fs.existsSync(guideDir)) {
50+
await getMdxFiles(guideDir, 'latest');
6251
}
6352

64-
return { allMdx };
53+
return { allDocs };
6554
},
6655
postBuild: async ({ content, outDir }) => {
67-
const { allMdx } = content;
56+
const { allDocs } = content;
6857

69-
// Filter out any remaining API reference content and concatenate
70-
const filteredContent = allMdx
71-
.filter((item) => !item.path.includes('api-reference'))
72-
.map((item) => item.content)
73-
.join('\n\n---\n\n');
58+
// Generate markdown content
59+
const markdownContent = [
60+
'# CommandKit Docs\n',
61+
...allDocs
62+
.filter(doc => doc.title && doc.description)
63+
.map(doc => {
64+
const url = `${DOCS_URL}/${doc.path}`;
65+
return `- [${doc.title}](${url}): ${doc.description || doc.title}`;
66+
})
67+
].join('\n');
7468

75-
// Write concatenated content as llms.txt
69+
// Write markdown content
7670
const llmsTxtPath = path.join(outDir, 'llms.txt');
7771
try {
78-
await fs.promises.writeFile(llmsTxtPath, filteredContent);
72+
await fs.promises.writeFile(llmsTxtPath, markdownContent);
7973
console.log('✅ llms.txt generated successfully');
8074
} catch (err) {
8175
console.error('❌ Error generating llms.txt:', err);

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)