Prerequisites
- have a blog with hugo
Introduction
So I spent a good couple of hours fighting with hugo and regex, because I didn’t like the fact that I had to manually enter the date each time in new posts. Furthermore I discovered that by only stating the date: year-month-day in the header. It can occur when uploading multiple posts on the same day, chronologically the listing becomes incorrect.
So to save ( hopefully at least millions of people ;)
) some time. I figured out how to fix this. Including setting the title and date automatically.
How to template
You can create template for pages in themes/beautifulhugo/archetypes > default.md
.
In this .md
file I placed the following 'code'
. Well regex stuff mostly..
---
title: "{{ replace .Name "-" " " | title }}"
author: "Vuurvoske"
type: "Post"
date: {{ .Date }}
subtitle: "<replace this>"
image: ""
tags: ["example", "tag"]
draft: true
---
## Prerequisites
# Introduction
## Documentation:
-
In the older version you can replace title as shown underneath:
title: {{ replace (replaceRE `^\d{4}.\d{2}.\d{2}-` "$1" .Name) "-" " " | title }}
note that this regex is encapsulated within a replace. I found out the hard way how to get it working
The title is the most difficult thing here. When I create a new blogpost, I did that by just copying a previous blog post, clearing it and setting everything manually.
But that takes to much time. I found that if you install hugo
, you can ‘autocreate’ new pages.
Let’s install the package hugo
with pacman
:
sudo pacman -S hugo
resolving dependencies...
looking for conflicting packages...
Packages (1) hugo-0.88.1-1
Total Download Size: 13,42 MiB
Total Installed Size: 61,01 MiB
:: Proceed with installation? [Y/n] y
:: Retrieving packages...
hugo-0.88.1-1-x86_64 13,4 MiB 23,5 MiB/s 00:01 [-----------------] 100%
:: Processing package changes...
(1/1) installing hugo [-----------------] 100%
Optional dependencies for hugo
python-pygments: syntax-highlight code snippets
python-docutils: reStructuredText support
:: Running post-transaction hooks...
(1/6) Arming ConditionNeedsUpdate...
(2/6) Foreign/AUR package notification
<blablababla>
(6/6) Syncing all file systems...
package installed!
Then use this command for example:
hugo new post/2021-10-29-Creating-a-template-for-new-posts.md
And if you are smart (or lazy), you can use this:
hugo new post/$(date +"%Y-%m-%d")-2Creating-a-template-for-new-posts.md
On a side note. I give my posts the following file name, this gives me some (chronological) overview in my git repo :)
I also found out that now i implemented this fix. The date will automatically be placed in the file name!
YYYY-MM-DD-<the-title-of-the-blog>.md
The script underneath will allow you to automatically create a new post, please make sure you create a template first!
#!/bin/bash
set -vx
if [ $# -eq 0 ]
then
echo "No arguments supplied"
echo "Please enter the title of your post"
exit 1
fi
echo "Website?"
read website
if [[ $(uname) == "Darwin" ]]
then
hugodir="$HOME/Documents/code/prive/${website}.gitlab.io"
elif [[ $(uname) == "Linux" ]]
then
hugodir="$HOME/code/prive/${website}.gitlab.io"
elif [[ $(uname) =~ "MING" ]]
then
hugodir="$HOME/code/${website}.gitlab.io"
fi
postdir="content/post"
namevar="${1}"
cd "${hugodir}"
echo "${hugodir}"
hugo new post/${namevar}.md
sed -i "s!username!${website}!g" ${hugodir}/${postdir}/${namevar}.md
mv ${hugodir}/${postdir}/${namevar}.md ${hugodir}/${postdir}/$(date +"%Y-%m-%d")-${namevar}.md
exit 1
Give it execution permissions
$ chmod u+x ~/hugopost.sh
and create an alias to use the script:
echo "alias newpost='~/hugopost.sh'" >> ~/.bash_aliases
Source the file and you’re ready to rock:
source ~/.bash_aliases
Now execute it on the terminal: newpost <name of your post>
$ newpost i-love-lord-of-the-rings
/home/vuurvoske/code/vuurvoske.gitlab.io/content/post/2021-10-29-i-love-lord-of-the-rings.md created
you do not need to place the date here in the filename yourself, hugo will take care of this for you! Thanks Hugo
Oke, sorry for the intermezzo. Let’s explain what’s happening within hugo when you create a new post
When create the new page with hugo new
the replaceRE
will first filter (read replace) out the date based on this regex ^\d{4}.\d{2}.\d{2}-
. It will filter out XXXX-XX-XX-
. (the dot will filter any character.
I know the first -
will be replaced and thus give an annoying extra empty space a the beginning of the title
. Hence the extra -
at the end of the regex.
When it has done the replaceRE
command, it will also replace all -
with spaces.
The result will be similar like underneath:
---
title: "Test Post"
author: "Vuurvoske"
type: "Post"
date: 2024-02-12T19:51:02+01:00
subtitle: "<replace this>"
image: ""
tags: ["example", "tag"]
draft: true
---
## Prerequisites
# Introduction
## Documentation:
-
note: Don’t forget to change the draft to false, once you are ready to publish\
Documentation:
The date
is based on a internal hugo function, which is based off go(lang).
Tags
, subtitle
and draft
speak for itself \
- https://theibbster.medium.com/how-to-build-a-blog-a-complete-beginners-guide-to-hugo-9f831b50aad
- https://regex101.com/
- https://gohugo.io/variables/shortcodes/
- https://gohugo.io/functions/replace/
- https://gohugo.io/functions/replacere/
- https://stackoverflow.com/questions/22061723/regex-date-validation-for-yyyy-mm-dd (shout out to wumpz!)
- https://gohugo.io/functions/format/