Repository navigation
Expand file tree
/
Copy pathzerops-llm-script.ts
More file actions
241 lines (205 loc) Β· 8 KB
/
Copy pathzerops-llm-script.ts
File metadata and controls
241 lines (205 loc) Β· 8 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import * as fs from 'node:fs'
import * as path from 'node:path'
import * as globModule from 'glob'
// Shared MDX -> markdown converters (also used by the per-page markdown-source
// plugin). Single source of truth so the LLM bundle and the per-page .md route
// produce consistent output.
import * as C from './apps/docs/src/plugins/markdown-source/converters'
const frontmatterRegex = /^\n*---(\n.+)*?\n---\n/
const frontmatterBlockRegex = /^\s*---\n([\s\S]*?)\n---\n/
const contentDir = path.resolve('apps/docs/content')
const dataJsonPath = path.resolve('apps/docs/static/data.json')
// Lazy data.json loader for UnorderedCodeList resolution.
let dataJsonCache: Record<string, unknown> | null = null
function getDataJson(): Record<string, unknown> {
if (dataJsonCache) return dataJsonCache
try {
dataJsonCache = JSON.parse(fs.readFileSync(dataJsonPath, 'utf-8'))
} catch {
dataJsonCache = {}
}
return dataJsonCache ?? {}
}
const sliceExt = (file: string) => {
return file.split('.').slice(0, -1).join('.')
}
const extractLabel = (file: string) => {
const pathWithoutExt = sliceExt(file)
const parts = pathWithoutExt.split('/')
return parts.map(part =>
part.split('-')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ')
).join(' > ')
}
function capitalizeDelimiter(str: string): string {
return str
.split('-')
.map((s: string) => s.charAt(0).toUpperCase() + s.slice(1))
.join('-')
}
function cleanMarkdownContent(content: string): string {
// 1. Strip frontmatter
let cleaned = content.replace(frontmatterRegex, '')
// 2. Drop JSX comments, normalize <br>, drop import lines, drop images.
cleaned = cleaned.replace(/\{\/\*[\s\S]*?\*\/\}/g, '')
cleaned = cleaned.replace(/<br\s*\/?>/g, '\n')
cleaned = cleaned.replace(/^import\s+.*?from\s+['"].*?['"];?\s*$/gm, '')
cleaned = cleaned.replace(/<Image\s+[^>]*\/?>/g, '')
cleaned = cleaned.replace(/<img\s+[^>]*\/?>/g, '')
cleaned = cleaned.replace(/!\[[^\]]*\]\([^)]+\)/g, '')
cleaned = cleaned.replace(/<figure[^>]*>([\s\S]*?)<\/figure>/g, '')
cleaned = cleaned.replace(/<Head>[\s\S]*?<\/Head>/g, '')
// 3. Convert HTML <a> tags to markdown links (mirrors the plugin's step 12).
cleaned = cleaned.replace(
/<a\s+[^>]*href=["']([^"']+)["'][^>]*>([\s\S]*?)<\/a>/g,
(_m, href, text) => {
const inner = String(text)
.replace(/<code>(.*?)<\/code>/g, '`$1`')
.replace(/<strong>(.*?)<\/strong>/g, '**$1**')
.replace(/<em>(.*?)<\/em>/g, '*$1*')
.replace(/<b>(.*?)<\/b>/g, '**$1**')
.replace(/<[^>]+>/g, '')
.trim()
return `[${inner}](${href})`
}
)
// 4. Shared converter chain (same module the per-page .md route uses).
cleaned = C.convertFAQToMarkdown(cleaned)
cleaned = C.convertTabsToMarkdown(cleaned)
cleaned = C.convertNoteToMarkdown(cleaned)
cleaned = C.convertAsciiGraphToMarkdown(cleaned)
cleaned = C.convertDocCardListToMarkdown(cleaned)
cleaned = C.convertCustomCardToMarkdown(cleaned)
cleaned = C.convertDropdownToMarkdown(cleaned)
cleaned = C.convertSectionLandscapeToMarkdown(cleaned)
cleaned = C.convertExpandableTableToMarkdown(cleaned)
cleaned = C.convertUnorderedCodeListStandalone(cleaned, getDataJson, null)
cleaned = C.convertCodingAgentsTopologyToMarkdown(cleaned)
cleaned = C.convertIntroAgentVisualToMarkdown(cleaned)
cleaned = C.convertDeployButtonToMarkdown(cleaned)
cleaned = C.convertVideoToMarkdown(cleaned)
cleaned = C.convertBadgeToMarkdown(cleaned)
cleaned = C.convertButtonToMarkdown(cleaned)
cleaned = C.convertLinkToMarkdown(cleaned)
cleaned = C.dropSilentComponents(cleaned)
cleaned = C.convertDetailsToMarkdown(cleaned)
// 5. Final catch-all: strip any remaining unknown uppercase JSX components
// while preserving markdown tables (the LLM corpus reads tables as data).
const sections = cleaned.split(/(\|.*\|\n\|.*\|\n(\|.*\|\n)*)/)
let processed = ''
for (const section of sections) {
if (!section) continue
if (section.trim().startsWith('|')) {
processed += section
} else {
processed += section.replace(
/<[A-Z][a-zA-Z]*[\s\S]*?(?:\/>|<\/[A-Z][a-zA-Z]*>)/g,
''
)
}
}
// 6. Collapse trailing whitespace + consecutive blank lines.
const lines = processed.split('\n').map((line) => line.replace(/[ \t]+$/g, ''))
const out: string[] = []
let lastEmpty = false
for (const line of lines) {
const trimmed = line.trim()
if (trimmed === '' && lastEmpty) continue
out.push(line)
lastEmpty = trimmed === ''
}
return out.join('\n')
}
function hasFrontmatterFlag(content: string, flag: string): boolean {
const frontmatter = content.match(frontmatterBlockRegex)?.[1] ?? ''
const flagRegex = new RegExp(`^${flag}:\\s*true\\s*$`, 'm')
return flagRegex.test(frontmatter)
}
function shouldIncludeInLLMDocs(file: string): boolean {
const fileContent = fs.readFileSync(path.resolve(contentDir, file), 'utf-8')
return (
!hasFrontmatterFlag(fileContent, 'draft') &&
!hasFrontmatterFlag(fileContent, 'unlisted')
)
}
async function generateContent(
files: string[],
contentDir: string
): Promise<string> {
let content = ''
for (let i = 0; i < files.length; i++) {
const file = files[i]
console.log(`> Writing '${file}' `)
const fileContent = fs.readFileSync(
path.resolve(contentDir, file),
'utf-8'
)
const cleanedContent = cleanMarkdownContent(fileContent)
const title = extractLabel(file)
if (i === 0) {
content += '-'.repeat(40) + '\n\n'
}
content += `# ${title}\n\n${cleanedContent}\n\n`
content += '-'.repeat(40) + '\n\n'
}
return content
}
async function generateLLMDocs() {
const publicDir = path.resolve('apps/docs/static')
if (!fs.existsSync(publicDir)) {
fs.mkdirSync(publicDir, { recursive: true })
}
const outputListFile = path.resolve(publicDir, 'llms.txt')
const optionalFiles = globModule
.sync('**/*.mdx', { cwd: contentDir })
.filter(shouldIncludeInLLMDocs)
const optionals: string[] = []
for (const file of optionalFiles) {
optionals.push(
`- [${capitalizeDelimiter(extractLabel(file)).replace(/-/, ' ')}](https://docs.zerops.io/${sliceExt(file)}.md)`
)
}
fs.writeFileSync(
outputListFile,
[
'# Zerops',
'',
'> Zerops is a developer-first Platform-as-a-Service, running on bare metal, with every part built from scratch. Zerops aims to be the perfect mix of developer experience, flexibility, scalability and affordability, making it a great fit for applications of any size, complexity and traffic.',
'',
'## Docs',
'',
`- [Full Docs](https://docs.zerops.io/llms-full.txt): The complete documentation as a single file (${optionalFiles.length} pages, no images).`,
'- [Core Docs](https://docs.zerops.io/llms-small.txt): The same, trimmed to the core platform β excludes API/CLI references, company and help pages.',
'',
'Every page below is also reachable in its rendered form by dropping the `.md`',
'suffix, and any docs.zerops.io URL can be turned into clean markdown by adding it.',
'',
'## Optional',
'',
...optionals,
].join('\n'),
'utf-8'
)
console.log(`< Output '${outputListFile}' `)
const outputFullFile = path.resolve(publicDir, 'llms-full.txt')
const files = globModule
.sync('**/*.mdx', { cwd: contentDir })
.filter(shouldIncludeInLLMDocs)
const fullContent = await generateContent(
files,
contentDir
)
fs.writeFileSync(outputFullFile, fullContent, 'utf-8')
console.log(`< Output '${outputFullFile}' `)
const outputTinyFile = path.resolve(publicDir, 'llms-small.txt')
const tinyExclude = ['references', 'company', 'help']
const tinyFiles = files.filter((filename: string) => !tinyExclude.some(exclude => filename.includes(exclude)))
const tinyContent = await generateContent(
tinyFiles,
contentDir
)
fs.writeFileSync(outputTinyFile, tinyContent, 'utf-8')
console.log(`< Output '${outputTinyFile}' `)
}
generateLLMDocs().catch(console.error)