-
-
Notifications
You must be signed in to change notification settings - Fork 506
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
last modified and frontmatter date #1032
Comments
Might be related to issue #869. And not the answer you want, but got me wondering if you could use const fs = require("fs");
//...
eleventyConfig.addFilter("stat", (file, field="birthtime") => {
return fs.statSync(file)[field];
}); USAGE <!-- in some Liquid template, but Nunjucks should be similar-ish... -->
<pre>
>> birthtime: {{ page.inputPath | stat }}
>> mtime: {{ page.inputPath | stat: "mtime" }}
>> size: {{ page.inputPath | stat: "size" }}
</pre> Note that the More info on available "stats" values at https://nodejs.org/api/fs.html#fs_class_fs_stats |
I had a few more minutes, so I was also playing around w/ a custom Nunjucks async filter, which can be a bit confusing since they use callbacks (and not promises): const fs = require("fs");
module.exports = (eleventyConfig) => {
/**
* USAGE:
* {{ page.inputPath | stat("atime") }} -- The timestamp indicating the last time this file was accessed.
* {{ page.inputPath | stat("birthtime") }} -- The timestamp indicating the creation time of this file.
* {{ page.inputPath | stat("mtime") }} -- The timestamp indicating the last time this file was modified.
* {{ page.inputPath | stat("ctime") }} -- The timestamp indicating the last time the file status was changed.
* {{ page.inputPath | stat }} -- Alias for `stat("birthtime")`.
*/
eleventyConfig.addNunjucksAsyncFilter("stat", (file, prop="birthtime", callback) => {
// If you called a naked `{{ page.inputPath | stat }}`, then the callback
// function gets set to the `prop` attribute, so we need to juggle some
// attribute values.
if (typeof prop === "function") {
callback = prop;
prop = "birthtime";
}
fs.stat(file, (err, stats) => callback(err, stats && stats[prop]));
}); <footer>
<p>© 2020-Infinity</p>
<p>Created: {{ page.inputPath | stat | to_locale_date }}</p>
<p>Modified: {{ page.inputPath | stat("mtime") | to_locale_date }}</p>
</footer> Personally, I prefer the original |
Totally - exactly my use case. I am sorry I didn't find that.
But doesn't that mean I am also just getting the lastmod of the layout instead of the blog post with the frontmatter? |
{{ page.inputPath | stat: "mtime" }} Well, you’re getting the last modified date of the specified file ( Another solution might be to just manually specify the last modified date in your posts frontmatter. Depending on your use case you may not want to display a last modified date on a post if all you did was change some UI or layout or frontmatter and not any of the page content. More manual, but more flexibility. |
Ah, OK. I missed the Well, it would still be a nice feature to have - but I might go for the manual then. |
I've read
https://www.11ty.dev/docs/dates/
https://www.11ty.dev/docs/data-cascade/
https://www.11ty.dev/docs/data-eleventy-supplied/
but I cannot figure out how to get both, the page date from the frontmatter AND the last modification date from the page.
The text was updated successfully, but these errors were encountered: