2021-11-29 08:46:32 +00:00
|
|
|
/*
|
2021-12-05 21:34:59 +00:00
|
|
|
* Return our renderer.
|
|
|
|
* Using Nunjucks out of the box.
|
|
|
|
* https://mozilla.github.io/nunjucks/
|
|
|
|
*/
|
2021-11-29 08:46:32 +00:00
|
|
|
|
|
|
|
import nunjucks from "nunjucks";
|
|
|
|
const env: nunjucks.Environment = nunjucks.configure({ autoescape: true });
|
|
|
|
import { readFile } from "fs/promises";
|
2021-12-05 21:34:59 +00:00
|
|
|
import { Feeds, JSONValue } from "./@types/bubo";
|
2021-11-29 08:46:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Global filters for my Nunjucks templates
|
|
|
|
*/
|
|
|
|
env.addFilter("formatDate", function (dateString): string {
|
|
|
|
const date: Date = new Date(parseInt(dateString));
|
|
|
|
return !isNaN(date.getTime()) ? date.toLocaleDateString() : dateString;
|
|
|
|
});
|
|
|
|
|
2021-12-05 21:34:59 +00:00
|
|
|
env.addGlobal("now", new Date().toUTCString());
|
2021-11-29 08:46:32 +00:00
|
|
|
|
|
|
|
// load the template
|
2021-12-05 21:34:59 +00:00
|
|
|
const template: string = (
|
|
|
|
await readFile(new URL("../config/template.html", import.meta.url))
|
|
|
|
).toString();
|
2021-11-29 08:46:32 +00:00
|
|
|
|
|
|
|
// generate the static HTML output from our template renderer
|
2021-12-05 21:34:59 +00:00
|
|
|
const render = ({
|
|
|
|
data,
|
|
|
|
errors,
|
|
|
|
info
|
|
|
|
}: {
|
|
|
|
data: Feeds;
|
|
|
|
errors: unknown[];
|
|
|
|
info?: JSONValue;
|
|
|
|
}) => {
|
2021-11-29 08:46:32 +00:00
|
|
|
return env.renderString(template, {
|
|
|
|
data,
|
2021-12-05 21:34:59 +00:00
|
|
|
errors,
|
|
|
|
info
|
2021-11-29 08:46:32 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export { render };
|