-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
524 lines (439 loc) · 17.6 KB
/
index.js
File metadata and controls
524 lines (439 loc) · 17.6 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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
const fs = require('fs-extra');
const path = require('path');
/**
* Docusaurus plugin to expose documentation as raw markdown for AI/LLM access
* - Converts MDX files to clean markdown
* - Generates _sitemap.md with full documentation structure
* - Adds "AI & Markdown" dropdown to each page with ChatGPT/Claude integration
*/
// Convert Tabs/TabItem components to readable markdown format
function convertTabsToMarkdown(content) {
const tabsPattern = /<Tabs[^>]*>([\s\S]*?)<\/Tabs>/g;
return content.replace(tabsPattern, (fullMatch, tabsContent) => {
const tabItemPattern = /<TabItem\s+[^>]*value="([^"]*)"[^>]*label="([^"]*)"[^>]*>([\s\S]*?)<\/TabItem>/g;
let result = [];
let match;
while ((match = tabItemPattern.exec(tabsContent)) !== null) {
const [, value, label, itemContent] = match;
const cleanContent = itemContent
.split('\n')
.map(line => line.replace(/^\s{4}/, ''))
.join('\n')
.trim();
result.push(`**${label}:**\n\n${cleanContent}`);
}
return result.join('\n\n---\n\n');
});
}
// Convert details/summary components to readable markdown format
function convertDetailsToMarkdown(content) {
const detailsPattern = /<details>\s*<summary>(<strong>)?([^<]+)(<\/strong>)?<\/summary>([\s\S]*?)<\/details>/g;
return content.replace(detailsPattern, (fullMatch, strongOpen, summaryText, strongClose, detailsContent) => {
const cleanContent = detailsContent
.split('\n')
.map(line => line.trim())
.filter(line => line.length > 0)
.join('\n')
.trim();
return `### ${summaryText.trim()}\n\n${cleanContent}`;
});
}
// Extract slug from frontmatter if present
function extractSlug(content) {
const frontmatterMatch = content.match(/^---\r?\n([\s\S]*?)\r?\n---/);
if (frontmatterMatch) {
const frontmatter = frontmatterMatch[1];
const slugMatch = frontmatter.match(/^slug:\s*(.+)$/m);
if (slugMatch) {
return slugMatch[1].trim();
}
}
return null;
}
// Extract title from frontmatter or first heading
function extractTitle(content) {
const frontmatterMatch = content.match(/^---\r?\n([\s\S]*?)\r?\n---/);
if (frontmatterMatch) {
const frontmatter = frontmatterMatch[1];
const titleMatch = frontmatter.match(/^title:\s*["']?([^"'\n]+)["']?$/m);
if (titleMatch) {
return titleMatch[1].trim();
}
}
const h1Match = content.match(/^#\s+(.+)$/m);
if (h1Match) {
return h1Match[1].trim();
}
return null;
}
// Extract sidebar_position from frontmatter
function extractSidebarPosition(content) {
const frontmatterMatch = content.match(/^---\r?\n([\s\S]*?)\r?\n---/);
if (frontmatterMatch) {
const frontmatter = frontmatterMatch[1];
const posMatch = frontmatter.match(/^sidebar_position:\s*(\d+)/m);
if (posMatch) {
return parseInt(posMatch[1], 10);
}
}
return 999;
}
// Clean markdown content for raw display - remove MDX/Docusaurus-specific syntax
function cleanMarkdownForDisplay(content, filepath, siteUrl, projectName) {
const fileDir = filepath.replace(/[^/]*$/, '');
// Strip YAML front matter
content = content.replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n/, '');
// Add header with link to sitemap
const header = `> This is official ${projectName} documentation. See [full sitemap](${siteUrl}/_sitemap.md) for all pages.\n\n`;
content = header + content;
// Remove import statements
content = content.replace(/^import\s+.*?from\s+['"].*?['"];?\s*$/gm, '');
// Convert HTML images to markdown
content = content.replace(
/<p align="center">\s*\n?\s*<img src=\{require\(['"]([^'"]+)['"]\)\.default\} alt="([^"]*)"(?:\s+width="[^"]*")?\s*\/>\s*\n?\s*<\/p>/g,
(match, imagePath, alt) => {
const cleanPath = imagePath.replace('@site/static/', '/');
return ``;
}
);
// Convert YouTube iframes to text links
content = content.replace(
/<iframe[^>]*src="https:\/\/www\.youtube\.com\/embed\/([a-zA-Z0-9_-]+)[^"]*"[^>]*title="([^"]*)"[^>]*>[\s\S]*?<\/iframe>/g,
'Watch the video: [$2](https://www.youtube.com/watch?v=$1)'
);
// Clean HTML5 video tags
content = content.replace(
/<video[^>]*>\s*<source src=["']([^"']+)["'][^>]*>\s*<\/video>/g,
'<video controls>\n <source src="$1" type="video/mp4" />\n <p>Video demonstration: $1</p>\n</video>'
);
// Remove <Head> components
content = content.replace(/<Head>[\s\S]*?<\/Head>/g, '');
// Convert Tabs/TabItem components
content = convertTabsToMarkdown(content);
// Convert details/summary components
content = convertDetailsToMarkdown(content);
// Remove custom React/MDX components
content = content.replace(/<[A-Z][a-zA-Z]*[\s\S]*?(?:\/>|<\/[A-Z][a-zA-Z]*>)/g, '');
// Convert relative image paths to absolute paths
content = content.replace(
/!\[([^\]]*)\]\((\.\/)?img\/([^)]+)\)/g,
(match, alt, relPrefix, filename) => {
return ``;
}
);
// Remove any leading blank lines
content = content.replace(/^\s*\n/, '');
return content;
}
// Recursively find all markdown/MDX files in a directory
function findMarkdownFiles(dir, fileList = [], baseDir = dir) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
findMarkdownFiles(filePath, fileList, baseDir);
} else if (file.endsWith('.md') || file.endsWith('.mdx')) {
const relativePath = path.relative(baseDir, filePath);
fileList.push(relativePath);
}
});
return fileList;
}
// Generate a markdown sitemap following sidebar hierarchy
function generateSitemap(mdFiles, docsDir, siteUrl, sidebarStructure, projectName, basePath = '') {
const lines = [
`# ${projectName} Documentation Sitemap`,
'',
`> Official ${projectName} documentation: ${siteUrl}`,
'',
'Each page link below points to the markdown version of the page.',
'',
];
// Build file data map
const fileDataMap = {};
for (const mdFile of mdFiles) {
const sourcePath = path.join(docsDir, mdFile);
const content = fs.readFileSync(sourcePath, 'utf8');
const slug = extractSlug(content);
const title = extractTitle(content) || mdFile.replace(/\.mdx?$/, '').split('/').pop();
const position = extractSidebarPosition(content);
let urlPath;
if (slug) {
urlPath = slug.startsWith('/') ? slug : '/' + slug;
if (urlPath === '/') urlPath = '/introduction';
} else {
urlPath = '/' + mdFile.replace(/\.mdx?$/, '');
}
// Prepend basePath to URL
const fullUrlPath = basePath + urlPath;
fileDataMap[mdFile] = {
title,
url: `${siteUrl}${fullUrlPath}.md`,
position,
dirName: mdFile.includes('/') ? mdFile.split('/')[0] : null
};
}
// Get files for a directory, sorted by sidebar_position
function getFilesForDir(dirName) {
return Object.entries(fileDataMap)
.filter(([file, data]) => {
if (!dirName) return !file.includes('/');
return file.startsWith(dirName + '/');
})
.sort((a, b) => a[1].position - b[1].position)
.map(([file, data]) => ({ file, ...data }));
}
// Render files with subcategories
function renderCategory(dirName, indent = 0) {
const files = getFilesForDir(dirName);
const prefix = ' '.repeat(indent) + '- ';
const subDirs = new Map();
const topLevelFiles = [];
for (const fileData of files) {
const relativePath = dirName ? fileData.file.slice(dirName.length + 1) : fileData.file;
if (relativePath.includes('/')) {
const subDir = relativePath.split('/')[0];
if (!subDirs.has(subDir)) {
subDirs.set(subDir, []);
}
subDirs.get(subDir).push(fileData);
} else {
topLevelFiles.push(fileData);
}
}
for (const fileData of topLevelFiles) {
lines.push(`${prefix}[${fileData.title}](${fileData.url})`);
}
for (const [subDir, subFiles] of subDirs) {
const subDirLabel = subDir.replace(/-/g, ' ').replace(/\b\w/g, c => c.toUpperCase());
lines.push(`${prefix}**${subDirLabel}/**`);
subFiles.sort((a, b) => a.position - b.position);
for (const fileData of subFiles) {
lines.push(`${' '.repeat(indent + 1)}- [${fileData.title}](${fileData.url})`);
}
}
}
// Follow sidebar structure if provided
if (sidebarStructure && sidebarStructure.length > 0) {
for (const item of sidebarStructure) {
if (item.type === 'doc') {
const fileEntry = Object.entries(fileDataMap).find(([file]) =>
file.replace(/\.mdx?$/, '') === item.id
);
if (fileEntry) {
lines.push(`- [${fileEntry[1].title}](${fileEntry[1].url})`);
}
} else if (item.type === 'category') {
lines.push(`- **${item.label}**`);
renderCategory(item.dirName, 1);
}
}
} else {
// Auto-generate from file structure
const topLevelDirs = [...new Set(mdFiles.filter(f => f.includes('/')).map(f => f.split('/')[0]))].sort();
const topLevelFiles = mdFiles.filter(f => !f.includes('/'));
for (const file of topLevelFiles) {
const data = fileDataMap[file];
if (data) {
lines.push(`- [${data.title}](${data.url})`);
}
}
for (const dir of topLevelDirs) {
const dirLabel = dir.replace(/-/g, ' ').replace(/\b\w/g, c => c.toUpperCase());
lines.push(`- **${dirLabel}**`);
renderCategory(dir, 1);
}
}
lines.push('');
lines.push('---');
lines.push('');
lines.push(`Generated: ${new Date().toISOString()}`);
return lines.join('\n');
}
// Parse sidebars.ts/js to extract structure
function parseSidebars(siteDir) {
const possiblePaths = [
path.join(siteDir, 'sidebars.ts'),
path.join(siteDir, 'sidebars.js'),
];
for (const sidebarPath of possiblePaths) {
if (fs.existsSync(sidebarPath)) {
try {
const content = fs.readFileSync(sidebarPath, 'utf8');
const structure = [];
// Parse category blocks with autogenerated dirName
const categoryRegex = /\{\s*type:\s*['"]category['"]\s*,\s*label:\s*['"]([^'"]+)['"]\s*,\s*items:\s*\[\s*\{\s*type:\s*['"]autogenerated['"]\s*,\s*dirName:\s*['"]([^'"]+)['"]\s*\}/g;
let match;
while ((match = categoryRegex.exec(content)) !== null) {
structure.push({ type: 'category', label: match[1], dirName: match[2] });
}
// Parse standalone doc references (simple string entries)
const docsMatch = content.match(/docs:\s*\[([\s\S]*?)\]/);
if (docsMatch) {
const docsContent = docsMatch[1];
// Find string entries like 'introduction' or "help-and-support"
const stringDocRegex = /['"]([a-zA-Z0-9-_/]+)['"]\s*(?:,|$)/g;
let docMatch;
while ((docMatch = stringDocRegex.exec(docsContent)) !== null) {
const docId = docMatch[1];
// Only add if not already captured as category
if (!structure.some(s => s.dirName === docId)) {
structure.push({ type: 'doc', id: docId, label: docId });
}
}
}
return structure;
} catch (e) {
console.warn('[docs-to-markdown] Could not parse sidebars file:', e.message);
}
}
}
return [];
}
// Build a map of slug -> source file path for dev server
function buildSlugToFileMap(docsDir, rootDocId) {
const mdFiles = findMarkdownFiles(docsDir);
const slugMap = {};
for (const mdFile of mdFiles) {
const sourcePath = path.join(docsDir, mdFile);
try {
const content = fs.readFileSync(sourcePath, 'utf8');
const slug = extractSlug(content);
let urlPath;
if (slug) {
urlPath = slug.startsWith('/') ? slug.slice(1) : slug;
if (urlPath === '' || urlPath === '/') urlPath = rootDocId;
} else {
urlPath = mdFile.replace(/\.mdx?$/, '');
}
slugMap[urlPath] = { sourcePath, mdFile };
} catch (e) {
// Skip files that can't be read
}
}
return slugMap;
}
/**
* Plugin factory function
* @param {Object} context - Docusaurus context
* @param {Object} options - Plugin options
* @param {string} options.rootDocId - Document ID for root slug (default: 'introduction')
* @param {string} options.projectName - Project name for headers (default: from site title)
* @param {string} options.routeBasePath - Base path for docs routes (default: '/')
*/
module.exports = function docsToMarkdownPlugin(context, options = {}) {
const { rootDocId = 'introduction', routeBasePath = '/' } = options;
const projectName = options.projectName || context.siteConfig.title || 'Documentation';
const sidebarStructure = parseSidebars(context.siteDir);
const docsDir = path.join(context.siteDir, 'docs');
// Normalize routeBasePath - ensure it starts with / but doesn't end with /
const normalizedBasePath = routeBasePath === '/' ? '' :
(routeBasePath.startsWith('/') ? routeBasePath : '/' + routeBasePath).replace(/\/$/, '');
return {
name: 'docusaurus-plugin-docs-to-markdown',
getThemePath() {
return path.resolve(__dirname, './theme');
},
// Add dev server middleware to serve .md files on-the-fly
configureWebpack(config, isServer, utils) {
return {
devServer: {
setupMiddlewares(middlewares, devServer) {
// Create middleware function to handle .md requests
const mdMiddleware = (req, res, next) => {
const urlPath = req.path;
// Only handle .md requests
if (!urlPath.endsWith('.md')) {
return next();
}
// Handle _sitemap.md specially
if (urlPath === '/_sitemap.md') {
try {
const mdFiles = findMarkdownFiles(docsDir);
const siteUrl = context.siteConfig.url || 'http://localhost:3000';
const sitemap = generateSitemap(mdFiles, docsDir, siteUrl, sidebarStructure, projectName, normalizedBasePath);
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.send(sitemap);
return;
} catch (e) {
console.error('[docs-to-markdown] Error generating sitemap:', e.message);
return next();
}
}
// Strip .md extension and leading slash to get the slug
let slug = urlPath.replace(/\.md$/, '').replace(/^\//, '');
// Strip routeBasePath prefix if present
if (normalizedBasePath && slug.startsWith(normalizedBasePath.slice(1) + '/')) {
slug = slug.slice(normalizedBasePath.length);
}
// Build slug map (rebuilt on each request to pick up changes)
const slugMap = buildSlugToFileMap(docsDir, rootDocId);
const fileInfo = slugMap[slug];
if (!fileInfo) {
return next();
}
try {
const content = fs.readFileSync(fileInfo.sourcePath, 'utf8');
const siteUrl = context.siteConfig.url || 'http://localhost:3000';
const cleanedContent = cleanMarkdownForDisplay(content, fileInfo.mdFile, siteUrl, projectName);
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.send(cleanedContent);
} catch (e) {
console.error(`[docs-to-markdown] Error serving ${slug}.md:`, e.message);
next();
}
};
// Add our middleware at the very beginning
middlewares.unshift({
name: 'docs-to-markdown',
middleware: mdMiddleware,
});
return middlewares;
},
},
};
},
async postBuild({ outDir }) {
const buildDir = outDir;
const siteUrl = context.siteConfig.url;
console.log('[docs-to-markdown] Copying markdown source files...');
if (normalizedBasePath) {
console.log(`[docs-to-markdown] Using routeBasePath: ${normalizedBasePath}`);
}
const mdFiles = findMarkdownFiles(docsDir);
let copiedCount = 0;
for (const mdFile of mdFiles) {
const sourcePath = path.join(docsDir, mdFile);
try {
const content = await fs.readFile(sourcePath, 'utf8');
const slug = extractSlug(content);
let destFile;
if (slug) {
let slugPath = slug.startsWith('/') ? slug.slice(1) : slug;
if (slugPath === '' || slugPath === '/') slugPath = rootDocId;
destFile = slugPath + '.md';
} else {
destFile = mdFile.replace(/\.mdx$/, '.md');
}
// Prepend routeBasePath to destination
const destPath = normalizedBasePath
? path.join(buildDir, normalizedBasePath.slice(1), destFile)
: path.join(buildDir, destFile);
await fs.ensureDir(path.dirname(destPath));
const cleanedContent = cleanMarkdownForDisplay(content, mdFile, siteUrl, projectName);
await fs.writeFile(destPath, cleanedContent, 'utf8');
copiedCount++;
} catch (error) {
console.error(` Failed to process ${mdFile}:`, error.message);
}
}
console.log(`[docs-to-markdown] Successfully copied ${copiedCount} markdown files`);
// Generate _sitemap.md
console.log('[docs-to-markdown] Generating _sitemap.md...');
const sitemap = generateSitemap(mdFiles, docsDir, siteUrl, sidebarStructure, projectName, normalizedBasePath);
await fs.writeFile(path.join(buildDir, '_sitemap.md'), sitemap, 'utf8');
console.log('[docs-to-markdown] Successfully generated _sitemap.md');
},
};
};