Skip to content
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

Closed
tcurdt opened this issue Mar 22, 2020 · 5 comments
Closed

last modified and frontmatter date #1032

tcurdt opened this issue Mar 22, 2020 · 5 comments

Comments

@tcurdt
Copy link

tcurdt commented Mar 22, 2020

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.

@pdehaan
Copy link
Contributor

pdehaan commented Mar 22, 2020

Might be related to issue #869.

And not the answer you want, but got me wondering if you could use fs.statSync in a custom filter to get the last modified/created dates:

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 size will be super weird result, since it will be "The size of the file in bytes.", where "the file" is the actual input file (so the Nunjucks/Liquid source template, and not the generated HTML, etc. Semi confusing, but I was just trying to proof of concept something to make sure it was working as expected.

More info on available "stats" values at https://nodejs.org/api/fs.html#fs_class_fs_stats

@pdehaan
Copy link
Contributor

pdehaan commented Mar 22, 2020

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>&copy; 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 fs.statSync() approach since it's more straightforward, although I imagine it could use some more testing and error checking.

@tcurdt
Copy link
Author

tcurdt commented Mar 22, 2020

Might be related to issue #869.

Totally - exactly my use case. I am sorry I didn't find that.
Sorry, for the quick feedback.

Note that the size will be super weird result, since it will be "The size of the file in bytes.", where "the file" is the actual input file (so the Nunjucks/Liquid source template, and not the generated HTML, etc. Semi confusing, but I was just trying to proof of concept something to make sure it was working as expected.

But doesn't that mean I am also just getting the lastmod of the layout instead of the blog post with the frontmatter?

@pdehaan
Copy link
Contributor

pdehaan commented Mar 22, 2020

{{ page.inputPath | stat: "mtime" }}

Well, you’re getting the last modified date of the specified file (page.inputPath). So in my case that would be the post, but you could probably figure out the path format and point to a layout file or global data file or any arbitrary 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.

@tcurdt
Copy link
Author

tcurdt commented Mar 22, 2020

Ah, OK. I missed the inputPath.

Well, it would still be a nice feature to have - but I might go for the manual then.
Thanks for the help!

@tcurdt tcurdt closed this as completed Mar 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants