jabe/index.ts

36 lines
1 KiB
TypeScript
Raw Normal View History

2024-03-29 06:29:56 +01:00
import marked from "marked";
import express from "express"
import ejs from "ejs";
2024-03-29 06:29:56 +01:00
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[] = []
2024-03-29 06:29:56 +01:00
fs.readdirSync(__dirname + "/posts").filter((name: string) => name.toLowerCase().endsWith(".md")).forEach((file: string) => {
posts.push({file:file, title: `lol (${file})`})
})
return posts;
}
2024-03-29 06:29:56 +01:00
app.get("/", (req:express.Request,res:express.Response) => {
res.render("index", {posts: (contentScan())})
});
2024-03-29 06:29:56 +01:00
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)