diff options
author | Tom Smeding <tom@tomsmeding.com> | 2024-03-01 22:16:54 +0100 |
---|---|---|
committer | Tom Smeding <tom@tomsmeding.com> | 2024-03-01 22:16:54 +0100 |
commit | a246f40d5de04c1c7fcc7385d36bc593bcee6ce6 (patch) | |
tree | 10114424db8779dcb8680203623cc1a2e829f38d /modules/blog/template.js | |
parent | 9fa687bbf6be41ab54e38ee7e567890a16287032 (diff) |
blog: Add timestamp to posts
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; } |