-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneratePosts.js
31 lines (26 loc) · 1004 Bytes
/
generatePosts.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
const fs = require("fs");
const path = require("path");
const postsDir = path.join(__dirname, "posts");
const outputFile = path.join(postsDir, "posts.json");
const posts = fs.readdirSync(postsDir)
.filter(file => file.endsWith(".html"))
.map(file => {
const content = fs.readFileSync(path.join(postsDir, file), "utf-8");
// Extract JSON metadata from the comment block
const metadataMatch = content.match(/<!--\s*({[^}]+})\s*-->/);
if (metadataMatch) {
const metadata = JSON.parse(metadataMatch[1]);
return {
...metadata,
file: file
};
} else {
console.error(`No metadata found in ${file}`);
return null;
}
})
.filter(post => post !== null);
// **Add this sorting step**
posts.sort((a, b) => new Date(b.date) - new Date(a.date));
fs.writeFileSync(outputFile, JSON.stringify(posts, null, 2));
console.log("posts.json generated successfully.");