feat: localized time on cards

This commit is contained in:
Leyla Becker 2026-02-19 17:35:13 -06:00
parent 9a80850230
commit 55e67d1e27
5 changed files with 25 additions and 3 deletions

View file

@ -22,7 +22,7 @@ description: Welcome to my website! I write about tech, politics, food, and hobb
<li> <li>
<a href="{{ post.url }}" class="post-card"> <a href="{{ post.url }}" class="post-card">
<h2>{{ post.data.title }}</h2> <h2>{{ post.data.title }}</h2>
<time datetime="{{ post.data.createdAt | dateTimeFormat }}">{{ post.data.createdAt | dateTimeFormat }}</time> <time class="localizable-time" datetime="{{ post.data.createdAt | isoDateTime }}">{{ post.data.createdAt | dateTimeFormat }}</time>
{% if post.data.description %} {% if post.data.description %}
<p>{{ post.data.description }}</p> <p>{{ post.data.description }}</p>
{% endif %} {% endif %}
@ -54,6 +54,7 @@ description: Welcome to my website! I write about tech, politics, food, and hobb
<li> <li>
<a href="/recipe/{{ recipe.slug }}/" class="post-card"> <a href="/recipe/{{ recipe.slug }}/" class="post-card">
<h2>{{ recipe.data.newest.data.title }}</h2> <h2>{{ recipe.data.newest.data.title }}</h2>
<time class="localizable-time" datetime="{{ recipe.data.newest.data.createdAt | isoDateTime }}">{{ recipe.data.newest.data.createdAt | dateTimeFormat }}</time>
{% if recipe.data.newest.data.description %} {% if recipe.data.newest.data.description %}
<p>{{ recipe.data.newest.data.description }}</p> <p>{{ recipe.data.newest.data.description }}</p>
{% endif %} {% endif %}

View file

@ -13,7 +13,7 @@ permalink: /posts/
<li> <li>
<a href="{{ post.url }}" class="post-card"> <a href="{{ post.url }}" class="post-card">
<h2>{{ post.data.title }}</h2> <h2>{{ post.data.title }}</h2>
<time datetime="{{ post.data.createdAt | dateTimeFormat }}">{{ post.data.createdAt | dateTimeFormat }}</time> <time class="localizable-time" datetime="{{ post.data.createdAt | isoDateTime }}">{{ post.data.createdAt | dateTimeFormat }}</time>
{% if post.data.description %} {% if post.data.description %}
<p>{{ post.data.description }}</p> <p>{{ post.data.description }}</p>
{% endif %} {% endif %}

View file

@ -21,6 +21,7 @@ permalink: /recipes/
<li> <li>
<a href="/recipe/{{ slug }}/" class="post-card"> <a href="/recipe/{{ slug }}/" class="post-card">
<h2>{{ recipeData.newest.data.title }}</h2> <h2>{{ recipeData.newest.data.title }}</h2>
<time class="localizable-time" datetime="{{ recipeData.newest.data.createdAt | isoDateTime }}">{{ recipeData.newest.data.createdAt | dateTimeFormat }}</time>
{% if recipeData.newest.data.description %} {% if recipeData.newest.data.description %}
<p>{{ recipeData.newest.data.description }}</p> <p>{{ recipeData.newest.data.description }}</p>
{% endif %} {% endif %}

View file

@ -1,6 +1,7 @@
const fs = require('fs') const fs = require('fs')
const path = require("path"); const path = require("path");
const { execSync } = require("child_process"); const { execSync } = require("child_process");
const { DateTime } = require("luxon");
const getSlugFromPath = (filePath) => { const getSlugFromPath = (filePath) => {
// Normalize the path - remove leading ./ if present // Normalize the path - remove leading ./ if present
@ -62,6 +63,21 @@ const getFileCreatedTime = (filePath) => {
} }
} }
const getFileCreatedDateTime = (filePath) => {
const gitTime = getGitCreatedTime(filePath);
if (gitTime) {
return DateTime.fromMillis(gitTime, { zone: 'utc' });
}
try {
const stats = fs.statSync(filePath);
const time = stats.birthtime ?? stats.mtime;
return DateTime.fromJSDate(time, { zone: 'utc' });
} catch (e) {
return DateTime.utc();
}
}
const getVersion = (filePath) => { const getVersion = (filePath) => {
const dirName = path.dirname(filePath) const dirName = path.dirname(filePath)
@ -145,6 +161,9 @@ module.exports = {
isDraft: (data) => { isDraft: (data) => {
return data.draft === true; return data.draft === true;
}, },
createdAt: (data) => {
return getFileCreatedDateTime(data.page.inputPath);
},
permalink: (data) => { permalink: (data) => {
const slug = getSlugFromPath(data.page.inputPath); const slug = getSlugFromPath(data.page.inputPath);
const version = getVersion(data.page.inputPath); const version = getVersion(data.page.inputPath);

View file

@ -19,7 +19,7 @@ layout: base.njk
<li> <li>
<a href="{{ post.url }}" class="post-card"> <a href="{{ post.url }}" class="post-card">
<h2>{{ post.data.title }}</h2> <h2>{{ post.data.title }}</h2>
<time datetime="{{ post.date | dateTimeFormat }}">{{ post.date | dateTimeFormat }}</time> <time class="localizable-time" datetime="{{ post.data.createdAt | isoDateTime }}">{{ post.data.createdAt | dateTimeFormat }}</time>
{% if post.data.description %} {% if post.data.description %}
<p>{{ post.data.description }}</p> <p>{{ post.data.description }}</p>
{% endif %} {% endif %}
@ -36,6 +36,7 @@ layout: base.njk
<li> <li>
<a href="/recipe/{{ recipe.data.recipeSlug }}/" class="post-card"> <a href="/recipe/{{ recipe.data.recipeSlug }}/" class="post-card">
<h2>{{ recipe.data.title }}</h2> <h2>{{ recipe.data.title }}</h2>
<time class="localizable-time" datetime="{{ recipe.data.createdAt | isoDateTime }}">{{ recipe.data.createdAt | dateTimeFormat }}</time>
{% if recipe.data.description %} {% if recipe.data.description %}
<p>{{ recipe.data.description }}</p> <p>{{ recipe.data.description }}</p>
{% endif %} {% endif %}