While going through my old blog posts, I realized that some of them are getting dated, and that I should set aside time to test and/or update their content if necessary (especially those with technical instructions and command line syntax).
I needed a way to let readers know that, unless otherwise marked as updated, there’s a small chance that instructions and guides contained in older posts may not produce intended results.
The easiest approach I could think of was to use JavaScript to calculate the effective age of a post, and display a message to readers based on how old that post is. I’ll explain how I did this for my own site, and I’m sure you’ll be able to replicate as needed.
Creating DOM Nodes
First, I needed to create a hidden div
in Jekyll’s _layouts/post.html
template that will store the initial values needed:
In the above markup, the Liquid variable {{ page.date | date: "%s" }}
can be used to get the date in UNIX time format.
Also, the Liquid variable {{ page.categories }}
can be used to populate the list of categories that Jekyll generates for the blog post. I only need these types of disclaimers for posts in the Guides category of my blog — where code snippets are often present which may become deprecated or non-functional over time.
Finally, I created another div
where the message will be shown if the condition is matched:
Since I’m using Bootstrap, I leveraged the class name d-none
to toggle hidden items. If you’re not using a framework that offers this, you can add your own class for this purpose using the following CSS syntax:
Calculating Post Dates
First, I’ll declare a variable that references the target node containing the HTML message to be displayed:
Next, I’ll take the value of the UNIX timestamp that Jekyll generated for the blog post and store that in another variable:
Then, I’ll get the category that Jekyll generated, and store that value in a variable as well:
The string method
toLowerCase()
is used to guarantee sanitization of a string that will be used conditionally, which is in itself a JavaScript best-practice anyway.
Next up is the important bit, which is simply calculating the current UNIX timestamp (i.e. the time when the page was loaded), and then subtracting from that value the post’s timestamp (i.e when it was published).
The difference represents the effective age of the post (in seconds), and is stored it its own variable:
Since the post age is in seconds, further dividing this number by the number of seconds in one month will result in the age of the post in months:
With those variables and methods called, I can then conditionally show a message to the reader based on the age of the blog post that has been calculated:
Once the if
statement is true, the d-none
hidden class will be removed from the target node, thereby making the div
element visible, and the text within that element will be updated with whatever message is specified, using the postAgeMonths
variable to display the age of the post.
If the conditions for the if
statement are falsy, then then nothing will happen (i.e. the target node will remain hidden due to it’s CSS class).
The final JavaScript code should look like this:
That file can ideally be saved somewhere with a .js
extension and then injected at the bottom of each blog post (to ensure the variables point to DOM elements that have already been rendered and therefore can be read):
Thanks for reading, and I hope you can tweak this technique in your Jekyll blog to give readers an acknowledgement that content within a blog post (or page) may be outdated given the passage of time.
Links
- UNIX Time - en.wikipedia.org/wiki/Unix_time
- JavaScript
Date()
Object - developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date