From 261ccdefb8f23ade817fe44c8e6866c3da579c7e Mon Sep 17 00:00:00 2001 From: Pixel998 Date: Sat, 8 Feb 2025 17:04:17 +0300 Subject: [PATCH 1/4] feat: add last updated date to blog posts --- .eleventy.js | 22 ++++++++++++++++++++++ src/_includes/components/hero.macro.html | 3 +++ src/_includes/layouts/post.html | 4 ++++ 3 files changed, 29 insertions(+) diff --git a/.eleventy.js b/.eleventy.js index 3a8a06c500..c5cc62386f 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -22,6 +22,7 @@ const path = require("node:path"); const fs = require("node:fs"); const yaml = require("js-yaml"); const { DateTime } = require("luxon"); +const { execSync } = require("node:child_process"); //----------------------------------------------------------------------------- // Eleventy Config @@ -159,6 +160,27 @@ module.exports = eleventyConfig => { value1.concat(value2), ); + eleventyConfig.addFilter("gitLastUpdated", filepath => { + // Only check git history in production + if (CONTEXT) { + try { + const date = execSync(`git log -1 --format=%cD ${filepath}`, { + encoding: "utf-8", + }).trim(); + + if (!date) { + return null; + } + + return new Date(date); + } catch { + return null; + } + } + + return null; + }); + /** * *************************************************************************************** * Plugins diff --git a/src/_includes/components/hero.macro.html b/src/_includes/components/hero.macro.html index 3bd5534167..90291b9bab 100644 --- a/src/_includes/components/hero.macro.html +++ b/src/_includes/components/hero.macro.html @@ -17,6 +17,9 @@
{%- if params.post -%} + {% if params.post.lastUpdated %} + + {% endif %} {%- endif -%}

{{ params.title }}

diff --git a/src/_includes/layouts/post.html b/src/_includes/layouts/post.html index d23725ee5d..432012741d 100644 --- a/src/_includes/layouts/post.html +++ b/src/_includes/layouts/post.html @@ -18,6 +18,9 @@ {% set hero_title = title %} {% set hero_supporting_text = teaser %} {% set hero_date = page.date | readableDate %} + {% set git_last_updated = page.inputPath | gitLastUpdated %} + {% set last_updated = page.date if git_last_updated and git_last_updated < page.date else git_last_updated %} + {% set hero_last_updated = last_updated | readableDate if last_updated %} {% set hero_category = categories[0] %} {% set hero_category_url = categories[0] | slugify %} @@ -26,6 +29,7 @@ supporting_text: hero_supporting_text, post: { date: hero_date, + lastUpdated: hero_last_updated, category: hero_category, categoryURL: hero_category_url } From 13596baed876291d2f677e57857d2798cb90d34a Mon Sep 17 00:00:00 2001 From: Pixel998 Date: Wed, 12 Feb 2025 21:19:44 +0300 Subject: [PATCH 2/4] move last updated to sidebar --- src/_includes/components/hero.macro.html | 3 --- src/_includes/layouts/post.html | 4 ---- src/_includes/partials/post-sidebar.html | 8 ++++++++ 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/_includes/components/hero.macro.html b/src/_includes/components/hero.macro.html index 90291b9bab..3bd5534167 100644 --- a/src/_includes/components/hero.macro.html +++ b/src/_includes/components/hero.macro.html @@ -17,9 +17,6 @@

{%- if params.post -%} - {% if params.post.lastUpdated %} - - {% endif %} {%- endif -%}

{{ params.title }}

diff --git a/src/_includes/layouts/post.html b/src/_includes/layouts/post.html index 432012741d..d23725ee5d 100644 --- a/src/_includes/layouts/post.html +++ b/src/_includes/layouts/post.html @@ -18,9 +18,6 @@ {% set hero_title = title %} {% set hero_supporting_text = teaser %} {% set hero_date = page.date | readableDate %} - {% set git_last_updated = page.inputPath | gitLastUpdated %} - {% set last_updated = page.date if git_last_updated and git_last_updated < page.date else git_last_updated %} - {% set hero_last_updated = last_updated | readableDate if last_updated %} {% set hero_category = categories[0] %} {% set hero_category_url = categories[0] | slugify %} @@ -29,7 +26,6 @@ supporting_text: hero_supporting_text, post: { date: hero_date, - lastUpdated: hero_last_updated, category: hero_category, categoryURL: hero_category_url } diff --git a/src/_includes/partials/post-sidebar.html b/src/_includes/partials/post-sidebar.html index c109dd40c1..06076c2762 100644 --- a/src/_includes/partials/post-sidebar.html +++ b/src/_includes/partials/post-sidebar.html @@ -10,6 +10,14 @@

{% endfor %}
+ {% set git_last_updated = page.inputPath | gitLastUpdated %} + {% set last_updated = page.date if git_last_updated and git_last_updated < page.date else git_last_updated %} + {% if last_updated %} + + {% endif %} + {% if tags %} - {% set git_last_updated = page.inputPath | gitLastUpdated %} - {% set last_updated = page.date if git_last_updated and git_last_updated < page.date else git_last_updated %} + {% set file_last_updated = page.inputPath | fileLastUpdated %} + {% set last_updated = page.date if file_last_updated and file_last_updated < page.date else file_last_updated %} {% if last_updated %} {% endif %} diff --git a/src/assets/scss/blog.scss b/src/assets/scss/blog.scss index dffea547a1..0960a624fd 100644 --- a/src/assets/scss/blog.scss +++ b/src/assets/scss/blog.scss @@ -116,6 +116,11 @@ ul.tag-list { font-size: var(--step-0); } +.post-last-updated { + display: flex; + gap: 0.5rem; +} + // author bios .post__author-bios { padding-top: var(--space-m); From b57ef9a83775bc99e16981a7b19a6f051cb82358 Mon Sep 17 00:00:00 2001 From: Pixel998 Date: Thu, 20 Feb 2025 03:41:56 +0300 Subject: [PATCH 4/4] revert --- .eleventy.js | 15 ++++++++++++--- src/_includes/partials/post-sidebar.html | 4 ++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/.eleventy.js b/.eleventy.js index de2048da6c..c5cc62386f 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -22,6 +22,7 @@ const path = require("node:path"); const fs = require("node:fs"); const yaml = require("js-yaml"); const { DateTime } = require("luxon"); +const { execSync } = require("node:child_process"); //----------------------------------------------------------------------------- // Eleventy Config @@ -159,11 +160,19 @@ module.exports = eleventyConfig => { value1.concat(value2), ); - eleventyConfig.addFilter("fileLastUpdated", filepath => { + eleventyConfig.addFilter("gitLastUpdated", filepath => { + // Only check git history in production if (CONTEXT) { try { - const stats = fs.statSync(filepath); - return stats.mtime; + const date = execSync(`git log -1 --format=%cD ${filepath}`, { + encoding: "utf-8", + }).trim(); + + if (!date) { + return null; + } + + return new Date(date); } catch { return null; } diff --git a/src/_includes/partials/post-sidebar.html b/src/_includes/partials/post-sidebar.html index 3862d2473f..01b11f7e48 100644 --- a/src/_includes/partials/post-sidebar.html +++ b/src/_includes/partials/post-sidebar.html @@ -10,8 +10,8 @@ {% endfor %}
- {% set file_last_updated = page.inputPath | fileLastUpdated %} - {% set last_updated = page.date if file_last_updated and file_last_updated < page.date else file_last_updated %} + {% set git_last_updated = page.inputPath | gitLastUpdated %} + {% set last_updated = page.date if git_last_updated and git_last_updated < page.date else git_last_updated %} {% if last_updated %}