forked from kevinamiri/maila
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrx.js
More file actions
57 lines (40 loc) · 1.86 KB
/
rx.js
File metadata and controls
57 lines (40 loc) · 1.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
const fs = require('fs');
const path = require('path');
function getLanguageCode(content) {
const langRegex = /lang:\s*(\w+)/;
const match = content.match(langRegex);
return match ? match[1] : null;
}
function fixFrontMatterFormatting(content) {
return content.replace(/(placeholder|title):\s*"(.*?)"/g, (match, key, value) => {
return `${key}: "${value.replace(/"/g, '\'')}"`;
});
}
function updateMdFile(filePath, langCode) {
const content = fs.readFileSync(filePath, 'utf-8');
// Fix front matter formatting
const fixedContent = fixFrontMatterFormatting(content);
// Update the slug and path
const newContent = fixedContent.replace(/(slug|path):\s*\/\w+\/(.+)/g, `$1: /${langCode}/$2`);
fs.writeFileSync(filePath, newContent);
}
function findAndUpdateMdFiles(dirPath) {
const files = fs.readdirSync(dirPath);
for (const file of files) {
const currentPath = path.join(dirPath, file);
if (fs.statSync(currentPath).isDirectory()) {
findAndUpdateMdFiles(currentPath);
} else if (path.extname(currentPath) === '.md') {
const content = fs.readFileSync(currentPath, 'utf-8');
const langCode = getLanguageCode(content);
if (langCode) {
updateMdFile(currentPath, langCode);
}
}
}
}
findAndUpdateMdFiles('src/pages/tools');
/*
First, the `getLanguageCode` function extracts the language code from the file content using a regular expression. The `updateMdFile` function then updates the file's `slug` and `path` values with the language code. Finally, the `findAndUpdateMdFiles` function recursively searches through the provided directory and its subdirectories, updating all `.md` files it encounters.
To use the function, call `findAndUpdateMdFiles('/your/path/here');`, replacing `/your/path/here` with the desired path.
*/