forked from stack-auth/stack-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-docs.js
More file actions
451 lines (372 loc) · 15.7 KB
/
generate-docs.js
File metadata and controls
451 lines (372 loc) · 15.7 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import fs from 'fs';
import { glob } from 'glob';
import yaml from 'js-yaml';
import path from 'path';
import { fileURLToPath } from 'url';
// Get __dirname equivalent in ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Configure paths
const TEMPLATE_DIR = path.resolve(__dirname, '../templates');
const PYTHON_TEMPLATE_DIR = path.resolve(__dirname, '../templates-python');
const OUTPUT_BASE_DIR = path.resolve(__dirname, '../content/docs');
const CONFIG_FILE = path.resolve(__dirname, '../docs-platform.yml');
const PLATFORMS = ['next', 'react', 'js', 'python'];
// Platform groups mapping
const PLATFORM_GROUPS = {
'react-like': ['next', 'react'], // Platforms that use React components
'js-like': ['next', 'react', 'js'] // Platforms that use JavaScript SDK (includes React-based platforms)
};
// Load platform configuration
let platformConfig = {};
try {
const configContent = fs.readFileSync(CONFIG_FILE, 'utf8');
platformConfig = yaml.load(configContent);
console.log('Loaded platform configuration from docs-platform.yml');
} catch (error) {
console.error('Failed to load platform configuration:', error.message);
console.log('Falling back to include all files for all platforms');
}
// Platform folder naming - now using root folders
function getFolderName(platform) {
return platform; // Use direct platform names instead of pages-{platform}
}
// Platform display names
function getPlatformDisplayName(platform) {
const platformNames = {
'next': 'Next.js',
'react': 'React',
'js': 'JavaScript',
'python': 'Python'
};
return platformNames[platform] || platform;
}
// Platform-specific content markers - Updated regex to handle both syntaxes (with and without colon)
const PLATFORM_START_MARKER = /{\s*\/\*\s*IF_PLATFORM:?\s*([\w-]+)\s*\*\/\s*}/;
const PLATFORM_ELSE_MARKER = /{\s*\/\*\s*ELSE_IF_PLATFORM:?\s+([\w-]+)\s*\*\/\s*}/;
const PLATFORM_END_MARKER = /{\s*\/\*\s*END_PLATFORM\s*\*\/\s*}/;
/**
* Check if a platform or platform group includes the target platform
*/
function isPlatformMatch(platformSpec, targetPlatform) {
// Direct platform match
if (platformSpec === targetPlatform) {
return true;
}
// Platform group match
if (PLATFORM_GROUPS[platformSpec]) {
return PLATFORM_GROUPS[platformSpec].includes(targetPlatform);
}
return false;
}
/**
* Check if a file should be included for a specific platform
*/
function shouldIncludeFileForPlatform(platform, filePath) {
// If no configuration loaded, include everything
if (!platformConfig.pages) {
return true;
}
// Find the page configuration for this file
const pageConfig = platformConfig.pages.find(page => page.path === filePath);
// If no specific configuration found, exclude by default
if (!pageConfig) {
console.log(`No configuration found for ${filePath}, excluding by default`);
return false;
}
// Check if the platform is in the allowed list
return pageConfig.platforms.includes(platform);
}
/**
* Process a template file for a specific platform
*/
function processTemplateForPlatform(content, targetPlatform) {
const lines = content.split('\n');
let result = [];
let currentPlatformSpec = null;
let isIncluding = true;
let platformSection = false;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
// Check for platform start
const startMatch = line.match(PLATFORM_START_MARKER);
if (startMatch) {
platformSection = true;
currentPlatformSpec = startMatch[1];
isIncluding = isPlatformMatch(currentPlatformSpec, targetPlatform);
continue;
}
// Check for platform else
const elseMatch = line.match(PLATFORM_ELSE_MARKER);
if (elseMatch && platformSection) {
currentPlatformSpec = elseMatch[1];
isIncluding = isPlatformMatch(currentPlatformSpec, targetPlatform);
continue;
}
// Check for platform end
const endMatch = line.match(PLATFORM_END_MARKER);
if (endMatch && platformSection) {
platformSection = false;
isIncluding = true;
continue;
}
// Include the line if we're supposed to
if (isIncluding) {
result.push(line);
}
}
return result.join('\n');
}
/**
* Generate meta.json files for Fumadocs navigation
*/
function generateMetaFiles() {
// Process meta.json files for each platform from templates
for (const platform of PLATFORMS) {
const folderName = getFolderName(platform);
const platformDisplayName = getPlatformDisplayName(platform);
// For Python platform, prioritize Python-specific templates, but also include shared templates
const templateDir = (platform === 'python' && fs.existsSync(PYTHON_TEMPLATE_DIR)) ? PYTHON_TEMPLATE_DIR : TEMPLATE_DIR;
// Find all meta.json files in the appropriate template directory
const metaFiles = glob.sync('**/meta.json', { cwd: templateDir });
// For Python, also get meta.json files from shared templates (excluding root meta.json to avoid conflicts)
let sharedMetaFiles = [];
if (platform === 'python' && fs.existsSync(PYTHON_TEMPLATE_DIR)) {
sharedMetaFiles = glob.sync('**/meta.json', { cwd: TEMPLATE_DIR }).filter(file => file !== 'meta.json');
}
// Process Python-specific meta files
for (const metaFile of metaFiles) {
const srcPath = path.join(templateDir, metaFile);
const destPath = path.join(OUTPUT_BASE_DIR, folderName, metaFile);
// If this is a nested meta.json (not root), check if the folder should exist for this platform
if (metaFile !== 'meta.json') {
const folderPath = path.dirname(metaFile);
// Check if any pages in this folder are included for this platform
const hasContentInFolder = platformConfig.pages && platformConfig.pages.some(configPage =>
configPage.path.startsWith(`${folderPath}/`) &&
configPage.platforms.includes(platform)
);
if (!hasContentInFolder) {
console.log(`Skipped meta.json for ${folderPath} (no content for ${platform})`);
continue; // Skip this meta.json file
}
}
// Read and parse the template meta.json
const templateContent = fs.readFileSync(srcPath, 'utf8');
const metaData = JSON.parse(templateContent);
// If this is the root meta.json, customize it for the platform
if (metaFile === 'meta.json') {
metaData.title = platformDisplayName;
metaData.description = `Stack Auth for ${platformDisplayName} applications`;
metaData.root = true;
// Filter pages based on platform configuration
if (platformConfig.pages && metaData.pages) {
const cleanedPages = [];
let currentSectionPages = [];
let currentSectionHeader = null;
for (let i = 0; i < metaData.pages.length; i++) {
const page = metaData.pages[i];
// If this is a section divider
if (typeof page === 'string' && page.startsWith('---')) {
// Process the previous section first (or pages before first section)
if (currentSectionPages.length > 0) {
if (currentSectionHeader !== null) {
// Add section header if we had one
cleanedPages.push(currentSectionHeader);
}
cleanedPages.push(...currentSectionPages);
}
// Start new section
currentSectionHeader = page;
currentSectionPages = [];
}
// If this is a folder reference (like "...customization")
else if (typeof page === 'string' && page.startsWith('...')) {
// Only include folder references if they have content for this platform
const folderName = page.substring(3); // Remove "..."
const hasContentInFolder = platformConfig.pages.some(configPage =>
configPage.path.startsWith(`${folderName}/`) &&
configPage.platforms.includes(platform)
);
if (hasContentInFolder) {
currentSectionPages.push(page);
}
}
// Regular page
else {
// Check if this is actually a folder reference vs a page reference
// Check both template directories for Python
let folderPath = path.join(templateDir, page);
let isActualFolder = fs.existsSync(folderPath) && fs.statSync(folderPath).isDirectory();
// For Python, also check shared templates directory
if (!isActualFolder && platform === 'python') {
folderPath = path.join(TEMPLATE_DIR, page);
isActualFolder = fs.existsSync(folderPath) && fs.statSync(folderPath).isDirectory();
}
if (isActualFolder) {
// This is a folder reference - check if folder has content for this platform
const hasContentInFolder = platformConfig.pages.some(configPage =>
configPage.path.startsWith(`${page}/`) &&
configPage.platforms.includes(platform)
);
if (hasContentInFolder) {
currentSectionPages.push(page);
}
} else {
// This is a regular page reference
const pagePath = `${page}.mdx`;
const shouldInclude = shouldIncludeFileForPlatform(platform, pagePath);
if (shouldInclude) {
currentSectionPages.push(page);
}
}
}
}
// Don't forget the last section (or remaining pages)
if (currentSectionPages.length > 0) {
if (currentSectionHeader !== null) {
cleanedPages.push(currentSectionHeader);
}
cleanedPages.push(...currentSectionPages);
}
metaData.pages = cleanedPages;
}
}
// Create directory if it doesn't exist
fs.mkdirSync(path.dirname(destPath), { recursive: true });
// Write the processed meta.json
fs.writeFileSync(destPath, JSON.stringify(metaData, null, 2));
console.log(`Generated platform-specific meta.json for ${platform}: ${destPath}`);
}
// For Python, also process shared meta.json files (but not root)
for (const metaFile of sharedMetaFiles) {
const folderPath = path.dirname(metaFile);
// Check if any pages in this folder are included for Python
const hasContentInFolder = platformConfig.pages && platformConfig.pages.some(configPage =>
configPage.path.startsWith(`${folderPath}/`) &&
configPage.platforms.includes(platform)
);
if (hasContentInFolder) {
const srcPath = path.join(TEMPLATE_DIR, metaFile);
const destPath = path.join(OUTPUT_BASE_DIR, folderName, metaFile);
// Read and copy the shared meta.json
const templateContent = fs.readFileSync(srcPath, 'utf8');
// Create directory if it doesn't exist
fs.mkdirSync(path.dirname(destPath), { recursive: true });
// Write the shared meta.json
fs.writeFileSync(destPath, templateContent);
console.log(`Generated shared meta.json for ${platform}: ${destPath}`);
}
}
}
}
/**
* Copy assets from template to platform-specific directories
*/
function copyAssets() {
const assetDirs = ['imgs'];
// Copy assets from main templates directory
for (const dir of assetDirs) {
const srcDir = path.join(TEMPLATE_DIR, dir);
if (fs.existsSync(srcDir)) {
// Copy assets to each platform directory
for (const platform of PLATFORMS) {
const folderName = getFolderName(platform);
const destDir = path.join(OUTPUT_BASE_DIR, folderName, dir);
fs.mkdirSync(destDir, { recursive: true });
// Find and copy all files
const files = glob.sync('**/*', { cwd: srcDir, nodir: true });
for (const file of files) {
const srcFile = path.join(srcDir, file);
const destFile = path.join(destDir, file);
fs.mkdirSync(path.dirname(destFile), { recursive: true });
fs.copyFileSync(srcFile, destFile);
console.log(`Copied asset: ${srcFile} -> ${destFile}`);
}
}
}
}
// Copy Python-specific assets if they exist
if (fs.existsSync(PYTHON_TEMPLATE_DIR)) {
for (const dir of assetDirs) {
const srcDir = path.join(PYTHON_TEMPLATE_DIR, dir);
if (fs.existsSync(srcDir)) {
const destDir = path.join(OUTPUT_BASE_DIR, 'python', dir);
fs.mkdirSync(destDir, { recursive: true });
// Find and copy all files
const files = glob.sync('**/*', { cwd: srcDir, nodir: true });
for (const file of files) {
const srcFile = path.join(srcDir, file);
const destFile = path.join(destDir, file);
fs.mkdirSync(path.dirname(destFile), { recursive: true });
fs.copyFileSync(srcFile, destFile);
console.log(`Copied Python-specific asset: ${srcFile} -> ${destFile}`);
}
}
}
}
}
/**
* Main function to generate platform-specific docs
*/
function generateDocs() {
// Find all MDX files in the main template directory
const templateFiles = glob.sync('**/*.mdx', { cwd: TEMPLATE_DIR });
if (templateFiles.length === 0) {
console.warn(`No template files found in ${TEMPLATE_DIR}`);
return;
}
console.log(`Found ${templateFiles.length} shared template files`);
// Process shared templates for each platform
for (const platform of PLATFORMS) {
const folderName = getFolderName(platform);
const outputDir = path.join(OUTPUT_BASE_DIR, folderName);
// Create the output directory
fs.mkdirSync(outputDir, { recursive: true });
// Process each shared template file
for (const file of templateFiles) {
// Check if this file should be included for this platform
if (!shouldIncludeFileForPlatform(platform, file)) {
console.log(`Skipped file (not configured for platform): ${file} for ${platform}`);
continue;
}
const inputFile = path.join(TEMPLATE_DIR, file);
const outputFile = path.join(outputDir, file);
// Read the template
const templateContent = fs.readFileSync(inputFile, 'utf8');
// Process for this platform
const processedContent = processTemplateForPlatform(templateContent, platform);
// Create output directory if it doesn't exist
fs.mkdirSync(path.dirname(outputFile), { recursive: true });
// Write the processed content
fs.writeFileSync(outputFile, processedContent);
console.log(`Generated: ${outputFile}`);
}
}
// Process Python-specific templates if they exist
if (fs.existsSync(PYTHON_TEMPLATE_DIR)) {
console.log(`Processing Python-specific templates from ${PYTHON_TEMPLATE_DIR}`);
const pythonTemplateFiles = glob.sync('**/*.mdx', { cwd: PYTHON_TEMPLATE_DIR });
if (pythonTemplateFiles.length > 0) {
const pythonOutputDir = path.join(OUTPUT_BASE_DIR, 'python');
for (const file of pythonTemplateFiles) {
const inputFile = path.join(PYTHON_TEMPLATE_DIR, file);
const outputFile = path.join(pythonOutputDir, file);
// Read the Python-specific template
const templateContent = fs.readFileSync(inputFile, 'utf8');
// Create output directory if it doesn't exist
fs.mkdirSync(path.dirname(outputFile), { recursive: true });
// Write the content (no platform processing needed for Python-specific files)
fs.writeFileSync(outputFile, templateContent);
console.log(`Generated Python-specific: ${outputFile}`);
}
}
}
// Generate meta.json files for navigation
generateMetaFiles();
// Copy assets (images, etc.)
copyAssets();
console.log('Documentation generation complete!');
}
// Run the generator
generateDocs();