-
Notifications
You must be signed in to change notification settings - Fork 5
/
save-index.js
42 lines (35 loc) · 1.01 KB
/
save-index.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
const ejs = require("ejs")
const fs = require("fs")
const constants = require("./constants.json")
function saveIndex(articles, outputFile, title, banner) {
articles.sort((a, b) => {
return (b.rawDate || 0) - (a.rawDate || 0)
})
return new Promise((resolve, reject) => {
fs.readFile("templates/index.ejs", (err, data) => {
if (err) {
return reject(err)
}
const indexBodyHTML = ejs.render(data.toString(), {
articles: articles.filter((article) => {
return !article.hidden
}),
banner,
})
fs.readFile("templates/layout.ejs", (err, data) => {
const indexHTML = ejs.render(data.toString(), {
title: title || constants.defaultTitle,
description: constants.defaultDescription,
body: indexBodyHTML,
})
fs.writeFile(outputFile, indexHTML, (err) => {
if (err) {
return reject(err)
}
resolve("ok!")
})
})
})
})
}
module.exports = saveIndex