forked from itwanger/toBeBetterJavaer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-sidebar.js
More file actions
476 lines (412 loc) · 13 KB
/
Copy pathsync-sidebar.js
File metadata and controls
476 lines (412 loc) · 13 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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const ROOT_DIR = path.resolve(__dirname, "..");
const DEFAULT_SIDEBAR = path.join(ROOT_DIR, "docs", "src", ".vuepress", "sidebar.ts");
const DEFAULT_TARGETS = [
{
route: "/sidebar/itwanger/ai/",
dir: "docs/src/sidebar/itwanger/ai",
fallbackGroup: "其他",
},
];
function printHelp() {
console.log(`Usage:
npm run sidebar:check
npm run sidebar:sync
node scripts/sync-sidebar.js [options]
Options:
--write Rewrite sidebar.ts. Default is dry-run only.
--check Exit with code 1 if sidebar.ts is out of sync.
--sidebar <path> Sidebar file. Default: docs/src/.vuepress/sidebar.ts
--route <route> Route to sync, for example /sidebar/itwanger/ai/
--dir <path> Markdown directory for --route.
--fallback <text> Group text for new files. Default: 其他
--verbose Print unchanged targets too.
--help Show this help.
Without --route/--dir, the script syncs the built-in targets:
${DEFAULT_TARGETS.map((target) => ` - ${target.route} <= ${target.dir}`).join("\n")}
`);
}
function parseArgs(argv) {
const options = {
write: false,
check: false,
sidebar: DEFAULT_SIDEBAR,
route: null,
dir: null,
fallbackGroup: "其他",
verbose: false,
help: false,
};
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
if (arg === "--help" || arg === "-h") {
options.help = true;
} else if (arg === "--write") {
options.write = true;
} else if (arg === "--check") {
options.check = true;
} else if (arg === "--verbose") {
options.verbose = true;
} else if (arg === "--sidebar") {
options.sidebar = path.resolve(ROOT_DIR, requireValue(argv, index));
index += 1;
} else if (arg.startsWith("--sidebar=")) {
options.sidebar = path.resolve(ROOT_DIR, arg.slice("--sidebar=".length));
} else if (arg === "--route") {
options.route = normalizeRoute(requireValue(argv, index));
index += 1;
} else if (arg.startsWith("--route=")) {
options.route = normalizeRoute(arg.slice("--route=".length));
} else if (arg === "--dir") {
options.dir = requireValue(argv, index);
index += 1;
} else if (arg.startsWith("--dir=")) {
options.dir = arg.slice("--dir=".length);
} else if (arg === "--fallback") {
options.fallbackGroup = requireValue(argv, index);
index += 1;
} else if (arg.startsWith("--fallback=")) {
options.fallbackGroup = arg.slice("--fallback=".length);
} else {
throw new Error(`Unknown option: ${arg}`);
}
}
if ((options.route && !options.dir) || (!options.route && options.dir)) {
throw new Error("--route and --dir must be used together");
}
return options;
}
function requireValue(argv, index) {
const value = argv[index + 1];
if (!value || value.startsWith("-")) {
throw new Error(`Missing value for ${argv[index]}`);
}
return value;
}
function normalizeRoute(route) {
if (!route.startsWith("/")) {
route = `/${route}`;
}
if (!route.endsWith("/")) {
route = `${route}/`;
}
return route;
}
function createTargets(options) {
if (options.route) {
return [
{
route: options.route,
dir: options.dir,
fallbackGroup: options.fallbackGroup,
},
];
}
return DEFAULT_TARGETS;
}
function main() {
try {
const options = parseArgs(process.argv.slice(2));
if (options.help) {
printHelp();
return;
}
if (!fs.existsSync(options.sidebar)) {
throw new Error(`Sidebar file not found: ${options.sidebar}`);
}
let source = fs.readFileSync(options.sidebar, "utf8");
const originalSource = source;
const reports = [];
for (const target of createTargets(options)) {
const result = syncTarget(source, target);
source = result.source;
reports.push(result.report);
}
const changed = source !== originalSource;
for (const report of reports) {
if (options.verbose || report.added.length > 0 || report.removed.length > 0 || report.warnings.length > 0) {
printReport(report);
}
}
if (options.write && changed) {
fs.writeFileSync(options.sidebar, source);
console.log(`Updated ${path.relative(ROOT_DIR, options.sidebar)}`);
} else if (!changed) {
console.log("Sidebar is already in sync.");
} else {
console.log("Dry run only. Re-run with --write to update sidebar.ts.");
}
if (options.check && changed) {
process.exitCode = 1;
}
} catch (error) {
console.error(`sync-sidebar: ${error.message}`);
process.exitCode = 1;
}
}
function syncTarget(source, target) {
const dir = path.resolve(ROOT_DIR, target.dir);
const report = {
route: target.route,
dir: path.relative(ROOT_DIR, dir),
added: [],
removed: [],
warnings: [],
};
if (!fs.existsSync(dir)) {
throw new Error(`Markdown directory not found: ${target.dir}`);
}
const files = collectMarkdownSlugs(dir);
const blockRange = findRouteArray(source, target.route);
if (!blockRange) {
throw new Error(`Route not found in sidebar.ts: ${target.route}`);
}
let block = source.slice(blockRange.start, blockRange.end + 1);
const refs = collectRefs(block);
const listedSlugs = new Set(refs.map((ref) => ref.slug));
const fileSlugs = new Set(files.map((file) => file.slug));
const staleSlugs = [...listedSlugs].filter((slug) => !fileSlugs.has(slug));
if (staleSlugs.length > 0) {
const removal = removeStaleStringEntries(block, staleSlugs);
block = removal.block;
report.removed = removal.removed;
for (const slug of staleSlugs) {
if (!removal.removed.includes(slug)) {
report.warnings.push(`Stale ref "${slug}" is not a plain string entry; remove it manually if needed.`);
}
}
}
const refsAfterRemoval = collectRefs(block);
const listedAfterRemoval = new Set(refsAfterRemoval.map((ref) => ref.slug));
const missingFiles = files.filter((file) => !listedAfterRemoval.has(file.slug));
if (missingFiles.length > 0) {
const insertion = appendMissingEntries(block, missingFiles, target.fallbackGroup);
block = insertion.block;
report.added = insertion.added;
report.warnings.push(...insertion.warnings);
}
const nextSource = source.slice(0, blockRange.start) + block + source.slice(blockRange.end + 1);
return { source: nextSource, report };
}
function collectMarkdownSlugs(dir) {
return fs
.readdirSync(dir, { withFileTypes: true })
.filter((entry) => entry.isFile() && entry.name.endsWith(".md"))
.map((entry) => {
const basename = entry.name.slice(0, -".md".length);
return {
slug: basename,
entry: basename === "readme" ? "readme.md" : basename,
};
})
.filter((file) => file.slug.length > 0)
.sort((a, b) => a.slug.localeCompare(b.slug, "en"));
}
function collectRefs(block) {
const refs = [];
const lines = block.split(/\r?\n/);
for (const line of lines) {
const simpleEntry = line.match(/^\s*["']([^"']+)["'],?\s*(?:\/\/.*)?$/);
if (simpleEntry) {
const slug = normalizeRef(simpleEntry[1]);
if (slug) {
refs.push({ slug, value: simpleEntry[1], kind: "entry" });
}
continue;
}
const linkEntry = line.match(/\blink\s*:\s*["']([^"']+)["']/);
if (linkEntry) {
const slug = normalizeRef(linkEntry[1]);
if (slug) {
refs.push({ slug, value: linkEntry[1], kind: "link" });
}
}
}
return refs;
}
function normalizeRef(value) {
if (
!value ||
value.startsWith("/") ||
value.startsWith("#") ||
/^[a-z]+:\/\//i.test(value) ||
value.endsWith("/") ||
value.includes("/")
) {
return null;
}
return value.endsWith(".md") ? value.slice(0, -".md".length) : value;
}
function removeStaleStringEntries(block, staleSlugs) {
const stale = new Set(staleSlugs);
const removed = [];
const lines = block.split(/\r?\n/);
const nextLines = [];
for (const line of lines) {
const simpleEntry = line.match(/^\s*["']([^"']+)["'],?\s*(?:\/\/.*)?$/);
const slug = simpleEntry ? normalizeRef(simpleEntry[1]) : null;
if (slug && stale.has(slug)) {
removed.push(slug);
continue;
}
nextLines.push(line);
}
return {
block: nextLines.join("\n"),
removed,
};
}
function appendMissingEntries(block, missingFiles, fallbackGroup) {
const warnings = [];
const entries = missingFiles.map((file) => file.entry);
const fallback = fallbackGroup ? findFallbackChildren(block, fallbackGroup) : null;
if (fallback) {
const childrenBlock = block.slice(fallback.start, fallback.end + 1);
const indent = detectChildIndent(childrenBlock, " ");
const insertion = entries.map((entry) => `${indent}"${entry}",`).join("\n");
const insertAt = findLineStart(block, fallback.end);
const prefix = block.slice(0, insertAt);
const suffix = block.slice(insertAt);
const separator = prefix.endsWith("\n") ? "" : "\n";
return {
block: `${prefix}${separator}${insertion}\n${suffix}`,
added: entries,
warnings,
};
}
warnings.push(`Fallback group "${fallbackGroup}" not found; appended new refs at route root.`);
const indent = detectChildIndent(block, " ");
const insertAt = findLineStart(block, block.length - 1);
const insertion = entries.map((entry) => `${indent}"${entry}",`).join("\n");
const prefix = block.slice(0, insertAt);
const suffix = block.slice(insertAt);
const separator = prefix.endsWith("\n") ? "" : "\n";
return {
block: `${prefix}${separator}${insertion}\n${suffix}`,
added: entries,
warnings,
};
}
function detectChildIndent(block, fallback) {
const lines = block.split(/\r?\n/).reverse();
for (const line of lines) {
const match = line.match(/^(\s*)["'][^"']+["'],?\s*$/);
if (match) {
return match[1];
}
}
return fallback;
}
function findFallbackChildren(block, fallbackGroup) {
const textPattern = new RegExp(`\\btext\\s*:\\s*["']${escapeRegExp(fallbackGroup)}["']`, "g");
let textMatch;
while ((textMatch = textPattern.exec(block)) !== null) {
const childrenPattern = /\bchildren\s*:\s*\[/g;
childrenPattern.lastIndex = textMatch.index + textMatch[0].length;
const childrenMatch = childrenPattern.exec(block);
if (!childrenMatch) {
continue;
}
const arrayStart = childrenMatch.index + childrenMatch[0].lastIndexOf("[");
const arrayEnd = findMatchingBracket(block, arrayStart);
if (arrayEnd !== -1) {
return { start: arrayStart, end: arrayEnd };
}
}
return null;
}
function findLineStart(source, index) {
const lineBreak = source.lastIndexOf("\n", index);
return lineBreak === -1 ? 0 : lineBreak + 1;
}
function findRouteArray(source, route) {
const routePattern = new RegExp(`(["'])${escapeRegExp(route)}\\1\\s*:\\s*\\[`);
const match = routePattern.exec(source);
if (!match) {
return null;
}
const arrayStart = match.index + match[0].lastIndexOf("[");
const arrayEnd = findMatchingBracket(source, arrayStart);
if (arrayEnd === -1) {
throw new Error(`Could not parse route array: ${route}`);
}
return { start: arrayStart, end: arrayEnd };
}
function findMatchingBracket(source, openIndex) {
let depth = 0;
let quote = null;
let escaped = false;
let lineComment = false;
let blockComment = false;
for (let index = openIndex; index < source.length; index += 1) {
const char = source[index];
const next = source[index + 1];
if (lineComment) {
if (char === "\n") {
lineComment = false;
}
continue;
}
if (blockComment) {
if (char === "*" && next === "/") {
blockComment = false;
index += 1;
}
continue;
}
if (quote) {
if (escaped) {
escaped = false;
} else if (char === "\\") {
escaped = true;
} else if (char === quote) {
quote = null;
}
continue;
}
if (char === "/" && next === "/") {
lineComment = true;
index += 1;
continue;
}
if (char === "/" && next === "*") {
blockComment = true;
index += 1;
continue;
}
if (char === '"' || char === "'" || char === "`") {
quote = char;
continue;
}
if (char === "[") {
depth += 1;
} else if (char === "]") {
depth -= 1;
if (depth === 0) {
return index;
}
}
}
return -1;
}
function printReport(report) {
console.log(`${report.route} <= ${report.dir}`);
if (report.removed.length > 0) {
console.log(` removed: ${report.removed.join(", ")}`);
}
if (report.added.length > 0) {
console.log(` added: ${report.added.join(", ")}`);
}
for (const warning of report.warnings) {
console.log(` warning: ${warning}`);
}
if (report.removed.length === 0 && report.added.length === 0 && report.warnings.length === 0) {
console.log(" no changes");
}
}
function escapeRegExp(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
main();