summaryrefslogtreecommitdiff
path: root/src/routes/static.ts
blob: f57d1db17ba887a7575b7ce5537e29f9ff8cf923 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { Router, Request, Response } from "express";
import fs from "fs";
import getConfig, { frontendConfig } from "../lib/config.js";
import { markdownToSanitizedHTML } from "../util/markdown.js";

const config = getConfig();
const router = Router();

config.static_pages
    .filter((page) => page.path?.startsWith("/") && page.filename)
    .forEach((page) => {
        router.get(page.path, (_: Request, res: Response) => {
            try {
                if (fs.existsSync(`./static/${page.filename}`)) {
                    const fileBody = fs.readFileSync(
                        `./static/${page.filename}`,
                        "utf-8",
                    );
                    const parsed = markdownToSanitizedHTML(fileBody);
                    return res.render("static", {
                        title: page.title,
                        content: parsed,
                        ...frontendConfig(),
                    });
                }
                return res.status(404).render("404", frontendConfig());
            } catch (err) {
                console.error(err);
                return res.status(404).render("404", frontendConfig());
            }
        });
    });

export default router;