diff options
author | Raphael <mail@raphaelkabo.com> | 2023-10-09 11:10:51 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-09 11:10:51 +0100 |
commit | cc6fcb4c405d8cffacbf9b1082abf61e918482fa (patch) | |
tree | 693f324550dccedd50b6313165b88281a8ebcac8 /src/routes/static.ts | |
parent | 25fcdd1023550631f5fec6f750829fe09a311d66 (diff) | |
parent | 31022a7d323a351041b7b8508fb56c14fd699580 (diff) |
Merge pull request #114 from lowercasename/rk/static-pages
Static pages
Diffstat (limited to 'src/routes/static.ts')
-rw-r--r-- | src/routes/static.ts | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/routes/static.ts b/src/routes/static.ts new file mode 100644 index 0000000..33f0225 --- /dev/null +++ b/src/routes/static.ts @@ -0,0 +1,36 @@ +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(); + +if (config.static_pages?.length) { + 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; |