disseminate/index.ts
2024-03-28 14:33:58 -04:00

36 lines
978 B
TypeScript

import express from "express";
import fs from "fs";
import ejs from "ejs";
import path from "path";
import marked from "marked";
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[] = []
let files = fs.readdirSync(__dirname + "/posts").filter(name => name.toLowerCase().endsWith(".md")).forEach(file => {
posts.push({file:file, title: `lol (${file})`})
})
return posts;
}
app.get("/", (req,res) => {
res.render("index", {posts: (contentScan())})
});
app.use("/post/:post", (req,res) => {
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)