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 | |
parent | 9fa687bbf6be41ab54e38ee7e567890a16287032 (diff) |
blog: Add timestamp to posts
Diffstat (limited to 'modules/blog')
-rw-r--r-- | modules/blog/blog.js | 24 | ||||
-rw-r--r-- | modules/blog/template.js | 18 | ||||
-rw-r--r-- | modules/blog/util.js | 24 |
3 files changed, 47 insertions, 19 deletions
diff --git a/modules/blog/blog.js b/modules/blog/blog.js index a866003..3985fdb 100644 --- a/modules/blog/blog.js +++ b/modules/blog/blog.js @@ -4,6 +4,7 @@ const https = require("https"); const child_process = require("child_process"); const generateTemplate = require("./template.js"); +const blogUtil = require("./util.js"); let moddir = null; let repodir = null; @@ -50,32 +51,17 @@ function fetch(url) { }); } -function runCommand(cmd, args) { - console.log(`blog: ${cmd} ${JSON.stringify(args)}`); - child_process.execFileSync( - cmd, args, - { stdio: "inherit", timeout: 20000 } - ); -} - -function runCommandOutput(cmd, args) { - return child_process.execFileSync( - cmd, args, - { timeout: 20000 } - ); -} - function ensureRepo() { try { const stats = fs.statSync(repodir); if (stats.isDirectory()) return; } catch (e) {} - runCommand("git", ["clone", "https://git.tomsmeding.com/blog", repodir]); + blogUtil.runCommand("git", ["clone", "https://git.tomsmeding.com/blog", repodir]); } function currentCommit() { - return runCommandOutput("git", ["-C", repodir, "rev-parse", "HEAD"]).toString().trim(); + return blogUtil.runCommandOutput("git", ["-C", repodir, "rev-parse", "HEAD"], { silent: true }).toString().trim(); } async function upstreamCommit() { @@ -85,8 +71,8 @@ async function upstreamCommit() { function updateRepo() { try { - runCommand("git", ["-C", repodir, "fetch", "--all"]); - runCommand("git", ["-C", repodir, "reset", "origin/master", "--hard"]); + blogUtil.runCommand("git", ["-C", repodir, "fetch", "--all"]); + blogUtil.runCommand("git", ["-C", repodir, "reset", "origin/master", "--hard"]); // Reset cache _after_ the commands succeeded; if anything failed, we // might at least still have stale cache data templateCache = new Map(); 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; } diff --git a/modules/blog/util.js b/modules/blog/util.js new file mode 100644 index 0000000..933ff30 --- /dev/null +++ b/modules/blog/util.js @@ -0,0 +1,24 @@ +const child_process = require("child_process"); + + +function runCommand(cmd, args) { + console.log(`blog: ${cmd} ${JSON.stringify(args)}`); + child_process.execFileSync( + cmd, args, + { stdio: "inherit", timeout: 20000 } + ); +} + +function runCommandOutput(cmd, args, opts) { + if (opts == null) opts = {}; + if (!opts.silent) console.log(`blog: ${cmd} ${JSON.stringify(args)}`); + return child_process.execFileSync( + cmd, args, + { timeout: 20000 } + ); +} + +module.exports = { + runCommand, + runCommandOutput, +}; |