This repository has been archived by the owner on Apr 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
159 lines (115 loc) · 3.46 KB
/
server.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
const sslRedirect = require("heroku-ssl-redirect");
const path = require("path")
const express = require("express")
const session = require("express-session")
const nunjucks = require("nunjucks")
const markdown = require("nunjucks-markdown")
const marked = require("marked")
const multer = require("multer")
const upload = multer({dest:'uploads/'})
const IS_HEROKU = process.env.hasOwnProperty('IS_HEROKU')
const routes = require("./app/routes")
const locals = require("./app/locals")
const port = process.env.PORT || 3000
const cookieParser = require("cookie-parser")
const bodyParser = require("body-parser")
const app = express()
module.exports = app
// enable ssl redirect
app.use(sslRedirect())
// application settings
app.set("view engine", "njk")
// set location of views
const appViews = [
path.join(__dirname, "/app/views"),
path.join(__dirname, "/app/views/layout"),
path.join(__dirname, "/app/views/partials"),
path.join(__dirname, "/app/views/macros"),
path.join(__dirname, "/node_modules/govuk_template_jinja/views/layouts")
]
const nunjucksAppEnv = nunjucks.configure(appViews, {
express: app,
autoescape: true,
watch: true,
noCache: true
})
// middleware to serve static assets
app.use("/public", express.static(path.join(__dirname, "/public")))
app.use("/public", express.static(path.join(__dirname, "/node_modules/govuk_template_jinja/assets")))
app.use("/public", express.static(path.join(__dirname, "/node_modules/govuk_frontend_toolkit")))
app.use("/public", express.static(path.join(__dirname, "/node_modules/govuk-elements-sass")))
// support for parsing data in POSTs
app.use(cookieParser())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))
// express session
app.use(session({
secret: "reform",
resave: false,
saveUninitialized: true,
cookie: {
secure: false
}
}))
// locals
app.locals = locals
// routes
app.use("/", routes)
var renderer = new marked.Renderer()
// headings
renderer.heading = function (text, level) {
var headingClass = 0;
switch(level) {
case 1:
headingClass = "heading-large";
break;
case 2:
headingClass = "heading-medium";
break;
case 3:
headingClass = "heading-small";
break;
}
var escapedText = text.toLowerCase().replace(/[^\w]+/g, "-");
return "<h" + level + " class=" + headingClass + ">" + text + "</h" + level + ">";
}
// nunjucks / HTML / examples
renderer.code = function (text, lang) {
if (lang == "nunjucks") {
return "<pre class=\"language-markup\" data-title=\"Nunjucks\"><code>" + text + "</code></pre>";
}
else if (lang == "html") {
return "<pre class=\"language-markup\" data-title=\"HTML\"><code>" + text + "</code></pre>";
}
else if (lang == "example") {
return "<div class=\"example\" data-title=\"Preview\">" + text + "</div>";
}
}
// lists
renderer.list = function (text, type) {
return "<ul class=\"list list-bullet\">" + text + "</ul>";
}
// blockquotes
renderer.blockquote = function (text) {
return "<div class=\"panel panel-border-wide text\" role=\"note\">" + text + "</div>";
}
marked.setOptions({
renderer: renderer,
gfm: true,
tables: true,
breaks: false,
pendantic: false,
sanitize: true,
smartLists: true,
smartypants: false
})
// markdown register
markdown.register(nunjucksAppEnv, marked)
// start app and listen
app.listen(port, function () {
if (!IS_HEROKU) {
console.log("Listening on port: " + port)
}
})