-
Notifications
You must be signed in to change notification settings - Fork 1
/
treerat.js
113 lines (98 loc) · 3.04 KB
/
treerat.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
require("dotenv").config({ override: true });
const PORT = 7533;
const express = require("express");
const app = express();
module.exports = app;
const build = require("./admin/utils/build");
const imgProc = require("./admin/utils/imgProc");
const ejs = require("ejs");
const fse = require("fs-extra")
const marked = require("marked");
marked.setOptions({ breaks: true, mangle: false, headerIds: false });
const sanitizeHtml = require("sanitize-html");
build.setup("treerat.json");
if (process.env.NODE_ENV === "development") {
console.log("Environment: Development");
app.use(express.static(app.locals.siteDir));
// Live Reload
const livereload = require("livereload");
const liveReloadServer = livereload.createServer();
const connectLivereload = require("connect-livereload");
app.use(connectLivereload());
liveReloadServer.server.once("connection", () => {
setTimeout(() => {
liveReloadServer.refresh("/");
}, 100);
});
} else {
console.log("Environment: Staging");
}
/*
* Render Middleware
*/
app.use((req, res, next) => {
res.adminRender = (file, data) => {
ejs.renderFile(process.cwd() + "/admin/views/" + file + ".ejs", { page: data, ...app.locals }, (err, html) => {
if (err) {
return res.send(`<body >${err.message.replace(/(?:\n)/g, "<br>")}</body>`);
}
res.send(html);
});
};
next();
});
app.use((req, res, next) => {
res.siteRender = (file, data) => {
let promises = []
ejs.renderFile(app.locals.viewDir + "/" + file + ".ejs", { page: data, ...app.locals, promises }, (err, html) => {
if (err) {
return res.send(`<body >${err.message.replace(/(?:\n)/g, "<br>")}</body>`);
}
Promise.all(promises).then(() => {
res.send(html);
})
});
};
next();
});
// Render page to process images without sending to client
app.use((req, res, next) => {
res.preRender = (file, data) => {
ejs.renderFile(app.locals.viewDir + "/" + file + ".ejs", { page: data, ...app.locals }, (err, html) => {
if (err) {
return res.send(`<body >${err.message.replace(/(?:\n)/g, "<br>")}</body>`);
}
});
};
next();
});
// Convert Markdown and sanitize the HTML
app.locals.md = (data) => {
return sanitizeHtml(marked.parse(data));
};
app.locals.publishImg = imgProc.publishImg;
app.locals.publishPage = (file, data) => {
ejs.renderFile(app.locals.viewDir + "/" + file + ".ejs", { page: data, ...app.locals }, (err, html) => {
if (err) {
console.error(err.message);
}
const link = (data.link != "home" ? "/"+data.link+"/" : "/")
fse.outputFile(app.locals.siteDir + link + "index.html", html)
});
};
// Log request
app.use("/", (req, res, next) => {
console.log(req.method, req.url);
next();
});
/**
* Build Site
*/
build.buildSite();
/*
/ Admin Route
*/
app.use("/admin", require("./admin/routes/admin"));
app.listen(PORT, () => {
console.log(`\n--\nTree Rat CMS Starting on port: ${PORT}\n--`);
});