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' )
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 ( ) {
2024-01-05 16:16:09 -06:00
var postsArray = fs . readdirSync ( postsPath ) . reverse ( )
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-05 21:16:20 -06:00
post = post . substring ( post . indexOf ( "]" ) + 2 , post . length - 3 )
2023-12-28 17:26:32 -06:00
2024-01-05 21:16:20 -06:00
addedHTML += ` <h2><a href="./post/ ${ post . split ( ' ' ) . join ( '_' ) } "> ${ post } </h2></a> `
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 ( 'add' , pageUpdate )
. on ( 'change' , pageUpdate )
. on ( 'unlink' , pageUpdate )
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-05 21:16:20 -06:00
var postsArray = fs . readdirSync ( postsPath ) . reverse ( )
var postsDict = { }
for ( let index = 0 ; index < postsArray . length ; index ++ ) {
const post = postsArray [ index ] ;
postsDict [ post . substring ( post . indexOf ( "]" ) + 2 , post . length - 3 ) . split ( ' ' ) . join ( '_' ) ] = post
}
console . log ( postsDict , postName )
if ( ! postsDict [ postName ] ) {
var html = fs . readFileSync ( path . join ( _ _dirname , 'resources/postPage.html' ) ) . toString ( )
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>" )
res . send ( html )
return
}
postName = postsDict [ postName ]
var post = fs . readFileSync ( path . join ( postsPath , postName ) ) . toString ( )
2023-12-28 17:26:32 -06:00
post = mkthtml . makeHtml ( post )
var html = fs . readFileSync ( path . join ( _ _dirname , 'resources/postPage.html' ) ) . toString ( )
html = html . replace ( '{POST}' , post )
2024-01-05 16:18:02 -06:00
html = html . replace ( '{POST_TITLE}' , postName . substring ( postName . indexOf ( "]" ) + 2 , postName . length - 3 ) )
2023-12-28 17:26:32 -06:00
res . send ( html )
} )