2023-12-28 17:26:32 -06:00
const express = require ( "express" ) ,
chokidar = require ( "chokidar" ) ,
fs = require ( "fs" ) ,
path = require ( "path" ) ,
showdown = require ( "showdown" ) ,
mkthtml = new showdown . Converter ( )
2023-12-28 17:48:56 -06:00
mkthtml . setFlavor ( "github" )
2023-12-28 17:26:32 -06:00
const PORT = process . env . PORT || 8080
var dataPath = path . join ( _ _dirname , 'data' )
var postsPath = path . join ( dataPath , 'posts' )
var staticPath = path . join ( _ _dirname , 'static' )
2024-01-30 19:07:57 -06:00
var watcher = chokidar . watch ( dataPath )
2023-12-28 17:26:32 -06:00
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 ( ) {
2024-01-30 19:07:57 -06:00
var data = JSON . parse ( fs . readFileSync ( path . join ( dataPath , 'data.json' ) ) )
var postsArray = data . posts
2023-12-28 17:26:32 -06:00
var html = ""
for ( var i = 0 ; i < postsArray . length ; i ++ ) {
var addedHTML = ""
2024-01-05 16:09:09 -06:00
var post = postsArray [ i ]
2024-01-30 19:07:57 -06:00
2023-12-28 17:26:32 -06:00
2024-01-30 19:07:57 -06:00
addedHTML +=
2024-03-03 16:17:15 -06:00
` <div class="post">
2024-01-30 19:07:57 -06:00
< a style = "text-decoration: none;" href = "./post/${post.path}" >
< h2 > $ { post . name } < / h 2 >
2024-03-03 16:17:15 -06:00
< p style = "color: white; font-size: 1rem;" > $ { post . desc } < / p >
2024-01-30 19:07:57 -06:00
< p style = "color: darkgray; font-size: 1rem;" > Path : < code > /post/ $ { post . path } < / c o d e > < / p >
< / a >
< / d i v > `
2023-12-28 17:26:32 -06:00
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 ( 'change' , pageUpdate )
2024-03-03 16:17:15 -06:00
. on ( 'add' , pageUpdate )
2023-12-28 17:26:32 -06:00
app . get ( '/post/:post*' , ( req , res ) => {
2024-01-05 16:18:02 -06:00
var postName = req . params . post
2023-12-28 17:26:32 -06:00
2024-01-30 19:07:57 -06:00
var data = JSON . parse ( fs . readFileSync ( path . join ( dataPath , 'data.json' ) ) )
var postsArray = data . posts
2024-01-05 21:16:20 -06:00
for ( let index = 0 ; index < postsArray . length ; index ++ ) {
const post = postsArray [ index ] ;
2024-01-30 19:07:57 -06:00
if ( post . path == postName ) {
postContent = mkthtml . makeHtml ( fs . readFileSync ( path . join ( postsPath , post . path ) ) . toString ( ) )
var html = fs . readFileSync ( path . join ( _ _dirname , 'resources/postPage.html' ) ) . toString ( )
html = html . replace ( '{POST}' , postContent )
html = html . replace ( '{POST_TITLE}' , post . name )
res . send ( html )
return
}
2024-01-05 21:16:20 -06:00
}
2023-12-28 17:26:32 -06:00
var html = fs . readFileSync ( path . join ( _ _dirname , 'resources/postPage.html' ) ) . toString ( )
2024-01-30 19:07:57 -06:00
html = html . replace ( "{POST_TITLE}" , "Not found!" )
html = html . replace ( "{POST}" , "<p>Couldn't find this post... Maybe try clearing your cache? Violet's Limbo is currently going through alot of backend changes, so expect things to break!</p>" )
2023-12-28 17:26:32 -06:00
res . send ( html )
2024-01-30 19:07:57 -06:00
return
2023-12-28 17:26:32 -06:00
} )