diff options
Diffstat (limited to 'modules/blog/template.js')
-rw-r--r-- | modules/blog/template.js | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/modules/blog/template.js b/modules/blog/template.js index 32d7bb3..a1d4d3d 100644 --- a/modules/blog/template.js +++ b/modules/blog/template.js @@ -1,5 +1,7 @@ const fs = require("fs").promises; +const blogUtil = require("./util.js"); + const pathRoot = "blog"; async function recursiveTree(dir) { @@ -39,6 +41,21 @@ function generateTree(tree, path) { return out; } +// path: file path inside repo +async function generateDatetime(repodir, path) { + if (path == "index.html") return ""; + + const stat = await fs.stat(repodir + "/" + path); + const gitout = await blogUtil.runCommandOutput("git", ["-C", repodir, "log", "--format=format:%as", "--", path]); + const lines = gitout.toString().trim().split("\n"); + const last = lines[0]; + const first = lines[lines.length - 1]; + if (last == first) + return `Last updated: <time datetime="${last}">${last}</time>`; + else + return `Created: <time datetime="${first}">${first}</time>, last updated: <time datetime="${last}">${last}</time>`; +} + async function template(repodir, contentPath) { const tree = await recursiveTree(repodir); tree.delete("$template"); @@ -50,6 +67,7 @@ async function template(repodir, contentPath) { html = html.replace("<!-- REPLACE TREE -->", generateTree(tree, pathRoot)); html = html.replace("<!-- REPLACE CONTENT -->", content); + html = html.replace("<!-- REPLACE DATETIME -->", await generateDatetime(repodir, contentPath)) return html; } |