-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate-raw-pages.js
More file actions
47 lines (40 loc) · 1.27 KB
/
generate-raw-pages.js
File metadata and controls
47 lines (40 loc) · 1.27 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
const {
readdirSync,
statSync,
readFileSync,
writeFileSync,
rmSync,
mkdirSync,
} = require('fs');
const { dirname, join, relative } = require('path');
const PAGES_DIR = join(__dirname, '..', 'pages');
const OUTPUT_DIR = join(__dirname, '..', 'public', 'raw');
function walkDir(dir) {
const results = [];
for (const entry of readdirSync(dir)) {
const full = join(dir, entry);
if (statSync(full).isDirectory()) {
results.push(...walkDir(full));
} else if (full.endsWith('.md')) {
results.push(full);
}
}
return results;
}
function getRawOutputPath(filePath) {
const rel = relative(PAGES_DIR, filePath)
.replace(/\.md$/, '')
.replace(/\\/g, '/');
const route = rel === 'index' ? '/index' : `/${rel.replace(/\/index$/, '')}`;
const outputRel = route === '/index' ? 'index.md' : `${route.slice(1)}.md`;
return join(OUTPUT_DIR, outputRel);
}
rmSync(OUTPUT_DIR, { recursive: true, force: true });
mkdirSync(OUTPUT_DIR, { recursive: true });
const files = walkDir(PAGES_DIR);
for (const file of files) {
const outputPath = getRawOutputPath(file);
mkdirSync(dirname(outputPath), { recursive: true });
writeFileSync(outputPath, readFileSync(file, 'utf-8'));
}
console.log(`Generated raw markdown files for ${files.length} pages`);