diff --git a/index.njk b/index.njk index a5a4eac..ddfc485 100644 --- a/index.njk +++ b/index.njk @@ -36,9 +36,10 @@ description: Welcome to my website! I write about tech, politics, food, and hobb {% set recipesList = [] %} {% for slug, recipeData in collections.recipesBySlug %} {% 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 %} {% endfor %} +{% set recipesList = recipesList | sort(false, false, 'updatedAt') | reverse %} {% if recipesList.length > 0 %}
diff --git a/recipes/recipes.11tydata.js b/recipes/recipes.11tydata.js index 040b09c..d0c658d 100644 --- a/recipes/recipes.11tydata.js +++ b/recipes/recipes.11tydata.js @@ -95,6 +95,21 @@ const getGitCreatedTime = (filePath) => { 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) => { // Try git first for accurate cross-system creation time 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 dirName = path.dirname(filePath) @@ -216,6 +248,11 @@ module.exports = { createdAt: (data) => { return getFileCreatedDateTime(data.page.inputPath); }, + updatedAt: (data) => { + return parseDate(data.updatedAt) ?? + getGitModifiedTime(data.page.inputPath) ?? + getFileModifiedTime(data.page.inputPath); + }, permalink: (data) => { const slug = getSlugFromPath(data.page.inputPath); const version = getVersion(data.page.inputPath);