-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathgenerate-llm-files.js
86 lines (73 loc) · 2.85 KB
/
generate-llm-files.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* eslint-disable */
const fs = require('fs');
const path = require('path');
const glob = require('glob');
// Ensure the static directory exists
const staticDir = path.join(__dirname, '..', 'static');
if (!fs.existsSync(staticDir)) {
fs.mkdirSync(staticDir, { recursive: true });
}
// Create a directory for markdown files
const markdownDir = path.join(staticDir, 'markdown');
if (!fs.existsSync(markdownDir)) {
fs.mkdirSync(markdownDir, { recursive: true });
}
// Find all markdown files
const files = glob.sync('./docs/**/*.md?(x)');
const llms = [];
const fullContent = [];
files.forEach((file) => {
const content = fs.readFileSync(file, 'utf-8');
const relativePath = path.relative('./docs', file);
const fileDir = path.dirname(file);
// Add to llms.txt (just the paths)
llms.push(relativePath);
// Add to llms-full.txt (full content with separators)
fullContent.push(`\n=== ${relativePath} ===\n${content}`);
// Create a markdown file for each page
const markdownFileName = relativePath.replace(/\.mdx?$/, '.md');
const markdownFilePath = path.join(markdownDir, markdownFileName);
// Ensure the directory exists
const markdownFileDir = path.dirname(markdownFilePath);
if (!fs.existsSync(markdownFileDir)) {
fs.mkdirSync(markdownFileDir, { recursive: true });
}
// Extract import statements to create a map of image variables to file paths
const importMap = {};
const importRegex = /import\s+(\w+)\s+from\s+['"](.+?)['"];?/g;
let importMatch;
while ((importMatch = importRegex.exec(content)) !== null) {
const [_, varName, filePath] = importMatch;
// Handle relative paths
const fullPath =
filePath.startsWith('./') || filePath.startsWith('../')
? path.relative(fileDir, path.resolve(fileDir, filePath))
: filePath;
importMap[varName] = fullPath;
}
// Process the content to handle common React/MDX components
let processedContent = content
// Convert import statements to comments
.replace(
/import\s+(.*?)\s+from\s+['"](.+?)['"];?/g,
'<!-- Import: $1 from "$2" -->',
)
// Handle image components with src
.replace(
/<img\s+src=\{(.+?)\}\s+alt="(.+?)"\s+style=\{\{(.+?)\}\}\s*\/>/g,
'',
)
// Replace image variables with their file paths
.replace(/!\[(.*?)\]\((\w+)\)/g, (match, alt, varName) => {
return importMap[varName] ? `` : match;
})
// Clean up multiple newlines
.replace(/\n{3,}/g, '\n\n')
.trim();
// Write the processed markdown file - directly as Markdown, not wrapped in HTML
fs.writeFileSync(markdownFilePath, processedContent);
});
// Write the files
fs.writeFileSync(path.join(staticDir, 'llms.txt'), llms.join('\n'));
fs.writeFileSync(path.join(staticDir, 'llms-full.txt'), fullContent.join('\n'));
console.log('✅ Generated llms.txt, llms-full.txt, and markdown files');