-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpathfinder.js
executable file
·78 lines (65 loc) · 2.1 KB
/
pathfinder.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* This function is to be called with 'npm run build'
* It iterates thorugh all .md files in /docs folder recursively
* and saves an array of all redirect links to ./redirects.json
* redirects.json is used during built time by Docusaurus
*/
const fs = require('fs');
const path = require('path');
async function traverseDirectory(dir) {
const files = await fs.promises.readdir(dir);
const contents = [];
for (const file of files) {
const filePath = path.join(dir, file);
const stats = await fs.promises.stat(filePath);
if (stats.isDirectory()) {
const subdirContents = await traverseDirectory(filePath);
contents.push(...subdirContents);
} else if (path.extname(file) === '.md') {
const data = await fs.promises.readFile(filePath, 'utf-8');
const lines = data.split('\n');
let inHeader = false;
let redirectFrom = '';
let slug = '';
for (const line of lines) {
if (line.startsWith('---')) {
inHeader = !inHeader;
} else if (inHeader) {
let [key, value] = line.split(':').map((s) => s.trim());
// Trim quotation marks from value
if (
value.charAt(0) === value.charAt(value.length - 1) &&
(value.charAt(0) === "'" || value.charAt(0) === '"')
) {
value = value.slice(1, -1); // Remove first and last character
}
if (key === 'redirect_from') {
redirectFrom = value;
} else if (key === 'slug') {
slug = value;
}
} else {
break;
}
}
const content = {
from: redirectFrom,
to: slug,
};
if (redirectFrom) {
contents.push(content);
}
}
}
return contents;
}
async function writeContentsToFile(contents, filePath) {
const data = JSON.stringify(contents, null, 2);
await fs.promises.writeFile(filePath, data);
console.log(`Wrote contents to file: ${filePath}`);
}
async function main() {
const contents = await traverseDirectory('./docs');
await writeContentsToFile(contents, './redirects.json');
}
main();