How to set lastmod to file modified date?

Hi,

One thing that I often forget to do when editing web content is to update the front matter property lastmod

Typically, I should update this property to be YYYY-MM-DD for current date.
Well, I often forget…

So I was thinking, is there a way to programmatically set this property to the date that the file was last edited?

If not by jekyll, maybe it would be possible to set it using a perl script at a little bit of regex… Problem with that approach is that then I would set it for all files and they would now be updated to today. So next time I run it, I would set all files to today…

There was a plugin to do this, maybe it still works? or maybe can point you in the right direction?

The main issue is there is no db where you could easily check the last mod date - so somehow you would have to get it off the file itself.

I use the plugin last_modified. Its filename is jekyll-file-last-modified.rb, the file sits in the _plugins folder and thus does not need to be mentioned in _config.yml, it’s automatically available. Here’s the code:

# 2020-01-10 dealt with error when file does not exist
# 2019-01-18 added, and used in footer
# modified from (thank you!):
#   https://github.com/michaelx/jekyll-last-modified

module Jekyll
  class LastModifiedTag < Liquid::Tag

    def initialize(tag_name, path, tokens)
      super
      @path = path # copy into class property
    end

    def render(context)
      # Pipe parameter through Liquid to make additional replacements possible
      url = Liquid::Template.parse(@path).render context
	  
      # Adds the site source, so that it also works with a custom one
      site_source = context.registers[:site].config['source']
      file_path = site_source + '/' + url
      
      # ensure it works even if the file does not exist (yet), for tags
      begin
        File.mtime(file_path.strip!) # last modified date
      rescue
		"" # if exception, return empty string
	  end
    end

  end
end

Liquid::Template.register_tag('last_modified', Jekyll::LastModifiedTag)

To use this tag (in my footer include) I capture the file’s modification date like so:

{% capture lastModificationDate %}{% last_modified {{ page.path }} %}{% endcapture %}

This works fine for me. Hope this helps.

Thanks. This might work.

I see that you use it to get modification date in the actual content of the file. However, I need the lastmod date in the preample/front matter. Is this possible at all?

I will take a look at this but I am probably not going to be able to modify the code to fit my needs as I know very little ruby.

Oh my, that would be quite involved, methinks. Have not advice there, sorry.

This is the plugin I use for my site, and it works fine. One caveat is that you have to fetch the entire Git history in a build pipeline, or else the plugin falls back to mtime, which is often much more recent than actual changes to the file.

For example, when deploying with GitHub Actions, you’d need

    steps:
      - name: Check out repo
        uses: actions/checkout@v4.1.0
        with:
          fetch-depth: 0

to get the entire history. There’s a performance penalty, but it’s rather small in my experience.

The theme that I use (Minimal Mistakes) accesses the modification date using page.last_modified_at.

I am not sure I understand - how does this plugin set the lastmod time in the front matter?
page.last_modified_at is a variable you use in your content, right? Can I access this in front matter?

Exactly, my theme uses that variable where the last modified date should appear, like this:

You can’t use Liquid in front matter out of the box, so you can’t do something like

lastmod: {{ page.last_modified_at }}

Rather, you’d have to use page.last_modified_at wherever you currently use page.lastmod instead.

Hmm… OK. Maybe that’ll work… Although this seems like a pretty big change…

It’s potentially a very small change, depending on how you use that front matter field; you might have to update just one or two layout files.