bubo-rss/src/renderer.ts
George Mandis c9e98d79b6
Introducing Bubo 2.0.0 (#6)
Converting to TypeScript!
2021-11-29 03:46:32 -05:00

37 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 };