diff options
| author | Raphael Kabo <raphaelkabo@hey.com> | 2023-10-09 10:48:10 +0100 | 
|---|---|---|
| committer | Raphael Kabo <raphaelkabo@hey.com> | 2023-10-09 10:48:10 +0100 | 
| commit | 8b33335584afbac74388c4ed16ff1ff7a04e3588 (patch) | |
| tree | bc35e2e96695a56780139856dfd1f5267546193f /src/routes | |
| parent | 8a1f07b11e8e18243c058149ac58ece7766b7ef3 (diff) | |
Add static page config and handler
Diffstat (limited to 'src/routes')
| -rw-r--r-- | src/routes/static.ts | 34 | 
1 files changed, 34 insertions, 0 deletions
diff --git a/src/routes/static.ts b/src/routes/static.ts new file mode 100644 index 0000000..f57d1db --- /dev/null +++ b/src/routes/static.ts @@ -0,0 +1,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;  | 
