-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.js
180 lines (142 loc) · 4.45 KB
/
generate.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const fs = require("fs").promises;
const path = require("path");
const ejs = require("ejs");
const yaml = require("yaml");
const { newPathCreate, getNewPath, getFiles, OUTPUT_DIR } = require("./util.js");
const ops = require("./ops.js");
const genRSS = require("./rss.js");
const hljs = require('highlight.js');
const marked = require('markdown-it')({
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(str, {
language: lang
}).value;
} catch (e) {
console.log(e)
}
}
return '';
},
typographer: true,
html: true,
breaks: true
});
const md = marked.render.bind(marked);
const config = require("./config.json");
const buildPublic = async () => {
for await (const filepath of getFiles(path.join(__dirname, "public"))) {
const data = await fs.readFile(filepath, 'utf-8');
const ext = path.extname(filepath).substring(".".length);
if(Object.keys(ops).includes(ext)) {
const output = await ops[ext](data, filepath);
if (output) {
let newPath = await newPathCreate(filepath);
if(ext === "less") newPath = newPath.replace(/\.less$/, ".css");
await fs.writeFile(newPath, output);
}
} else {
const newPath = await newPathCreate(filepath);
await fs.copyFile(filepath, newPath);
}
}
await fs.copyFile(path.join(__dirname, "/node_modules/highlight.js/styles/default.css"), await getNewPath("highlight.css"));
}
// 2019-10-17-pico19-ghost-diary.md
// => 2019/10/17/pico19-ghost-diary.md
const pathToKey = file => {
let ret = path.basename(file, ".md");
for(let i = 0; i < 3; i++) ret = ret.replace("-", "/");
return ret;
}
// 2019-10-17-pico19-ghost-diary.md
// => Pico19 Ghost Diary
const pathToTitle = file => {
return path.basename(file, ".md").split("-").slice(3).map(a => a.toUpperCase()[0] + a.substring(1)).join(" ")
}
// 2019-10-17-pico19-ghost-diary.md
// => Date corresponding to 2019-10-17
const pathToDate = file => {
const ret = new Date(0);
ret.setHours(0);
const parts = path.basename(file).split("-");
ret.setYear(parts[0]);
ret.setMonth(+parts[1] - 1);
ret.setDate(parts[2]);
return ret;
}
;(async () => {
await fs.rmdir(OUTPUT_DIR, { recursive: true });
await buildPublic();
const posts = [];
for await (const file of getFiles('./posts', "md")) {
const data = (await fs.readFile(file)).toString().trim();
let content = data;
let postConfig = Object.create(null);
postConfig.title = pathToTitle(file);
postConfig.description = "";
postConfig.tags = [];
if(data.startsWith("---")) {
/*
---
<yaml>
---
<post>
*/
const configData = data.split("---")[1].trim();
const yamlData = yaml.parse(configData);
for (const key in yamlData) {
postConfig[key] = yamlData[key];
}
content = data.split("---").slice(2).join("---");
}
const summary = content.split("<!--more-->")[0];
posts.push({
content: md(content),
summary: md(summary),
config: postConfig,
path: "/blog/" + pathToKey(file),
timestamp: pathToDate(file)
});
}
posts.sort((a, b) => b.path.localeCompare(a.path));
const ejsConfig = {
gtag: config.gtag
}
const toWrite = [];
const { postsPerPage } = config.blog;
for (let i = 0; i < posts.length; i += postsPerPage) {
const data = await ejs.renderFile(path.join(__dirname, "views/blog.ejs"), {
...ejsConfig,
title: "Blog",
posts: posts.slice(i, i + postsPerPage)
});
if (i == 0) {
toWrite.push({ path: "blog.html", data });
toWrite.push({ path: "blog/index.html", data });
}
toWrite.push({ path: `blog/${i / postsPerPage}.html`, data });
}
for (const post of posts) {
const data = await ejs.renderFile(path.join(__dirname, "views/post.ejs"), {
...ejsConfig,
content: post.content,
config: post.config,
title: post.config.title,
});
toWrite.push({ path: post.path + ".html", data });
}
{
const data = await ejs.renderFile(path.join(__dirname, "views/index.ejs"), {
...ejsConfig,
config,
title: "Robert Chen",
});
toWrite.push({ path: "index.html", data });
}
await Promise.all(
toWrite.map(async ({ path, data }) => fs.writeFile(await getNewPath(path), await ops["html"](data)))
);
await fs.writeFile(await getNewPath("feed.xml"), genRSS(posts));
})();