const express = require("express"), chokidar = require("chokidar"), fs = require("fs"), path = require("path"), showdown = require("showdown"), mkthtml = new showdown.Converter() mkthtml.setFlavor("github") const PORT = process.env.PORT || 8080 var dataPath = path.join(__dirname, 'data') var postsPath = path.join(dataPath, 'posts') var staticPath = path.join(__dirname, 'static') var watcher = chokidar.watch(postsPath) var app = express() app.use(express.static(staticPath)) var reqPaths = [dataPath, postsPath] for (var i = 0; i < reqPaths.length; i++) { var p = reqPaths[i] if (!fs.existsSync(p)) { fs.mkdirSync(p) } } app.listen(PORT, () => { console.log("Violet's Limbo is now listening on: " + PORT) }) function pageUpdate() { var postsArray = fs.readdirSync(postsPath).reverse() var html = "" for (var i = 0; i < postsArray.length; i++) { var addedHTML = "" var post = postsArray[i] addedHTML += `

${post.substring(4, post.length - 3)}

` html += addedHTML } var html = fs.readFileSync(path.join(__dirname, 'resources/mainPage.html')).toString().replace('{POSTS}', html) fs.writeFileSync(path.join(staticPath, 'index.html'), html) } watcher .on('add', pageUpdate) .on('change', pageUpdate) .on('unlink', pageUpdate) app.get('/post/:post*', (req, res) => { var post = fs.readFileSync(path.join(postsPath, req.params.post)).toString() post = mkthtml.makeHtml(post) var html = fs.readFileSync(path.join(__dirname, 'resources/postPage.html')).toString() html = html.replace('{POST}', post) html = html.replace('{POST_TITLE}', req.params.post.substring(3, req.params.post.length - 3)) res.send(html) })