-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.ts
More file actions
43 lines (36 loc) · 1.38 KB
/
generator.ts
File metadata and controls
43 lines (36 loc) · 1.38 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
import fs from 'fs/promises';
async function generateIndex() {
try {
// Check if index.md exists
let existingContent = "";
try {
existingContent = await fs.readFile('./index.md', 'utf-8');
} catch (err) {
if ((err as ErrnoException).code === 'ENOENT') {
// File doesn't exist, write a default home page content
existingContent = `# Welcome to Brioche: A Bun-Based Blog Template\n\nThis is a template for creating your own blog using Bun.`;
await fs.writeFile('./index.md', existingContent);
} else {
throw err; // Re-throw other errors
}
}
// Fetch a list of all Markdown files in the 'posts' folder
const files = await fs.readdir('./posts');
// Filter out non-Markdown files
const mdFiles = files.filter(file => file.endsWith('.md'));
// Generate Markdown links
let indexContent = '\n\n## Blog Posts Index\n\n';
for (const mdFile of mdFiles) {
const postName = mdFile.replace('.md', '');
indexContent += `- [${postName}](./${postName})\n`;
}
// Concatenate existing content and new index content
const finalContent = existingContent + indexContent;
// Write back to index.md
await fs.writeFile('./posts/index.md', finalContent);
} catch (err) {
console.error(`Error generating index: ${err}`);
}
}
// Run the function to generate the index
generateIndex();