-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrename-doc-filenames.js
More file actions
294 lines (236 loc) · 8.17 KB
/
rename-doc-filenames.js
File metadata and controls
294 lines (236 loc) · 8.17 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
#!/usr/bin/env node
/**
* Rename Docusaurus docs files from snake_case to kebab-case (only non-API docs),
* and update references across markdown, sidebars.js, and redirects.conf.
*
* Key fixes vs the draft:
* - Mapping is by doc-relative path (e.g. "guides/auto_scheduling.md"), not by basename,
* so "guides/auto_scheduling.md" won't affect "api/config/auto_scheduling.md".
* - Nothing under any "/api/" path segment is renamed or rewritten.
* - Updates Docusaurus ids in sidebars (no extension) and URL targets in redirects.conf.
*/
const fs = require("fs");
const path = require("path");
const repoRoot = process.cwd();
const TEXT_EXTENSIONS = new Set([
".md",
".mdx",
".js",
".ts",
".tsx",
".json",
".yml",
".yaml",
".conf",
]);
/**
* Where references may appear.
* Add/remove roots as needed (script will skip missing paths).
*/
const REFERENCE_TARGETS = [
"docs",
"i18n",
"sidebars.js",
"docusaurus.config.js",
path.join("docker", "redirects.conf"),
];
/**
* Content roots to rename:
* - docs/
* - i18n/*docusaurus-plugin-content-docs/** (current + versioned)
*/
function getDocsContentRoots() {
const roots = [];
const docsRoot = path.join(repoRoot, "docs");
if (fs.existsSync(docsRoot) && fs.statSync(docsRoot).isDirectory()) {
roots.push(docsRoot);
}
const i18nRoot = path.join(repoRoot, "i18n");
if (!fs.existsSync(i18nRoot) || !fs.statSync(i18nRoot).isDirectory()) {
return roots;
}
const locales = fs.readdirSync(i18nRoot, { withFileTypes: true }).filter((d) => d.isDirectory());
for (const localeDir of locales) {
const pluginDocsDir = path.join(i18nRoot, localeDir.name, "docusaurus-plugin-content-docs");
if (!fs.existsSync(pluginDocsDir) || !fs.statSync(pluginDocsDir).isDirectory()) {
continue;
}
// "current" and "version-*" are the common ones; we'll include any directory under it.
const versions = fs
.readdirSync(pluginDocsDir, { withFileTypes: true })
.filter((d) => d.isDirectory())
.map((d) => path.join(pluginDocsDir, d.name));
roots.push(...versions);
}
return roots;
}
function walkDirectory(rootDir) {
const results = [];
const stack = [rootDir];
while (stack.length > 0) {
const currentDir = stack.pop();
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(currentDir, entry.name);
if (entry.isDirectory()) {
stack.push(fullPath);
} else {
results.push(fullPath);
}
}
}
return results;
}
function toPosixPath(p) {
return p.split(path.sep).join("/");
}
function hasApiSegment(posixRelativePath) {
// true if path contains "/api/" or starts with "api/"
return posixRelativePath === "api" || posixRelativePath.startsWith("api/") || posixRelativePath.includes("/api/");
}
function getDocRelPath(contentRootAbs, fileAbsPath) {
// relative path inside docs root (posix)
const rel = path.relative(contentRootAbs, fileAbsPath);
return toPosixPath(rel);
}
function computeRenameForFile(fileAbsPath) {
const dirName = path.dirname(fileAbsPath);
const baseName = path.basename(fileAbsPath); // e.g. auto_scheduling.md
const ext = path.extname(baseName);
const stem = baseName.slice(0, baseName.length - ext.length);
const newStem = stem.replace(/_/g, "-");
if (newStem === stem) {
return null;
}
const newBaseName = newStem + ext;
const newAbsPath = path.join(dirName, newBaseName);
return { newAbsPath, newBaseName };
}
/**
* Builds a replacement mapping keyed by doc-relative paths:
* - "guides/auto_scheduling.md" -> "guides/auto-scheduling.md"
* plus derived ids/urls without extension.
*/
function renameMarkdownFilesAndBuildMapping() {
const contentRoots = getDocsContentRoots();
const mapping = [];
for (const contentRootAbs of contentRoots) {
const allFiles = walkDirectory(contentRootAbs);
for (const fileAbsPath of allFiles) {
const ext = path.extname(fileAbsPath);
if (ext !== ".md" && ext !== ".mdx") {
continue;
}
const docRelPath = getDocRelPath(contentRootAbs, fileAbsPath);
// Never touch anything under /api/
if (hasApiSegment(docRelPath)) {
continue;
}
const renameInfo = computeRenameForFile(fileAbsPath);
if (!renameInfo) {
continue;
}
const oldDocRelPath = docRelPath; // e.g. guides/auto_scheduling.md
const newDocRelPath = toPosixPath(path.relative(contentRootAbs, renameInfo.newAbsPath));
// Rename on disk
console.log(`RENAME: ${toPosixPath(path.relative(repoRoot, fileAbsPath))} -> ${toPosixPath(path.relative(repoRoot, renameInfo.newAbsPath))}`);
fs.renameSync(fileAbsPath, renameInfo.newAbsPath);
// Also store id variants without extension (Docusaurus ids)
const oldId = oldDocRelPath.replace(/\.(md|mdx)$/i, "");
const newId = newDocRelPath.replace(/\.(md|mdx)$/i, "");
mapping.push({
oldDocRelPath,
newDocRelPath,
oldId,
newId,
});
}
}
// Sort by length (desc) to avoid partial collisions
mapping.sort((a, b) => b.oldDocRelPath.length - a.oldDocRelPath.length);
return mapping;
}
function shouldProcessAsTextFile(filePathAbs) {
const ext = path.extname(filePathAbs);
if (!TEXT_EXTENSIONS.has(ext)) {
return false;
}
return true;
}
function getAllReferenceFiles() {
const files = [];
for (const target of REFERENCE_TARGETS) {
const abs = path.join(repoRoot, target);
if (!fs.existsSync(abs)) {
continue;
}
const stat = fs.statSync(abs);
if (stat.isFile()) {
files.push(abs);
continue;
}
if (stat.isDirectory()) {
files.push(...walkDirectory(abs));
}
}
return Array.from(new Set(files));
}
function applyReplacementsToContent(originalContent, mapping) {
let updated = originalContent;
for (const entry of mapping) {
// 1) Replace doc-relative paths with extension (works for "../guides/auto_scheduling.md" too)
updated = updated.split(entry.oldDocRelPath).join(entry.newDocRelPath);
// 2) Replace ids in sidebars/config (no extension)
// Example: "integrations/react/quick_start_wrapper" -> ".../quick-start-wrapper"
updated = updated.split(entry.oldId).join(entry.newId);
// 3) Replace URL-ish forms used in redirects.conf: "/something/<id>/".
// We handle both with and without trailing slash.
updated = updated.split(`${entry.oldId}/`).join(`${entry.newId}/`);
updated = updated.split(`/${entry.oldId}/`).join(`/${entry.newId}/`);
updated = updated.split(`/${entry.oldId}"`).join(`/${entry.newId}"`);
updated = updated.split(`/${entry.oldId}'`).join(`/${entry.newId}'`);
}
return updated;
}
function updateReferences(mapping) {
if (mapping.length === 0) {
console.log("No files renamed. Skipping reference updates.");
return;
}
const allRefFiles = getAllReferenceFiles();
const visited = new Set();
for (const fileAbsPath of allRefFiles) {
if (visited.has(fileAbsPath)) {
continue;
}
visited.add(fileAbsPath);
if (!shouldProcessAsTextFile(fileAbsPath)) {
continue;
}
const rel = toPosixPath(path.relative(repoRoot, fileAbsPath));
const content = fs.readFileSync(fileAbsPath, "utf8");
// Important: we only generate mapping from non-api renamed pages,
// so API references should remain unchanged automatically.
const updated = applyReplacementsToContent(content, mapping);
if (updated !== content) {
console.log(`UPDATE: ${rel}`);
fs.writeFileSync(fileAbsPath, updated, "utf8");
}
}
}
function main() {
console.log("=== Step 1: Rename docs files (snake_case -> kebab-case), excluding /api/ ===");
const mapping = renameMarkdownFilesAndBuildMapping();
if (mapping.length === 0) {
console.log("No matching files found to rename. Done.");
return;
}
console.log("\nRenamed entries:");
for (const entry of mapping) {
console.log(` ${entry.oldDocRelPath} -> ${entry.newDocRelPath}`);
}
console.log("\n=== Step 2: Update references in md/mdx, sidebars.js, redirects.conf ===");
updateReferences(mapping);
console.log("\nAll done. Run a Docusaurus build to verify link integrity.");
}
main();