-
Notifications
You must be signed in to change notification settings - Fork 4
/
updatepath.js
43 lines (34 loc) · 1.27 KB
/
updatepath.js
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
const fs = require("fs");
const path = require("path");
// Path to the docs folder
const docsFolder = path.join(__dirname, "docs");
// Updated regular expression to find links that do not start with http, https, /, #, or mailto
const regex = /\[(.*?)\]\((?!https?:\/\/|\/|#|mailto:)(.*?)\)/g;
// Function to process each markdown file
const processFile = (filePath) => {
// Read the file content
const content = fs.readFileSync(filePath, "utf8");
// Replace the markdown links
const updatedContent = content.replace(regex, (match, text, link) => {
return `[${text}](/${link})`;
});
// Save the updated content back to the file
fs.writeFileSync(filePath, updatedContent);
console.log(`Updated file: ${filePath}`);
};
// Function to iterate over all .md and .mdx files in the docs folder
const processDocsFolder = (folderPath) => {
fs.readdirSync(folderPath).forEach((file) => {
const filePath = path.join(folderPath, file);
// Check if it's a markdown or mdx file
if (
fs.lstatSync(filePath).isFile() &&
(file.endsWith(".md") || file.endsWith(".mdx"))
) {
processFile(filePath);
}
});
};
// Process all files in the docs folder
processDocsFolder(docsFolder);
console.log("All .md and .mdx files updated in the docs folder.");