Add Last Post to 11ty Page

Apr 2, 2024

I wanted to show the latest post on my homepage, but only the last one. 11ty made this pretty easy. All that I had to do was add the following to my index page:

<hr />

### Latest Post
<a href={collections.post.last.url}>{collections.post.last.data.title}</a> - {collections.post.last.date | postDate }

Important Note: All curly braces above should be duplicated in actual code. I had to remove it so that 11ty would not actually interpret the code and resolve it to links. Putting it into a code block does not prevent it from doing so.

There's a lot of repeated code here. collections.post.last., specifically. I wanted to set that to a variable, but since I write my posts in Markdown, it seems that that isn't supported. I could probably do something with "shortcodes," a feature of 11ty that I haven't looked into yet, but this is good enough.

One thing I've learned from various projects is that sometimes, for small things like this site, "good enough" is actually better than "best." The value of my sanity and enjoyment of the process is greater than the value that I get out of a "perfect" solution.

Something that came up in this process was that, due to the way that I parse dates, there's no guarantee that posts written the same day will be shown in the correct order. It seems to default to alphabetical, so I simply added a 1 or a 2, etc, before the name of the post. My post files are formatted like this now:

2024-04-02_1homeServerOverview.md
2024-04-02_2addLastPostToHomepage.md