feat: sorted home recipe list by updated at time

This commit is contained in:
Leyla Becker 2026-02-22 11:13:07 -06:00
parent 928e7aecff
commit e46d2b8d38
2 changed files with 39 additions and 1 deletions

View file

@ -36,9 +36,10 @@ description: Welcome to my website! I write about tech, politics, food, and hobb
{% set recipesList = [] %} {% set recipesList = [] %}
{% for slug, recipeData in collections.recipesBySlug %} {% for slug, recipeData in collections.recipesBySlug %}
{% if recipeData.newest %} {% if recipeData.newest %}
{% set recipesList = recipesList.concat([{slug: slug, data: recipeData}]) %} {% set recipesList = recipesList.concat([{slug: slug, data: recipeData, updatedAt: recipeData.newest.data.updatedAt}]) %}
{% endif %} {% endif %}
{% endfor %} {% endfor %}
{% set recipesList = recipesList | sort(false, false, 'updatedAt') | reverse %}
{% if recipesList.length > 0 %} {% if recipesList.length > 0 %}
<div class="section-header"> <div class="section-header">

View file

@ -95,6 +95,21 @@ const getGitCreatedTime = (filePath) => {
return null; return null;
} }
const getGitModifiedTime = (filePath) => {
try {
const result = execSync(
`git log -1 --format=%aI -- "${filePath}"`,
{ encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }
).trim();
if (result) {
return DateTime.fromISO(result).toUTC();
}
} catch (e) {
// Git command failed, return null
}
return null;
}
const getFileCreatedTime = (filePath) => { const getFileCreatedTime = (filePath) => {
// Try git first for accurate cross-system creation time // Try git first for accurate cross-system creation time
const gitTime = getGitCreatedTime(filePath); const gitTime = getGitCreatedTime(filePath);
@ -127,6 +142,23 @@ const getFileCreatedDateTime = (filePath) => {
} }
} }
const getFileModifiedTime = (filePath) => {
try {
const stats = fs.statSync(filePath);
return DateTime.fromJSDate(stats.mtime, { zone: 'utc' });
} catch (e) {
return null;
}
}
const parseDate = (value) => {
if (!value) return null;
if (DateTime.isDateTime(value)) return value;
if (value instanceof Date) return DateTime.fromJSDate(value, { zone: 'utc' });
const parsed = DateTime.fromISO(value, { zone: 'utc' });
return parsed.isValid ? parsed : null;
}
const getVersion = (filePath) => { const getVersion = (filePath) => {
const dirName = path.dirname(filePath) const dirName = path.dirname(filePath)
@ -216,6 +248,11 @@ module.exports = {
createdAt: (data) => { createdAt: (data) => {
return getFileCreatedDateTime(data.page.inputPath); return getFileCreatedDateTime(data.page.inputPath);
}, },
updatedAt: (data) => {
return parseDate(data.updatedAt) ??
getGitModifiedTime(data.page.inputPath) ??
getFileModifiedTime(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);