Initial commit

This commit is contained in:
Daniella 2022-01-17 08:04:31 +01:00
commit acc2e5823b
4 changed files with 235 additions and 0 deletions

88
index.js Normal file
View file

@ -0,0 +1,88 @@
const Express = require('express')
const bdb = require('bdb.js')
const posts = bdb.load('posts.json', 1)
const webname = 'TheForum'
const server = new Express()
server.set('view engine', 'ejs')
function replace(req, regex, repl) {
const url = req.url.replaceAll(regex, repl)
console.log(url)
const parsedUrl = new URL('http://localhost' + url)
req.url = url
req.originalUrl = url
req.path = parsedUrl.pathname
req.search = parsedUrl.search
req._parsedUrl = parsedUrl
const query = {}
for(const entry of parsedUrl.searchParams) {
query[entry[0]] = entry[1]
}
req.query = query
}
server.use(function replacer(req, res, next) {
replace(req, /^\/post\/([0-9]+)/g, '/post?id=$1')
replace(req, /^\/comment\/([0-9]+)\/([0-9]+)(\?(.*))?/g, '/comment?id=$1&comment=$2&$4')
next()
})
server.get('/', function get(req, res) {
if(req.query.name && req.query.title && req.query.content) {
posts.unshift({author: req.query.name, title: req.query.title, content: req.query.content, comments: []})
res.redirect('/')
return
}
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++) {
mainPage.comments.push({author: posts[i].author, title: posts[i].title, content: posts[i].content, comments: []})
}
res.render('post.ejs', {post: mainPage, postid: '-1', webname: webname, comment: 1})
})
server.get('/post', function get(req, res) {
if(req.query.id) {
let id = req.query.id
if(posts[id]) {
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, comment: null})
}
}
})
server.get('/comment', function get(req, res) {
if(req.query.id && req.query.comment) {
let id = req.query.id
let comment = req.query.comment
if(posts[id]) {
let cid = 0
function recurse(post) {
cid++
console.log(String(cid) + comment)
if(String(cid) === comment) {
if(req.query.name && req.query.title && req.query.content) {
post.comments.unshift({author: req.query.name, title: req.query.title, content: req.query.content, comments: []})
res.redirect(`/post/${id}`)
return
}
return true
}
for(const comment of post.comments) {
if(recurse(comment))
return true
}
return false
}
if(recurse(posts[id])) {
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, comment: cid})
}
else
res.send(`err2 ${req.search} ${cid}`)
}
}
else
res.send("err1")
})
server.listen(process.env.PORT | 8080)

16
package.json Normal file
View file

@ -0,0 +1,16 @@
{
"name": "theforum",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"bdb.js": "^0.2.1-a",
"ejs": "^3.1.6",
"express": "^4.17.2"
}
}

15
posts.json Normal file
View file

@ -0,0 +1,15 @@
[
{
"title": "Default post",
"content": "This is the first post, it was here since the beginning.\n(Remove the space after the '[' and before the ']')\n [ * [* text *] * ] [ ** [** text **] ** ] [ *** [*** text ***] *** ] [ _ [_ text _] _ ] [ *_ [*_ text _*] _* ] [ **_ [**_ text _**] _** ] [ ***_ [***_ text _***] _*** [ **_ [**_ text _**] _** ] [ ***_ [***_ text _***] _*** ] [ \" [\" text \"] \" ]",
"author": "root",
"comments": [
{
"title": "The first comment",
"content": "This is the first comment, it was here since the beginning.",
"author": "Anonymous",
"comments": []
}
]
}
]

116
views/post.ejs Normal file
View file

@ -0,0 +1,116 @@
<html>
<head>
<meta name="viewport" content="width=device-width height=device-height">
<title><%=post.title%> - <%=webname%></title>
<style>
html {
background-color: #202020;
color: #a0a0a0;
font-family: sans-serif;
}
a {
color: #ffffff;
text-decoration: none;
}
div.posts {
margin: auto;
display: block;
width: fit-content;
}
post .content {
max-width: min(70vw, max(50vw, 100em));
background-color: #202030;
display: block;
padding: 0 5px 5px 5px;
border: 1px solid #808080;
}
post .content a {
display: block;
text-align: right;
}
post {
display: block;
margin-left: 5px;
padding-left: 5px;
border-left: 2px solid #808080;
padding-bottom: 5px;
padding-top: 5px;
}
h3 {
display: block;
margin-top: 5px;
}
pre {
overflow-x: scroll;
}
pre.inline {
display: inline;
}
button {
padding: 10px;
border-radius: 3px;
border: 1px solid #000000;
background-color: #000000;
color: #ffffff;
}
</style>
</head>
<body>
<a style="position: absolute; bottom: 10px; right: 10px;" href="/"><button type="button">Home</button></a>
<div class="posts">
<%- (function(){
let ret = ''
let mainPost = post
let id = postid == -1 ? -2 : 0
function recurse(post) {
id++
const makeForm = id == comment || postid == -1
ret +=
`<post><div class="content"><h3>${post.author}: ${post.title}</h3>` +
post.content.replaceAll('<','&lt;').replaceAll('>','&gt;')
.replaceAll('[* ', '<i>').replaceAll(' *]', '</i>')
.replaceAll('[** ', '<b>').replaceAll(' **]', '</b>')
.replaceAll('[*** ', '<b><i>').replaceAll(' ***]', '</i></b>')
.replaceAll('[_ ', '<u>').replaceAll(' _]', '</u>')
.replaceAll('[*_ ', '<i><u>').replaceAll(' _*]', '</u></i>')
.replaceAll('[**_ ', '<b><u>').replaceAll(' _**]', '</u></b>')
.replaceAll('[***_ ', '<b><i><u>').replaceAll(' _***]', '</u></i></b>')
.replaceAll('[" ', '<pre class="inline">').replaceAll(' "]', '</pre>')
.replaceAll('["" ', '<pre>').replaceAll(' ""]', '</pre>')
.replaceAll('\n\n', '<br/>')
if (postid != -1 || id == -1) {
if(makeForm) {
if(postid != -1)
ret += `<a href="/post/${postid}">[Nevermind]</a>`
ret +=
`</div><post><div class="content"><form><label>` +
`<h3><input type="username" name="name" value="Anonymous">: <input type="text" name="title" placeholder="Title"></h3>` +
`<textarea name="content" placeholder="Oh, nice!"></textarea><br/><br/><button type="submit">Submit</button>` +
`</label></form></div></post>`
}
else
ret += `<a href="/comment/${postid}/${id}">[Reply]</a> </div>`
}
else
ret += `<a href="/post/${id}">[Open]</a></div>`
for(const comment of post.comments) {
recurse(comment)
}
ret += '</post>'
}
recurse(post)
return ret
})() %>
</div>
<body>
<html>