-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.js
More file actions
108 lines (88 loc) · 2.8 KB
/
Copy pathgenerate.js
File metadata and controls
108 lines (88 loc) · 2.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
import fs from 'fs';
import path from 'path';
const docsDir = path.resolve('./docs/spec');
const customDocsDir = path.resolve('./docs/spec2');
const ignoreFiles = ['index.md', 'contributor.md', 'toc.md', 'overview.md', 'contributing.md'];
const ignoreDirs = ['node_modules', '.vitepress', '.git'];
const mdFiles = [];
function moveCustomFiles(srcDir, destDir) {
if (!fs.existsSync(srcDir)) return;
const entries = fs.readdirSync(srcDir, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(srcDir, entry.name);
const destPath = path.join(destDir, entry.name);
if (entry.isDirectory()) {
fs.mkdirSync(destPath, { recursive: true });
moveCustomFiles(srcPath, destPath);
} else if (entry.name.endsWith('.md')) {
fs.copyFileSync(srcPath, destPath);
}
}
}
function extractTitle(filePath) {
try {
const content = fs.readFileSync(filePath, 'utf8');
const lines = content.split('\n');
for (const line of lines) {
const trimmed = line.trim();
if (trimmed.startsWith('# ')) {
return trimmed.substring(2).trim();
}
}
return path.basename(filePath, '.md');
} catch (error) {
console.warn(`Error reading the file ${filePath}:`, error.message);
return path.basename(filePath, '.md');
}
}
function scan(dir) {
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
if (entry.isDirectory()) {
if (!ignoreDirs.includes(entry.name)) {
scan(path.join(dir, entry.name));
}
} else if (
entry.name.endsWith('.md') &&
!ignoreFiles.includes(entry.name)
) {
const fullPath = path.join(dir, entry.name);
const originalName = path.relative(docsDir, fullPath).replace(/\\/g, '/');
let relativePath = originalName
.replace(/(^|\/)[^/]*?[_-](?=[^/]+\.md$)/g, '$1')
.replace(/_/g, '-')
.toLowerCase();
let title = extractTitle(fullPath);
if (relativePath.endsWith('readme.md')) {
title = 'Introduction';
}
mdFiles.push({
originalName,
path: relativePath,
title: title
});
}
}
}
moveCustomFiles(customDocsDir, docsDir);
scan(docsDir);
mdFiles.sort((a, b) => a.originalName.localeCompare(b.originalName));
const readmeIndex = mdFiles.findIndex(file =>
file.originalName.toLowerCase().endsWith('readme.md')
);
let readmeEntry;
if (readmeIndex !== -1) {
readmeEntry = mdFiles.splice(readmeIndex, 1)[0];
}
if (readmeEntry) {
mdFiles.unshift(readmeEntry);
}
fs.writeFileSync(
path.join(docsDir, 'toc.json'),
JSON.stringify(mdFiles, null, 2)
);
console.log(`TOC generated with ${mdFiles.length} entries.`);
console.log('Sample entries:');
mdFiles.slice(0, 3).forEach(file => {
console.log(` ${file.path} -> "${file.title}"`);
});