Add preview function
This commit is contained in:
parent
59aa77238f
commit
9df50a426d
2 changed files with 47 additions and 31 deletions
74
index.js
74
index.js
|
@ -59,39 +59,55 @@ server.all('/comment', function get(req, res) {
|
|||
let id = req.query.id
|
||||
let comment = req.query.comment
|
||||
if(posts[id]) {
|
||||
const fake = req.body.fake === 'yes'
|
||||
let toRemove = null
|
||||
let cid = ''
|
||||
function recurse(post) {
|
||||
console.log(String(cid) + ' ' + comment)
|
||||
if(String(cid) === comment) {
|
||||
if(req.body.name && req.body.title && req.body.content) {
|
||||
post.comments.push({timestamp: new Date().getTime(), author: req.body.name, title: req.body.title, content: req.body.content.replaceAll('\r\n', '\n'), comments: []})
|
||||
res.redirect(`/post/${id}`)
|
||||
cid = -1
|
||||
}
|
||||
return true
|
||||
}
|
||||
for(let i = 0; i < post.comments.length; i++) {
|
||||
const pid = cid
|
||||
cid += i + '_'
|
||||
if(recurse(post.comments[i]))
|
||||
try {
|
||||
function recurse(post) {
|
||||
console.log(String(cid) + ' ' + comment)
|
||||
if(String(cid) === comment) {
|
||||
if(req.body.name && req.body.title && req.body.content) {
|
||||
post.comments.push({timestamp: new Date().getTime(), author: req.body.name, title: req.body.title, content: req.body.content.replaceAll('\r\n', '\n'), comments: []})
|
||||
if(fake) {
|
||||
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: cid, fake: fake})
|
||||
toRemove = post
|
||||
} else
|
||||
res.redirect(`/post/${id}`)
|
||||
cid = -1
|
||||
}
|
||||
return true
|
||||
cid = pid
|
||||
}
|
||||
for(let i = 0; i < post.comments.length; i++) {
|
||||
const pid = cid
|
||||
cid += i + '_'
|
||||
if(recurse(post.comments[i]))
|
||||
return true
|
||||
cid = pid
|
||||
}
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
const f = recurse(posts[id]);
|
||||
if(f && cid == -1)
|
||||
return
|
||||
if(f) {
|
||||
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: cid})
|
||||
}
|
||||
else {
|
||||
if(req.body.name && req.body.title && req.body.content) {
|
||||
posts[id].comments.push({timestamp: new Date().getTime(), author: req.body.name, title: req.body.title, content: req.body.content.replaceAll('\r\n', '\n'), comments: []})
|
||||
res.redirect(`/post/${id}`)
|
||||
const f = recurse(posts[id]);
|
||||
if(f && cid == -1)
|
||||
return
|
||||
if(f) {
|
||||
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: cid, fake: fake})
|
||||
}
|
||||
else {
|
||||
if(req.body.name && req.body.title && req.body.content) {
|
||||
posts[id].comments.push({timestamp: new Date().getTime(), author: req.body.name, title: req.body.title, content: req.body.content.replaceAll('\r\n', '\n'), comments: []})
|
||||
if(fake) {
|
||||
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: cid, fake: fake})
|
||||
toRemove = posts[id]
|
||||
} else
|
||||
res.redirect(`/post/${id}`)
|
||||
}
|
||||
else
|
||||
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: ''})
|
||||
}
|
||||
} finally {
|
||||
if(fake) {
|
||||
toRemove.comments.pop()
|
||||
}
|
||||
else
|
||||
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: ''})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -125,8 +125,8 @@
|
|||
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" required>: <input type="text" name="title" placeholder="Title" required></h3>` +
|
||||
`<textarea name="content" placeholder="So basically, ..." required></textarea><br/><br/><button type="submit">Submit</button>` +
|
||||
`<h3><input type="username" name="name" value="Anonymous" ${fake ? `value="${post.comments[post.comments.length - 1].author.replaceAll("\"", """)}"` : ''} required>: <input type="text" name="title" placeholder="Title" ${fake ? `value="${post.comments[post.comments.length - 1].title.replaceAll("\"", """)}"` : ''} required></h3>` +
|
||||
`<textarea name="content" placeholder="So basically, ..." required>${fake ? post.comments[post.comments.length - 1].content.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">") : ''}</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>`
|
||||
}
|
||||
else
|
||||
|
|
Loading…
Add table
Reference in a new issue