-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
154 lines (147 loc) · 5.17 KB
/
index.js
File metadata and controls
154 lines (147 loc) · 5.17 KB
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
"use strict";
process.on('uncaughtException', err => {
fs.appendFile('error.log', err.message + '\n', err => {
if (err) console.error('Failed to log error ' + err.message);
});
});
const express = require('express');
const app = express();
app.use(express.urlencoded({extended: true, limit: '15mb'}));
const mustache = require('mustache-express');
app.engine('mustache',mustache());
app.set('view engine','mustache');
app.set('views',__dirname+'/views');
const fs = require('fs');
const cache = JSON.parse(fs.readFileSync('cache.json').toString());
const revisions = JSON.parse(fs.readFileSync('revisions.json').toString());
function updateCache(articleName, html) {
cache[articleName].revisions = revisions[articleName];
cache[articleName].cachedHtml = html;
fs.writeFile('cache.json', JSON.stringify(cache), (err) => {
if(err) throw err;
});
}
function addItemToCache(item) {
cache[item] = {
revisions: -1 //means that no cache has been registered
};
fs.writeFile('cache.json',JSON.stringify(cache), (err) => {
if(err) throw err;
});
}
function updateRevisionsFile() {
fs.writeFile('revisions.json', JSON.stringify(revisions), (err) => {
if(err) throw err;
});
}
function logEdit(user, page) {
fs.appendFile('activity.log', `[${(new Date()).toUTCString()}] ${user} edited "${page}"\n`, err => {if (err) throw err;});
}
const showdown = require('showdown');
const converter = new showdown.Converter();
const sanitizeHtml = require('sanitize-html');
app.get('/article', (req,res) => {
res.set('Content-Type', 'text/plain');
res.status(404).send('404 Not Found. Were you looking for /article/<article name>?');
});
app.get('/article/:articleName',(req,res) => {
const path = 'public/articles/'+req.params.articleName+'.md';
fs.readFile(path, 'utf-8',(err,data) => {
if(err) {
if(err.code === 'ENOENT') res.status(404).send('No article with that name');
else res.status(500).send('Server Error');
return;
}
let resultHtml;
if (!cache[req.params.articleName]) cache[req.params.articleName] = {revisions: -1, cachedHtml: ''};
if(cache[req.params.articleName].revisions !== revisions[req.params.articleName]) {
resultHtml = sanitizeHtml(converter.makeHtml(data.toString()), {
allowedTags: [
"address", "article", "aside", "footer", "header", "h1", "h2", "h3", "h4",
"h5", "h6", "hgroup", "main", "nav", "section", "blockquote", "dd", "div",
"dl", "dt", "figcaption", "figure", "hr", "li", "main", "ol", "p", "pre",
"ul", "a", "abbr", "b", "bdi", "bdo", "br", "cite", "code", "data", "dfn",
"em", "i", "kbd", "mark", "q", "rb", "rp", "rt", "rtc", "ruby", "s", "samp",
"small", "span", "strong", "sub", "sup", "time", "u", "var", "wbr", "caption",
"col", "colgroup", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "img", "strikethrough"
],
});
updateCache(req.params.articleName, resultHtml);
} else {
resultHtml = cache[req.params.articleName].cachedHtml;
}
res.render('article-page', {
articleName: req.params.articleName,
articleBody: resultHtml,
loggedIn: !!req.get('X-Replit-User-Name')
});
});
});
app.get('/edit', (req,res) => {
res.set('Content-Type', 'text/plain');
res.status(404).send('404 Not Found. Were you looking for /edit/<article name>?');
});
app.get('/edit/:articleName', (req,res) => {
if (!req.get('X-Replit-User-Name')) return res.redirect(401, '/login');
const path = 'public/articles/'+req.params.articleName+'.md';
fs.readFile(path, 'utf-8',(err,data) => {
if(err && err.code !== 'ENOENT') {
res.status(500).send('Server Error');
return;
}
res.render('edit', {
newPageMsg: (function() {
if(err && err.code === 'ENOENT') {
return req.params.articleName+' does not exist, so you will create a new article';
} else {
return '';
}
})(),
articleName: req.params.articleName,
articleContent: data || ''
});
});
});
app.post('/edit/submit', (req,res) => {
if (!req.get('X-Replit-User-Name')) return res.redirect(401, '/login');
fs.writeFile('public/articles/'+req.body.articleName+'.md', req.body.articleContent, err => {
if(err) {
res.status(500).send('Server Error');
return;
}
if(!revisions[req.body.articleName]) {
revisions[req.body.articleName] = 0;
addItemToCache(req.body.articleName);
} else {
++revisions[req.body.articleName];
}
updateRevisionsFile();
res.redirect('/article/'+req.body.articleName);
logEdit(req.get('X-Replit-User-Name'), req.body.articleName);
});
});
app.get('/index', (req,res) => {
fs.readdir('public/articles', (err,files) => {
if(err) {
res.status(500).send('Server Error');
}
files.forEach((item,index) => {
files[index] = item.replace('.md','');
});
res.render('index-of-articles', {
listOfArticles: files
});
});
});
app.get('/', (req,res) => {
res.render('index', {
signed_in: !!req.get('X-Replit-User-Name')
});
});
app.get('/login', (req,res) => {
res.sendFile(__dirname + '/views/login.html');
});
app.get('/login/submit', (req,res) => {
res.redirect('/');
});
app.listen(8080);