aboutsummaryrefslogtreecommitdiff
path: root/save-index.js
blob: 3f304ff331f28aff0597bde231197126e738a143 (plain) (blame)
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
const ejs = require("ejs");
const fs = require("fs");

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 || "Blog | Fuwn",
          description: "fuwn, a naritive through my struggles",
          body: indexBodyHTML,
        });

        fs.writeFile(outputFile, indexHTML, err => {
          if (err) { return reject(err); }

          resolve("ok!");
        });
      });
    });
  });
}

module.exports = saveIndex;