-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.js
252 lines (188 loc) · 5.87 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const express = require("express");
const fs = require("fs").promises;
const path = require("path");
const ejs = require("ejs");
const less = require("less");
const minify = require('minify');
const webp = require("webp-converter");
const autoprefixer = require('autoprefixer')
const postcss = require('postcss')
const OUTPUT_DIR = path.join(__dirname, "build");
const PUBLIC_DIR = path.join(__dirname, "public");
const hljs = require('highlight.js');
const marked = require('markdown-it')({
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(lang, str).value;
} catch (e) {
console.log(e)
}
}
return '';
},
typographer: true,
html: true,
breaks: true
});
const md = marked.render.bind(marked);
const PORT = process.env.PORT || 3000;
const app = express();
app.set("view engine", "ejs");
const staticPath = __dirname + '/public';
app.use(express.static(staticPath, { maxAge: 60 * 60 * 1000 }));
const config = require("./config.json");
const gtag = config.gtag;
app.get("/", (req, res) => {
});
const posts = new Map();
async function* getFiles(dir, extension) {
const dirents = await fs.readdir(dir, { withFileTypes: true });
for (const dirent of dirents) {
const res = path.resolve(dir, dirent.name);
if(dirent.isDirectory()) {
yield* getFiles(res);
} else {
if(extension && !dirent.name.endsWith("." + extension)) continue;
yield res;
}
}
}
const getNewPath = async relPath => {
const newPath = path.join(OUTPUT_DIR, relPath);
await fs.mkdir(path.dirname(newPath), { recursive: true });
return newPath;
}
const newPathCreate = async oldPath => {
if(!oldPath.startsWith(PUBLIC_DIR)) throw "Invalid newPath";
const newPath = oldPath.replace(PUBLIC_DIR, OUTPUT_DIR);
await fs.mkdir(path.dirname(newPath), { recursive: true });
return newPath;
}
const ops = {};
ops["png"] = async (data, filepath) => {
const newPath = await newPathCreate(filepath);
const newWebpPath = newPath.split(".").slice(0, -1).join(".") + ".webp";
await fs.copyFile(filepath, newPath);
await webp.cwebp(filepath, newWebpPath, "-q 100 -lossless");
}
ops["jpg"] = ops["png"];
ops["html"] = async (data) => {
return minify.html(data);
}
ops["js"] = async (data) => {
return minify.js(data);
}
ops["css"] = async (data, filepath) => {
const result = await postcss([ autoprefixer() ]).process(data);
if(result.css) {
return minify.css(result.css);
}
}
ops["less"] = async (data, filepath) => {
try {
const { css } = await less.render(data, { paths: [path.dirname(filepath)] });
return ops["css"](css, filepath);
} catch(e) {
console.log("skipping on less error: %s", filepath);
}
}
;(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);
}
}
})();
const pathToKey = file => {
let ret = path.basename(file, ".md");
for(let i = 0; i < 3; i++) ret = ret.replace("-", "/");
return ret;
}
;(async () => {
for await (const file of getFiles('./posts', "md")) {
const data = (await fs.readFile(file)).toString().trim();
let content = data;
let config = new Map();
// 2019-10-17-pico19-ghost-diary.md
config.set("title", path.basename(file, ".md").split("-").slice(3).map(a => a.toUpperCase()[0] + a.substring(1)).join(" "));
config.set("description", "");
if(data.startsWith("---")) {
/*
---
key: value
---
post
*/
const configData = data.split("---")[1].trim();
configData.split("\n").forEach(line => {
const [key, value] = line.split(": ");
config.set(key, value);
});
content = data.split("---").slice(2).join("---");
}
const summary = content.split("<!--more-->")[0];
posts.set(pathToKey(file), {
content,
summary,
config,
path: "/blog/" + pathToKey(file)
});
}
{
const _posts = [];
for(const [key, post] of posts.entries()) {
_posts.push({key, post});
}
_posts.sort((a, b) => b.key.localeCompare(a.key));
const data = await ejs.renderFile(path.join(__dirname, "views/blog.ejs"), {
title: "Blog",
posts: _posts.map(a => a.post),
md,
gtag
});
await fs.writeFile(await getNewPath("blog.html"), await ops["html"](data));
}
{
for (const [key, post] of posts) {
const data = await ejs.renderFile(path.join(__dirname, "views/post.ejs"), {
content: post.content,
title: post.config.get("title"),
description: post.config.get("description"),
md,
gtag
});
await fs.writeFile(await getNewPath("blog/" + key + ".html"), await ops["html"](data));
}
}
{
const data = await ejs.renderFile(path.join(__dirname, "views/index.ejs"), {
config,
title: "Robert Chen",
gtag
});
await fs.writeFile(await getNewPath("index.html"), await ops["html"](data));
}
await fs.copyFile(path.join(__dirname, "/node_modules/highlight.js/styles/default.css"), await getNewPath("highlight.css"));
})();
app.get("/blog/*", (req, res) => {
res.set('Cache-control', 'public, max-age=300');
let key = req.path.substring("/blog/".length);
if(key.endsWith("/")) key = key.slice(0, -1);
if(posts.has(key)) {
const post = posts.get(key);
} else {
res.status(404);
res.end("Not Found");
}
});