-
Notifications
You must be signed in to change notification settings - Fork 6
/
rx.js
55 lines (47 loc) · 1.93 KB
/
rx.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
44
45
46
47
48
49
50
51
52
53
54
55
const fs = require('fs').promises;
const path = require('path');
// Get language code from content
const getLangCode = content => {
const match = content.match(/lang:\s*(\w+)/);
return match ? match[1] : null;
};
// Fix front matter formatting
const fixFormatting = content => content.replace(/(placeholder|title):\s*"(.*?)"/g, (match, key, value) => {
const newValue = value.replace(/"/g, '\'').replace(/\n/g, '\\n');
return `${key}: "${newValue}"`;
});
// Update Markdown file with language code
const updateMdFile = async (filePath, langCode) => {
try {
const content = await fs.readFile(filePath, 'utf-8');
const fixedContent = fixFormatting(content);
const newContent = fixedContent.replace(/(slug|path):\s*\/\w+\/(.+)/g, `$1: /${langCode}/$2`);
await fs.writeFile(filePath, newContent);
console.log(`Updated: ${filePath}`);
} catch (error) {
console.error(`Failed to update ${filePath}: ${error.message}`);
}
};
// Recursively find and update Markdown files
const findAndUpdateMdFiles = async dirPath => {
try {
const files = await fs.readdir(dirPath);
await Promise.all(files.map(async file => {
const currentPath = path.join(dirPath, file);
const stats = await fs.stat(currentPath);
if (stats.isDirectory()) {
await findAndUpdateMdFiles(currentPath);
} else if (path.extname(currentPath) === '.md') {
const content = await fs.readFile(currentPath, 'utf-8');
const langCode = getLangCode(content);
if (langCode) {
await updateMdFile(currentPath, langCode);
}
}
}));
} catch (error) {
console.error(`Failed to process directory ${dirPath}: ${error.message}`);
}
};
// Example usage: findAndUpdateMdFiles('src/pages/tools');
findAndUpdateMdFiles('src/pages/tools');