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 all 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
12 changes: 12 additions & 0 deletions src/_includes/partials/post-sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ <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 On</h2>
<div class="post-last-updated">
<svg focusable="false" width="20" height="20" viewBox="0 0 610.398 610.398" aria-hidden="true" fill="currentColor"><path d="M159.567 0h-15.329c-1.956 0-3.811.411-5.608.995-8.979 2.912-15.616 12.498-15.616 23.997v51.613c0 2.611.435 5.078 1.066 7.44 2.702 10.146 10.653 17.552 20.158 17.552h15.329c11.724 0 21.224-11.188 21.224-24.992V24.992c0-13.804-9.5-24.992-21.224-24.992zM461.288 0h-15.329c-11.724 0-21.224 11.188-21.224 24.992v51.613c0 13.804 9.5 24.992 21.224 24.992h15.329c11.724 0 21.224-11.188 21.224-24.992V24.992C482.507 11.188 473.007 0 461.288 0z"/><path d="M539.586 62.553h-37.954v14.052c0 24.327-18.102 44.117-40.349 44.117h-15.329c-22.247 0-40.349-19.79-40.349-44.117V62.553H199.916v14.052c0 24.327-18.102 44.117-40.349 44.117h-15.329c-22.248 0-40.349-19.79-40.349-44.117V62.553H70.818c-21.066 0-38.15 16.017-38.15 35.764v476.318c0 19.784 17.083 35.764 38.15 35.764h468.763c21.085 0 38.149-15.984 38.149-35.764V98.322c.005-19.747-17.059-35.769-38.144-35.769zM527.757 557.9l-446.502-.172V173.717h446.502V557.9z"/><path d="M353.017 266.258h117.428c10.193 0 18.437-10.179 18.437-22.759s-8.248-22.759-18.437-22.759H353.017c-10.193 0-18.437 10.179-18.437 22.759 0 12.575 8.243 22.759 18.437 22.759zM353.017 348.467h117.428c10.193 0 18.437-10.179 18.437-22.759 0-12.579-8.248-22.758-18.437-22.758H353.017c-10.193 0-18.437 10.179-18.437 22.758 0 12.58 8.243 22.759 18.437 22.759zM353.017 430.676h117.428c10.193 0 18.437-10.18 18.437-22.759s-8.248-22.759-18.437-22.759H353.017c-10.193 0-18.437 10.18-18.437 22.759s8.243 22.759 18.437 22.759zM353.017 512.89h117.428c10.193 0 18.437-10.18 18.437-22.759 0-12.58-8.248-22.759-18.437-22.759H353.017c-10.193 0-18.437 10.179-18.437 22.759 0 12.579 8.243 22.759 18.437 22.759zM145.032 266.258H262.46c10.193 0 18.436-10.179 18.436-22.759s-8.248-22.759-18.436-22.759H145.032c-10.194 0-18.437 10.179-18.437 22.759.001 12.575 8.243 22.759 18.437 22.759zM145.032 348.467H262.46c10.193 0 18.436-10.179 18.436-22.759 0-12.579-8.248-22.758-18.436-22.758H145.032c-10.194 0-18.437 10.179-18.437 22.758.001 12.58 8.243 22.759 18.437 22.759zM145.032 430.676H262.46c10.193 0 18.436-10.18 18.436-22.759s-8.248-22.759-18.436-22.759H145.032c-10.194 0-18.437 10.18-18.437 22.759s8.243 22.759 18.437 22.759zM145.032 512.89H262.46c10.193 0 18.436-10.18 18.436-22.759 0-12.58-8.248-22.759-18.436-22.759H145.032c-10.194 0-18.437 10.179-18.437 22.759.001 12.579 8.243 22.759 18.437 22.759z"/></svg>
<time datetime="{{ last_updated | readableDate }}">{{ last_updated | readableDate }}</time>
</div>
</div>
{% endif %}

{% if tags %}
<div class="post__sidebar-module">
<h2 class="post__sidebar-module__title">Tags</h2>
Expand Down
5 changes: 5 additions & 0 deletions src/assets/scss/blog.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down