-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_index.js
More file actions
47 lines (44 loc) · 1.43 KB
/
build_index.js
File metadata and controls
47 lines (44 loc) · 1.43 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 fs = require("fs");
const matter = require("gray-matter");
const POSTS_DIR = "posts";
const BODY_READ_LIMIT = 2048;
function estimateReadTime(summary, body) {
const combined = `${summary}\n${body}`;
const words = combined.split(/\s+/).length;
const minutes = words / 200;
const seconds = Math.round(minutes * 60);
let human;
if (seconds < 60) {
human = `${seconds} seconds`;
} else if (seconds < 3600) {
const m = Math.round(seconds / 60);
human = `${m} minute${m === 1 ? "" : "s"}`;
} else {
const h = (seconds / 3600).toFixed(1);
human = `${h} hours`;
}
return { seconds, human };
}
function main() {
const files = fs.readdirSync(POSTS_DIR).filter(f => f.endsWith(".md"));
const posts = files.map(filename => {
const fullPath = `${POSTS_DIR}/${filename}`;
const raw = fs.readFileSync(fullPath, "utf8");
const parsed = matter(raw);
const data = parsed.data;
const body = parsed.content.slice(0, BODY_READ_LIMIT);
const { seconds, human } = estimateReadTime(data.summary || "", body);
return {
slug: filename.replace(".md", ""),
post_title: data.post_title || "",
date: data.date || null,
tags: data.tags || [],
summary: data.summary || "",
read_time_seconds: seconds,
read_time_human: human
};
});
posts.sort((a, b) => new Date(b.date) - new Date(a.date));
fs.writeFileSync("index.json", JSON.stringify(posts, null, 2));
}
main();