-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
150 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.