disseminate/index.ts
hexlocation's laptop (asiago) 7c89da0baf fix dependencies
2024-03-29 01:29:56 -04:00

35 lines
1 KiB
TypeScript

import marked from "marked";
import express from "express"
import ejs from "ejs";
import fs from "fs";
import path from "path";
let app = express();
app.set("view engine", "ejs")
app.set('views', path.join(__dirname, 'templates'))
type Post = {
file: string;
title: string;
}
function contentScan(){
let posts: Post[] = []
fs.readdirSync(__dirname + "/posts").filter((name: string) => name.toLowerCase().endsWith(".md")).forEach((file: string) => {
posts.push({file:file, title: `lol (${file})`})
})
return posts;
}
app.get("/", (req:express.Request,res:express.Response) => {
res.render("index", {posts: (contentScan())})
});
app.use("/post/:post", (req:express.Request,res:express.Response) => {
let pathToPost = path.join(__dirname, "posts", req.params.post);
if(!fs.existsSync(pathToPost)) return res.end("404.");
let rawPost = fs.readFileSync(pathToPost, "utf-8");
let parsedPost = marked.parse(rawPost);
res.render("post", {posts: (contentScan()), rawPost: rawPost, parsedPost: parsedPost})
})
app.listen(3024)