forked from loulanyue/interview-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandardize-markdown-navigation.mjs
More file actions
78 lines (63 loc) · 2.12 KB
/
Copy pathstandardize-markdown-navigation.mjs
File metadata and controls
78 lines (63 loc) · 2.12 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
import fs from "node:fs";
import path from "node:path";
import {
findNearestReadme,
findParentReadme,
navMarkerEnd,
navMarkerStart,
relativeLink,
repoRoot,
walkMarkdownFiles,
} from "./lib/markdown-maintenance.mjs";
const excludedFiles = new Set(["README.md"]);
function buildNavigationBlock(filePath) {
const lines = [navMarkerStart, "## 导航"];
const rootReadme = "README.md";
const nearestReadme = findNearestReadme(filePath);
const parentReadme = findParentReadme(filePath, nearestReadme);
const isReadme = path.basename(filePath) === "README.md";
const links = [];
if (filePath !== rootReadme) {
links.push(["返回仓库首页", relativeLink(filePath, rootReadme)]);
}
if (nearestReadme && nearestReadme !== rootReadme) {
links.push([isReadme ? "返回上一级导航" : "返回当前专题导航", relativeLink(filePath, nearestReadme)]);
}
if (parentReadme && parentReadme !== rootReadme && parentReadme !== nearestReadme) {
links.push(["返回上一级主题", relativeLink(filePath, parentReadme)]);
}
const deduped = new Set();
for (const [label, target] of links) {
const key = target;
if (deduped.has(key)) {
continue;
}
deduped.add(key);
lines.push(`- [${label}](${target})`);
}
lines.push(navMarkerEnd);
return `${lines.join("\n")}\n`;
}
function upsertNavigation(content, navBlock) {
const markerPattern = new RegExp(
`${navMarkerStart}[\\s\\S]*?${navMarkerEnd}\\n*`,
"m",
);
if (markerPattern.test(content)) {
return content.replace(markerPattern, navBlock);
}
const trimmed = content.replace(/\s+$/, "");
return `${trimmed}\n\n---\n\n${navBlock}`;
}
const markdownFiles = walkMarkdownFiles({ excludeFiles: excludedFiles });
let updatedCount = 0;
for (const filePath of markdownFiles) {
const absolutePath = path.join(repoRoot, filePath);
const original = fs.readFileSync(absolutePath, "utf8");
const next = upsertNavigation(original, buildNavigationBlock(filePath));
if (next !== original) {
fs.writeFileSync(absolutePath, next);
updatedCount += 1;
}
}
console.log(`Updated ${updatedCount} markdown files.`);