forked from PalisadoesFoundation/talawa-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-readme-links.js
More file actions
54 lines (49 loc) · 1.37 KB
/
Copy pathfix-readme-links.js
File metadata and controls
54 lines (49 loc) · 1.37 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
import fs from "node:fs";
import path from "node:path";
const docsDir = path.resolve(
process.env.DOCS_DIR || "./docs/docs/docs/auto-schema",
);
const debug = process.env.DEBUG === "true";
//test
function replaceLinks(dir) {
try {
const files = fs.readdirSync(dir);
if (!Array.isArray(files)) {
console.error("Please enter a valid directory path");
return;
}
if (debug) {
console.log(
JSON.stringify({
level: "info",
message: "Processing directory",
directory: dir,
}),
);
}
for (const file of files) {
const filePath = path.join(dir, file);
const stats = fs.lstatSync(filePath);
if (stats.isDirectory() && !stats.isSymbolicLink()) {
replaceLinks(filePath);
} else if (file.endsWith(".md")) {
let content = fs.readFileSync(filePath, "utf8");
console.log(`Processing file: ${filePath}`);
// Replace any README.md links with root directory ("/")
content = content.replace(
/\[([^\]]+)\]\((?:\.\.\/|\/)*README\.md(?:#[^\)]+)?\)/gi,
(match, linkText) => {
console.log(`Replacing README.md link in ${filePath}`);
return "[Admin Docs](/)"; // Preserve original link text if needed
},
);
fs.writeFileSync(filePath, content, "utf8");
}
}
} catch (err) {
console.error(`Error processing directory ${dir}:`, err);
throw err;
}
}
replaceLinks(docsDir);
export default replaceLinks;