Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add last updated date to blog posts #692

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there not some way built into Eleventy to get the updated date/time? This feels a bit heavy to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


//-----------------------------------------------------------------------------
// Eleventy Config
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/_includes/partials/post-sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ <h2 class="post__sidebar-module__title">Contributors</h2>
{% endfor %}
</div>

{% 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 %}
<div class="post__sidebar-module">
<h2 class="post__sidebar-module__title">Last updated <time datetime="{{ last_updated | readableDate }}">{{ last_updated | readableDate }}</time></h2>
</div>
{% endif %}

{% if tags %}
<div class="post__sidebar-module">
<h2 class="post__sidebar-module__title">Tags</h2>
Expand Down