-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfix-tab.js
50 lines (43 loc) · 1.33 KB
/
fix-tab.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
const fs = require("fs");
const path = require("path");
function travel(dir, callback) {
if (dir.includes("node_modules")) {
return;
}
if (fs.statSync(dir).isFile()) {
if (dir.endsWith(".md")) {
callback(dir);
}
} else {
fs.readdirSync(dir).forEach((file) => {
var pathname = path.join(dir, file);
if (fs.statSync(pathname).isDirectory()) {
travel(pathname, callback);
} else if (pathname.endsWith(".md")) {
callback(pathname);
}
});
}
}
function fix(mdpath) {
let data = fs.readFileSync(mdpath, "utf8");
data = data
.replace(/::: tabs/g, "{% tabs %}")
.replace(/:::/g, "{% /tabs %}")
.replace(/@@@[ ]+(?<tab_name>.+)/g, '{% tab label="$<tab_name>" %}')
.replace(/@@@\s*\n/g, "{% /tab %}\n");
fs.writeFileSync(mdpath, data);
console.log(mdpath + " fixed");
}
// Example: node fix-tab.js connectors/aws-lambda-sink/2.10.0/aws-lambda-sink.md
const [dir] = process.argv.slice(2);
travel(path.join(__dirname, dir), fix);
// Usage:
// Example 1: Update a specify md file:
// node fix-tab.js connectors/aws-lambda-sink/2.10.0/aws-lambda-sink.md
// Example 2: Update files in a specify directory:
// node fix-tab.js connectors/aws-lambda-sink
// Example 3: Update all connectors docs:
// node fix-tab.js connectors
// Example 4: Update all docs:
// node fix-tab.js .