const fs = require("fs").promises; const pathRoot = "blog"; async function recursiveTree(dir) { const res = new Map(); const entries = await fs.readdir(dir, { withFileTypes: true }); for (const entry of entries) { if (entry.name[0] == ".") continue; if (entry.isDirectory()) { res.set(entry.name, await recursiveTree(dir + "/" + entry.name)); } else if (entry.isFile() && entry.name.endsWith(".html")) { res.set(entry.name.slice(0, entry.name.length - 5), true); } } return res; } function generateTree(tree, path) { let out = ""; for (const [entry, sub] of tree) { if (sub === true) { // file const elt = `
${entry}
\n`; out += elt; } else { // subdirectory out += '
\n'; out += `${entry}/\n`; out += '
\n'; out += generateTree(sub, path + "/" + entry); out += "
\n"; } } return out; } async function template(repodir, contentPath) { const tree = await recursiveTree(repodir); tree.delete("$template"); tree.delete("index"); let html = await fs.readFile(repodir + "/$template.html", { encoding: "utf-8" }); html = html.replace("", generateTree(tree, pathRoot)); if (contentPath) { const content = await fs.readFile(repodir + "/" + contentPath, { encoding: "utf-8" }); html = html.replace("", content); } return html; } module.exports = template;