Skip to content

Commit 1d64720

Browse files
authored
Fix last updated (#28)
* Add helper * Update submodule
1 parent cb35a50 commit 1d64720

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

library

site/_globals/helpers.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const fs = require("fs");
2+
const { execSync } = require("child_process");
3+
const path = require("path");
24

35
/**
46
* Assemble related patterns by loading the page source from disk
@@ -35,6 +37,39 @@ const categoryName = (data) => {
3537
return category.data.title;
3638
};
3739

40+
const getPublishedDate = (data) => {
41+
try {
42+
const filePath = data.page.inputPath;
43+
44+
// Extract pattern name from path (e.g., site/library/visual-hash/index.md -> visual-hash)
45+
const pathParts = filePath.split("/");
46+
const patternName = pathParts[pathParts.length - 2];
47+
48+
// Query git history
49+
const libraryPatternPath = `patterns/${patternName}/index.md`;
50+
const workingDir = path.join(process.cwd(), "library");
51+
const gitCommand = `git log -1 --format="%ci" -- "${libraryPatternPath}"`;
52+
53+
const result = execSync(gitCommand, {
54+
cwd: workingDir,
55+
encoding: "utf-8",
56+
}).trim();
57+
58+
if (result) {
59+
const date = new Date(result);
60+
return date.toLocaleDateString("en-us");
61+
}
62+
} catch (error) {
63+
console.warn(
64+
`Could not get git date for pattern ${data.page.inputPath}:`,
65+
error.message
66+
);
67+
}
68+
69+
// Fallback to file date
70+
return data.page.date.toLocaleDateString("en-us");
71+
};
72+
3873
module.exports = {
3974
// environment helper
4075
environment: process.env.ELEVENTY_ENV,
@@ -45,4 +80,6 @@ module.exports = {
4580
relatedPatterns: getRelatedPatterns,
4681

4782
categoryName,
83+
84+
publishedDate: getPublishedDate,
4885
};

0 commit comments

Comments
 (0)