This TIL site is powered by Hugo, but it’s currently a lot of effort to create the title and name for one of these posts… It would be great if we could have a single command line tool which does this! Each post right now has the following header:

title: 'November 18, 2022 - Creating the perfect archetype in Hugo'
date: 2022-11-18T16:17:02-08:00
showToc: true
TocOpen: false
draft: false
hidemeta: false
comments: false
disableShare: false
disableHLJS: false
hideSummary: false
searchHidden: true
ShowReadingTime: true
ShowBreadCrumbs: true
ShowPostNavLinks: true
ShowWordCount: true
ShowRssButtonInSectionTermList: true
UseHugoToc: true

Most of this information is super easy to deal with - everything after showToc is static, so our template can just write those directly. This means we have only to deal with the title and the date.

The date is also pretty simple. Hugo provides a built-in short code for managing the date: {{ .Date }}, so we can easily add that.

The difficult detail is the title. We want it to be today’s date, followed by the text, and ideally, we create this with as few commands as possible. Turns out that Hugo just isn’t powerful enough to do this. So we’ll turn to bash. We can create a zsh function:

til () {
    # Change the working directory to this project
    cd ~/Projects/til

    # Get the current date in MM-DD-YYYY form
    date=$(date +"%m-%d-%Y")

    # Get the current date in plain text
    date_long=$(date +"%B %d, %Y")

    # Get the post name
    post_name=$1

    # Check if the file exists, and while they exist, add a postfix
    # to the file name
    index=0
    title_date=$date
    while [ -f "content/posts/$title_date.md" ]; do
        title_date="$date-$index"
        index=$((index + 1))
    done
    hugo new -k posts posts/${title_date}.md

    # Edit the title line to add the post title
    # If the index is 0, then we don't need to add the postfix
    if [ $index -eq 0 ]; then
        index=$((index + 1))
        sed -i '' "s/TITLE_TEMPLATE/$date_long - ${post_name}/g" "content/posts/$title_date.md"
    else
        sed -i '' "s/TITLE_TEMPLATE/$date_long ($index) - ${post_name}/g" "content/posts/$title_date.md"
    fi

    # Open the TIL document in VSCODE
    code content/posts/${title_date}.md
}

The final archetype looks like:

---
title: '$date_long ($index) - ${post_name}'
date: { { .Date } }
showToc: true
TocOpen: false
draft: false
hidemeta: false
comments: false
disableShare: false
disableHLJS: false
hideSummary: false
searchHidden: true
ShowReadingTime: true
ShowBreadCrumbs: true
ShowPostNavLinks: true
ShowWordCount: true
ShowRssButtonInSectionTermList: true
UseHugoToc: true
---

And running `til “My awesome title” from anywhere on my laptop publishes the TIL!