Skip to content

Commit

Permalink
small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed May 21, 2022
1 parent fde3e4c commit 640743a
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 345 deletions.
66 changes: 11 additions & 55 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import express from "express";
import nunjucks from "nunjucks";
import markdown from "nunjucks-markdown";
import cookieParser from "cookie-parser";
import sessions from "express-session";
import { marked } from "marked";
import { configureNunjucks } from "./init.mjs";
import {
getPublicInformation,
gallery,
Expand Down Expand Up @@ -78,7 +76,7 @@ const aw = (cb) => {
// ROUTES
router.get(
"/resource/:id",
aw(async (req, res, next) => {
aw(async (req, res, _next) => {
const resp = await download(req.params.id);
const headers = Array.from(resp.headers)
.filter(([key]) => !key.includes("content-encoding"))
Expand All @@ -90,7 +88,7 @@ router.get(
);
router.get(
"/sparql-form",
aw(async (req, res, next) => {
aw(async (req, res, _next) => {
res.render("sparql.html", {
pageTitle: WEBSITE_TITLE,
activePage: "sparql-form",
Expand All @@ -101,7 +99,7 @@ router.get(
);
router.post(
"/sparql-form",
aw(async (req, res, next) => {
aw(async (req, res, _next) => {
const result = await postSparqlQuery(req.body);

res.render("sparql.html", {
Expand All @@ -115,7 +113,7 @@ router.post(
);
router.get(
"/contact",
aw(async (req, res, next) => {
aw(async (req, res, _next) => {
const message = req.query.message;
res.render("contact.html", {
pageTitle: WEBSITE_TITLE,
Expand All @@ -126,7 +124,7 @@ router.get(
);
router.post(
"/contact",
aw(async (req, res, next) => {
aw(async (req, res, _next) => {
if (req.session?.mailSent) {
res.redirect("/contact?message=" + "mail already sent");
} else {
Expand All @@ -139,7 +137,7 @@ router.post(
);
router.get(
"/gallery",
aw(async (req, res, next) => {
aw(async (req, res, _next) => {
const page = parseInt(req.query.page) || undefined;
const pageSize = parseInt(req.query.pageSize) || undefined;
res.render("gallery.html", {
Expand All @@ -152,7 +150,7 @@ router.get(
// ROUTES
router.get(
"/blog/posts/:postId/:slug",
aw(async (req, res, next) => {
aw(async (req, res, _next) => {
const id = req.params.postId;
const slug = req.params.slug;
res.render("post.html", {
Expand All @@ -164,7 +162,7 @@ router.get(
);
router.get(
"/blog",
aw(async (req, res, next) => {
aw(async (req, res, _next) => {
const page = parseInt(req.query.page) || undefined;
const pageSize = parseInt(req.query.pageSize) || undefined;
res.render("blog.html", {
Expand All @@ -177,7 +175,7 @@ router.get(

router.get(
"/",
aw(async (req, res, next) => {
aw(async (_req, res, _next) => {
res.render("index.html", {
publicInfo: await getPublicInformation(),
pageTitle: WEBSITE_TITLE,
Expand All @@ -189,54 +187,12 @@ router.get(
app.use("/", router);
// ERROR

app.use((err, req, res, next) => {
app.use((err, _req, res, _next) => {
res.status(500);
console.error(err.stack);
res.render("error.html", { error: err });
});

app.listen(SERVER_PORT, () => console.log(`Listen to ${SERVER_PORT}`));

function configureNunjucks(app) {
const nunjucksEnv = nunjucks.configure("views", {
autoescape: true,
express: app,
});

nunjucksEnv.addFilter("json", function (value, spaces) {
if (value instanceof nunjucks.runtime.SafeString) {
value = value.toString();
}
const jsonString = JSON.stringify(value, null, spaces).replace(
/</g,
"\\u003c"
);
return nunjucks.runtime.markSafe(jsonString);
});
nunjucksEnv.addFilter("slug", function (value, spaces) {
const slugify = (input) => {
return input
.toString()
.toLowerCase()
.replace(/\s+/g, "-") // Replace spaces with -
.replace(/[^\w\-]+/g, "") // Remove all non-word chars
.replace(/\-\-+/g, "-") // Replace multiple - with single -
.replace(/^-+/, "") // Trim - from start of text
.replace(/-+$/, ""); // Trim - from end of text
};
if (value instanceof nunjucks.runtime.SafeString) {
value = value.toString();
}
const slug = slugify(value);
return nunjucks.runtime.markSafe(slug);
});

nunjucksEnv.addFilter("date", function (value, spaces) {
if (value instanceof nunjucks.runtime.SafeString) {
value = value.toString();
}
const dateValue = new Date(value);
return nunjucks.runtime.markSafe(dateValue.toLocaleDateString("fr"));
});
markdown.register(nunjucksEnv, marked);
}
48 changes: 48 additions & 0 deletions init.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

import nunjucks from "nunjucks";
import markdown from "nunjucks-markdown";
import { marked } from "marked";

export function configureNunjucks(app) {
const nunjucksEnv = nunjucks.configure("views", {
autoescape: true,
express: app,
});

nunjucksEnv.addFilter("json", function (value, spaces) {
if (value instanceof nunjucks.runtime.SafeString) {
value = value.toString();
}
const jsonString = JSON.stringify(value, null, spaces).replace(
/</g,
"\\u003c"
);
return nunjucks.runtime.markSafe(jsonString);
});
nunjucksEnv.addFilter("slug", function (value, spaces) {
const slugify = (input) => {
return input
.toString()
.toLowerCase()
.replace(/\s+/g, "-") // Replace spaces with -
.replace(/[^\w\-]+/g, "") // Remove all non-word chars
.replace(/\-\-+/g, "-") // Replace multiple - with single -
.replace(/^-+/, "") // Trim - from start of text
.replace(/-+$/, ""); // Trim - from end of text
};
if (value instanceof nunjucks.runtime.SafeString) {
value = value.toString();
}
const slug = slugify(value);
return nunjucks.runtime.markSafe(slug);
});

nunjucksEnv.addFilter("date", function (value, spaces) {
if (value instanceof nunjucks.runtime.SafeString) {
value = value.toString();
}
const dateValue = new Date(value);
return nunjucks.runtime.markSafe(dateValue.toLocaleDateString("fr"));
});
markdown.register(nunjucksEnv, marked);
}
Loading

0 comments on commit 640743a

Please sign in to comment.