Initialize

This commit is contained in:
2025-10-24 22:07:49 +08:00
commit 233f3fe40e
13 changed files with 1580 additions and 0 deletions

30
utils/templates.js Normal file
View File

@@ -0,0 +1,30 @@
const fs = require("fs");
const path = require("path");
function createTemplateRenderer({
baseDir = path.resolve("templates"),
useCache = true,
} = {}) {
const cache = {};
return function render(fileName, vars = {}) {
const filePath = path.join(baseDir, fileName);
let template;
if (useCache && cache[filePath]) {
template = cache[filePath];
} else {
template = fs.readFileSync(filePath, "utf8");
if (useCache) cache[filePath] = template;
}
let html = template;
for (const [key, value] of Object.entries(vars)) {
html = html.replace(new RegExp(`{{${key}}}`, "g"), value);
}
return html;
};
}
module.exports = { createTemplateRenderer };