Hugo Post Script

I bounce around between Windows, Linux and MacOS, I have long since had a shell script that creates a new post with a page bundle and inserts the front matter and opens the file in sublime text. I wanted to do the same on windows with powershell so I could add the script to my path and call it from anywhere, I have a folder named bin in the root of C that is in my system path. You can find the script below.

 1# Change directory to blog root directory
 2cd "C:\Path\to\hugo\project"
 3
 4# Prompt the user for the path to the new post
 5$path = Read-Host "Enter the path to the new post (e.g. my-new-post)"
 6
 7# Create the new post using a page bundle
 8New-Item -ItemType Directory "content\post\$path"
 9New-Item -ItemType File "content\post\$path\index.md"
10
11# Add front matter to the new post
12@"
13+++
14categories = ['Technology']
15codeLineNumbers = false
16codeMaxLines = 10
17date = "$(Get-Date -Format "yyyy-MM-ddTHH:mm:sszzz")"
18description = ''
19draft = false
20featureImage = ''
21featureImageAlt = ''
22featureImageCap = ''
23figurePositionShow = true
24shareImage = ''
25tags = ['featured', '']
26thumbnail = ''
27title = "$(echo $path | %{ $_ -replace '-',' ' } | %{ $_.Substring(0,1).ToUpper() + $_.Substring(1) })"
28toc = false
29usePageBundles = true
30+++
31
32**Insert Lead paragraph here.**
33"@ | Out-File -FilePath "content\post\$path\index.md" -Encoding UTF8
34
35# Open the new post in Sublime Text
36& "C:\Program Files\Sublime Text\sublime_text.exe" "content\post\$path\index.md"

Here is the Bash version as well

 1#!/bin/bash
 2
 3# Prompt the user for the path to the new post
 4read -p "Enter the path to the new post (e.g. my-new-post): " path
 5
 6# Create the new post using a page bundle
 7mkdir -p "content/post/$path"
 8touch "content/post/$path/index.md"
 9
10# Add front matter to the new post
11cat <<EOT >> "content/post/$path/index.md"
12+++
13categories = ['Technology']
14codeLineNumbers = false
15codeMaxLines = 10
16date = "$(date +"%Y-%m-%dT%H:%M:%S%z")"
17description = ''
18draft = false
19featureImage = ''
20featureImageAlt = ''
21featureImageCap = ''
22figurePositionShow = true
23shareImage = ''
24tags = ['featured', '']
25thumbnail = ''
26title = "$(echo "$path" | sed -E 's/-/ /g' | awk '{print toupper(substr($0, 1, 1)) substr($0, 2)}')"
27toc = false
28usePageBundles = true
29+++
30
31**Insert Lead paragraph here.**
32EOT
33
34# Open the new post in Sublime Text
35subl "content/post/$path/index.md"

The script will change directory to the hugo project (You need to edit the line 2 to the correct path to your hugo directory) create a folder with an index.md using page bundles and add the front matter to the post. (You can edit the front matter to suite your needs just make sure that the output is UTF8 encoded, I had issues with posts not publishing or them showing up with weird characters instead of my text.)

Now your off to the races, This script has mad it so much easier to write content for hugo without having to remember syntax or long commands.

So long and Thanks for all the fish!