-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfix_links.js
More file actions
122 lines (107 loc) · 2.86 KB
/
fix_links.js
File metadata and controls
122 lines (107 loc) · 2.86 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
#!/usr/bin/env node
/**
* Fixes broken links in Spanish documentation.
*
* Issues fixed:
* 1. Links like `/cluster/*` → `/docs/cluster/*` (add /docs prefix)
* 2. Links ending in `/index` or `/index.mdx` → remove index
* 3. Links with `.mdx` extension → remove extension
* 4. Relative links like `./extra_templates` in wrong context
*
* Usage: node fix_links.js
*/
const fs = require("fs");
const path = require("path");
const ROOT = __dirname;
const DIRS_TO_PROCESS = [
path.join(ROOT, "content/docs/es"),
path.join(ROOT, "content/tutorials/es"),
path.join(ROOT, "content/tutorials/en"),
];
// Patterns to fix in order
const LINK_PATTERNS = [
// 1. Add /docs prefix to bare section links (for tutorials that still have old format)
{
regex:
/\]\(\/(cluster|project|provider|environment|domain|user|network|cli|connect_to_git|quickstart)([^)]*)\)/g,
replacement: "](/docs/$1$2)",
},
// 2. Remove /index.mdx from links
{
regex: /\/index\.mdx\)/g,
replacement: ")",
},
// 3. Remove /index from links (but keep the path)
{
regex: /\/index\)/g,
replacement: ")",
},
// 4. Remove .mdx extension from links
{
regex: /\.mdx\)/g,
replacement: ")",
},
// 5. Remove .mdx# pattern (for anchors)
{
regex: /\.mdx#/g,
replacement: "#",
},
];
let totalFilesProcessed = 0;
let totalLinksFixed = 0;
/**
* Process a single file
*/
function processFile(filePath) {
let content = fs.readFileSync(filePath, "utf-8");
let originalContent = content;
let linksFixed = 0;
for (const pattern of LINK_PATTERNS) {
const matches = content.match(pattern.regex);
if (matches) {
linksFixed += matches.length;
}
content = content.replace(pattern.regex, pattern.replacement);
}
if (content !== originalContent) {
fs.writeFileSync(filePath, content);
console.log(
` ✓ ${path.relative(ROOT, filePath)}: ${linksFixed} links fixed`
);
totalLinksFixed += linksFixed;
return true;
}
return false;
}
/**
* Process all files in a directory recursively
*/
function processDirectory(dir) {
if (!fs.existsSync(dir)) {
console.log(` Directory not found: ${dir}`);
return;
}
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
processDirectory(fullPath);
} else if (entry.name.endsWith(".mdx") || entry.name.endsWith(".md")) {
if (processFile(fullPath)) {
totalFilesProcessed++;
}
}
}
}
function main() {
console.log("🔗 Fixing broken links...\n");
for (const dir of DIRS_TO_PROCESS) {
console.log(`Processing: ${path.relative(ROOT, dir)}/`);
processDirectory(dir);
console.log("");
}
console.log(
`✅ Done! Fixed ${totalLinksFixed} links in ${totalFilesProcessed} files.`
);
}
main();