-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
views.ts
28 lines (24 loc) · 852 Bytes
/
views.ts
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
/* SPDX-FileCopyrightText: 2016-present Kriasoft <[email protected]> */
/* SPDX-License-Identifier: MIT */
import { Express } from "express";
import handlebars from "express-handlebars";
/**
* Configure Handlebars as the default Express.js view engine.
*
* @see https://handlebarsjs.com/
* @see https://www.npmjs.com/package/express-handlebars
*/
export function withViews<T extends Express>(app: T): T {
app.engine(".hbs", handlebars({ extname: ".hbs", helpers: { json } }));
app.set("view engine", ".hbs");
return app;
}
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Safely serialize an object into a JSON string. Usage example:
*
* <script id="data" type="application/json">{{{json data}}}</script>
*/
function json(value: any): string {
return JSON.stringify(value).replace(/<\/script/gi, "</\\u0073cript");
}