2024-03-29 06:29:56 +01:00
|
|
|
import marked from "marked";
|
|
|
|
import express from "express"
|
2024-03-28 19:33:58 +01:00
|
|
|
import ejs from "ejs";
|
2024-03-29 06:29:56 +01:00
|
|
|
import fs from "fs";
|
2024-03-28 19:33:58 +01:00
|
|
|
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) => {
|
2024-03-28 19:33:58 +01:00
|
|
|
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) => {
|
2024-03-28 19:33:58 +01:00
|
|
|
res.render("index", {posts: (contentScan())})
|
|
|
|
});
|
|
|
|
|
2024-03-29 06:29:56 +01:00
|
|
|
app.use("/post/:post", (req:express.Request,res:express.Response) => {
|
2024-03-28 19:33:58 +01:00
|
|
|
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)
|