···
1
1
import fs from "node:fs";
2
2
+
import path from "node:path";
2
3
3
4
const CONFIG_PATH = "tdocs.config.json";
4
5
···
52
53
53
54
console.log("\n🔍 Result:\n" + fs.readFileSync(CONFIG_PATH, "utf8"));
54
55
55
55
-
// rename all .md files to .mdx in root folder and subfolders recursively
56
56
-
const mdFiles = fs.readdirSync(".", { withFileTypes: true, recursive: true })
57
57
-
.filter(dirent => dirent.isFile() && dirent.name.endsWith(".md"))
58
58
-
.map(dirent => dirent.name);
56
56
+
function renameMdToMdx(dir = process.cwd()) {
57
57
+
// Read all items in the current directory
58
58
+
const items = fs.readdirSync(dir, { withFileTypes: true });
59
59
60
60
-
for (const mdFile of mdFiles) {
61
61
-
fs.renameSync(mdFile, mdFile.replace(".md", ".mdx"));
62
62
-
}
60
60
+
for (const item of items) {
61
61
+
const fullPath = path.join(dir, item.name);
62
62
+
63
63
+
if (item.isDirectory()) {
64
64
+
// Recurse into subdirectories
65
65
+
renameMdToMdx(fullPath);
66
66
+
} else if (item.isFile() && path.extname(item.name) === ".md") {
67
67
+
const newName = path.basename(item.name, ".md") + ".mdx";
68
68
+
const newPath = path.join(dir, newName);
69
69
+
70
70
+
fs.renameSync(fullPath, newPath);
71
71
+
console.log(`Renamed: ${fullPath} → ${newPath}`);
72
72
+
}
73
73
+
}
74
74
+
}
75
75
+
76
76
+
// Run the function
77
77
+
renameMdToMdx();