diff --git a/.eleventy.js b/.eleventy.js index f68f173..a193533 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -44,7 +44,9 @@ const { DateTime } = require("luxon"); const { promisify } = require("util"); const fs = require("fs"); const hasha = require("hasha"); -const readFile = promisify(require("fs").readFile); +const readFile = promisify(fs.readFile); +const stat = promisify(fs.stat); +const execFile = promisify(require("child_process").execFile); const pluginRss = require("@11ty/eleventy-plugin-rss"); const pluginSyntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight"); const pluginNavigation = require("@11ty/eleventy-navigation"); @@ -89,9 +91,40 @@ module.exports = function (eleventyConfig) { .catch((error) => callback(error)); }); - eleventyConfig.addFilter("lastModifiedDate", function (filename) { - const stats = fs.statSync(filename); - return stats.mtime; // Date + async function lastModifiedDate(filename) { + try { + const { stdout } = await execFile("git", [ + "log", + "-1", + "--format=%cd", + filename, + ]); + return new Date(stdout); + } catch (e) { + console.error(e.message); + // Fallback to stat if git isn't working. + const stats = await stat(filename); + return stats.mtime; // Date + } + } + // Cache the lastModifiedDate call because shelling out to git is expensive. + // This means the lastModifiedDate will never change per single eleventy invocation. + const lastModifiedDateCache = new Map(); + eleventyConfig.addNunjucksAsyncFilter("lastModifiedDate", function ( + filename, + callback + ) { + const call = (result) => { + result.then((date) => callback(null, date)); + result.catch((error) => callback(error)); + }; + const cached = lastModifiedDateCache.get(filename); + if (cached) { + return call(cached); + } + const promise = lastModifiedDate(filename); + lastModifiedDateCache.set(filename, promise); + call(promise); }); eleventyConfig.addFilter("encodeURIComponent", function (str) { diff --git a/_11ty/blurry-placeholder.js b/_11ty/blurry-placeholder.js index c9a24ce..b1bc6be 100644 --- a/_11ty/blurry-placeholder.js +++ b/_11ty/blurry-placeholder.js @@ -109,7 +109,7 @@ module.exports = async function (src) { - diff --git a/_includes/layouts/base.njk b/_includes/layouts/base.njk index 99ae89d..f0135b7 100644 --- a/_includes/layouts/base.njk +++ b/_includes/layouts/base.njk @@ -15,6 +15,7 @@ {% endif %} + {{ renderData.title or title or metadata.title | escape }} {% set desc = renderData.description or description %} diff --git a/test/test-generic-post.js b/test/test-generic-post.js index b01ed52..bd0fba0 100644 --- a/test/test-generic-post.js +++ b/test/test-generic-post.js @@ -63,7 +63,8 @@ describe("check build output for a generic post", () => { it("should have script elements", () => { const scripts = doc.querySelectorAll("script[src]"); - expect(scripts).to.have.length(GA_ID ? 2 : 1); + let has_ga_id = GA_ID ? 1 : 0; + expect(scripts).to.have.length(has_ga_id + 1); // NOTE: update this when adding more