diff --git a/.eleventy.js b/.eleventy.js index 5597bec..e1c27cc 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -142,6 +142,75 @@ module.exports = function (eleventyConfig) { } ); + async function getOriginalDate(filename) { + try { + const { stdout } = await execFile("git", [ + "log", + "--diff-filter=A", + "--follow", + "-1", + "--format=%cd", + filename, + ]); + return new Date(stdout); + } catch (e) { + console.error(e.message); + return null; + } + } + const originalDateCache = new Map(); + + eleventyConfig.addNunjucksAsyncFilter( + "earliestDate", + function (dateObj, filename, callback) { + const getDates = async () => { + let dates = []; + + // 1. Frontmatter Date + if (dateObj instanceof Date && !isNaN(dateObj)) { + dates.push(dateObj); + } + + // 2. Last Modified Date (cached) + let modDatePromise = lastModifiedDateCache.get(filename); + if (!modDatePromise) { + modDatePromise = lastModifiedDate(filename); + lastModifiedDateCache.set(filename, modDatePromise); + } + try { + const modDate = await modDatePromise; + if (modDate instanceof Date && !isNaN(modDate)) { + dates.push(modDate); + } + } catch(e) {} + + // 3. Original Creation Date (cached) + let origDatePromise = originalDateCache.get(filename); + if (!origDatePromise) { + origDatePromise = getOriginalDate(filename); + originalDateCache.set(filename, origDatePromise); + } + try { + const origDate = await origDatePromise; + if (origDate instanceof Date && !isNaN(origDate)) { + dates.push(origDate); + } + } catch(e) {} + + if (dates.length === 0) { + // Fallback if no valid dates found + return new Date(); + } + + // Find earliest + dates.sort((a, b) => a.getTime() - b.getTime()); + return dates[0]; + }; + + getDates().then(date => callback(null, date)).catch(err => callback(null, dateObj)); + } + ); + eleventyConfig.addFilter("encodeURIComponent", function (str) { return encodeURIComponent(str); }); diff --git a/_includes/layouts/base.njk b/_includes/layouts/base.njk index 80eddb0..5a894a1 100644 --- a/_includes/layouts/base.njk +++ b/_includes/layouts/base.njk @@ -106,6 +106,8 @@ {% if page.url and page.url.indexOf("/posts/") > -1 %}
Published
+ {% elif page.url and page.url.indexOf("/site-index/") == -1 %} +Originally published on
{% endif %} diff --git a/package-lock.json b/package-lock.json index 5b4c1da..ef148fa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -66,6 +66,7 @@ "integrity": "sha512-03ER4zukR6BgwppI5DHRE11lc+8B0fWsBrqacVWo3o49QkdEFXnEWjhyI9qd9LrPlgQHK2/MYyxuOvNwecyCLQ==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@11ty/dependency-tree": "^2.0.1", "@11ty/eleventy-utils": "^1.0.1", @@ -5468,6 +5469,7 @@ "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.2.13.tgz", "integrity": "sha512-FCE5xLH+hjbzRdpbRb1IMCvPv9yZx2QnDarBEYSN0N0HYk+TcXsEhwdFcFb+SRWOKzKGErhIEbBK2ogyLdTtfQ==", "license": "MIT", + "peer": true, "dependencies": { "colorette": "^1.2.2", "nanoid": "^3.1.22", @@ -6554,6 +6556,7 @@ "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.79.2.tgz", "integrity": "sha512-fS6iqSPZDs3dr/y7Od6y5nha8dW1YnbgtsyotCVvoFGKbERG++CVRFv1meyGDE1SNItQA8BrnCw7ScdAhRJ3XQ==", "license": "MIT", + "peer": true, "bin": { "rollup": "dist/bin/rollup" },