mirror of
https://github.com/georgemandis/bubo-rss.git
synced 2024-11-27 14:08:19 +00:00
38 lines
950 B
TypeScript
38 lines
950 B
TypeScript
|
/*
|
||
|
* Return our renderer.
|
||
|
* Using Nunjucks out of the box.
|
||
|
* https://mozilla.github.io/nunjucks/
|
||
|
*/
|
||
|
|
||
|
import nunjucks from "nunjucks";
|
||
|
const env: nunjucks.Environment = nunjucks.configure({ autoescape: true });
|
||
|
import { readFile } from "fs/promises";
|
||
|
import { Feeds } from "./@types/bubo";
|
||
|
|
||
|
/**
|
||
|
* 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;
|
||
|
});
|
||
|
|
||
|
env.addGlobal("now", (new Date()).toUTCString());
|
||
|
|
||
|
// load the template
|
||
|
const template: string =
|
||
|
(await readFile(
|
||
|
new URL("../config/template.html", import.meta.url)
|
||
|
)).toString();
|
||
|
|
||
|
// generate the static HTML output from our template renderer
|
||
|
const render = ({ data, errors }: { data: Feeds; errors: unknown[] }) => {
|
||
|
return env.renderString(template, {
|
||
|
data,
|
||
|
errors
|
||
|
});
|
||
|
};
|
||
|
|
||
|
|
||
|
export { render };
|