Incrementing variable in every build

I am trying to increment a front matter variable in every build. Initially the front-matter would be -

---
layout: essay
title: "Steampunk Metal Engraving"
counter: 0
---

and after a build the counter will increment automatically.

---
layout: essay
title: "Steampunk Metal Engraving"
counter: 1
---

Therefore, during every build down the line -

---
layout: essay
title: "Steampunk Metal Engraving"
counter: {{ counter++ }}
---

I want this to happen every time a netlify form is submitted and CI triggers a build. I think liquid is not allowed inside front-matter section of a file. Is there a way I can make this work?

Any suggestions will be greatly appreciated. TIA.

If I understand you correctly then you can’t do it like that as far as I am aware. You build static pages with Jekyll and they will always be generated the same way from identical source files.
So what you want to do is either manually edit the source file with some script or keep an extern database (or really just a file) that gets changed every time the site is built. For this you can use Jekyll’s Hooks which are really just plugins and are written in a similar way.

Then you can run a shell script from ruby as suggested here or just write the file editing in ruby if you feel you are up for the task.

1 Like

I looked at a plugin I wrote a few months ago that does a similar thing. I’ll leave it here and explain what it does:

Jekyll::Hooks.register :site, :post_write do

  # checks whether or not the directory 'downloads' exists in the
  # target folder and creates it if necessary.

  downloads = "[ -d '_site/downloads' ] || ([ -d '/path/to/original/downloads' ] && ln -s /path/to/original/downloads _site/downloads)"
  %x( #{downloads} )

end

So what did I do here? :site, :post_write says “After the whole site was built and written into the target directory, do the thing that is written below”.
In there I first define a linux shell oneliner as a string and execute that one afterwards using
%x( #{name_of_the_defined_command_string} ). This should work for you, too. If it doesn’t, have a look at the other options at the bottom of the page on hooks.
Those two things can be done in the same line of course but in my opinion that hurts readability (which is already pretty bad here). [-d ] just says if a directory exists and ln -s creates symbolic links. The rest is getting the logic straight.

For your problem you could look at how to read and write from files (if you are using a unix system) and use that instead of my string. Also, create the file if it doesn’t exist yet and maybe write an initial 1 into it.
You’ll also also need to figure out if you want to increase the counter with shell or with ruby and you have to keep in mind that this is a file editing process, which could lead you to potential deadlocks or information loss should multiple people trigger the process at the same time on the same file. Lastly, if you work locally and edit the file locally, then push it onto some server where multiple users can use it, then the two files need to synch up and maybe add their numbers correctly. Maybe only execute that script on the server or execute it all the time but exclude the file from being pushed to the server.

My solution isn’t perfect but all these latter problems you’d need to tackle anyway should you want to solve it in a way like you initially suggested.

Edit: That hook file of mine just sits in the same _plugins directory as all the other plugins in case you were wondering. I was stuck at this for a while when I started writing hooks.

1 Like

I will try to build on top of your example. Thanks a lot.

You are welcome :slight_smile:
Good luck!