Fix preview

This commit is contained in:
Daniella / Tove 2022-01-23 06:31:05 +01:00
parent e8fc11cbce
commit 35f3f1fe84
2 changed files with 14 additions and 7 deletions

View file

@ -35,16 +35,23 @@ server.use(require('body-parser').urlencoded({extended: false}))
server.all('/', function get(req, res) {
if(req.body.name && req.body.title && req.body.content) {
posts.push({timestamp: new Date().getTime(), author: req.body.name, title: req.body.title, content: req.body.content.replaceAll('\r\n', '\n'), comments: []})
res.redirect('/')
return
}
const fake = req.body.fake === 'yes'
let mainPage = {author: webname, title: 'All posts', content: 'These are all the posts on the board:', comments: []}
for (let i = 0; i < posts.length && i < 2000; i++) {
mainPage.comments.push({timestamp: posts[i].timestamp, author: posts[i].author, title: posts[i].title, content: posts[i].content, comments: []})
}
res.render('post.ejs', {post: mainPage, postid: '-1', webname: webname, email: email, comment: ''})
if(req.body.name && req.body.title && req.body.content) {
let post = {timestamp: new Date().getTime(), author: req.body.name, title: req.body.title, content: req.body.content.replaceAll('\r\n', '\n'), comments: []}
if(fake) {
mainPage.comments.push(post)
res.render('post.ejs', {post: mainPage, postid: '-1', webname: webname, email: email, comment: '', fake: true})
} else {
posts.push(post)
res.redirect(`/`)
}
return
}
res.render('post.ejs', {post: mainPage, postid: '-1', webname: webname, email: email, comment: '', fake: false})
})
server.get('/post', function get(req, res) {
if(req.query.id) {

View file

@ -125,7 +125,7 @@
ret += `<a href="/post/${postid}">[Nevermind]</a>`
ret +=
`</div></div><post><div class="content"><form method="post"><label>` +
`<h3><input type="username" name="name" value="Anonymous" ${fake ? `value="${post.comments[post.comments.length - 1].author.replaceAll("\"", "&quot;")}"` : ''} required>: <input type="text" name="title" placeholder="Title" ${fake ? `value="${post.comments[post.comments.length - 1].title.replaceAll("\"", "&quot;")}"` : ''} required></h3>` +
`<h3><input type="username" name="name" ${fake ? `value="${post.comments[post.comments.length - 1].author.replaceAll("\"", "&quot;")}"` : 'value="Anonymous"'} required>: <input type="text" name="title" placeholder="Title" ${fake ? `value="${post.comments[post.comments.length - 1].title.replaceAll("\"", "&quot;")}"` : ''} required></h3>` +
`<textarea name="content" placeholder="So basically, ..." required>${fake ? post.comments[post.comments.length - 1].content.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;") : ''}</textarea><br/><br/><button type="submit" name="fake" value="no">Submit</button> <button type="submit" name="fake" value="yes">Preview</button>` +
`</label></form></div></post><div>`
}