-
Notifications
You must be signed in to change notification settings - Fork 5
/
save-article.js
41 lines (35 loc) · 1.01 KB
/
save-article.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
const ejs = require("ejs")
const fs = require("fs")
const mkdirp = require("mkdirp")
const path = require("path")
function saveArticle(article) {
fs.readFile("templates/article.ejs", (err, articleTemplate) => {
if (err) {
return reject(err)
}
fs.readFile("templates/layout.ejs", (err, layoutTemplate) => {
if (err) {
return reject(err)
}
const articleBodyHTML = ejs.render(articleTemplate.toString(), article)
const articleHTML = ejs.render(layoutTemplate.toString(), {
title: article.title + " | jordan scales",
body: articleBodyHTML,
description: article.description || article.summary,
})
const articlePath = path.join("output", article.route)
return mkdirp(articlePath).then((_) => {
fs.writeFile(
path.join(articlePath, "index.html"),
articleHTML,
(err) => {
if (err) {
throw err
}
}
)
})
})
})
}
module.exports = saveArticle