Archetypes and the hugo new
command make creating new content based on predefined frontmatter fields in Hugo easy as pie. But if you’re like me, the syntax for doing so tends to escape you when you need it most — especially when page bundles or content sections are involved.
I try to avoid context switches and start writing ASAP when creativity strikes to keep my ideas flowing.
In this post, I’ll show you how to write a short shell script to help remedy this. It’s nothing too complex. You should be able to follow along even if you’re not familiar with the syntax.
Assumptions
- You’ve defined a page bundle archetype within your Hugo project (e.g.
archetypes/posts/index.md
). If you don’t have one, you can copy the default or your custom (standard) archetype into a same-named directory, and change the file name toindex.md
as shown in the path above. - You’re creating content under a section or subsection (e.g.
posts
,posts/tutorials
). This isn’t worth automating otherwise. - You prefer generating content as (leaf) page bundles, not standard single pages. The syntax is easy to remember in the latter case.
Writing the Script
Create the File
Create the file new.sh
in the base directory of your project. The whole idea is to keep the name short to avoid extra typing.
1touch new.sh
Arguments
Our script will have 3 available arguments:
archetype-name
(required)section-name
(optional)post-name
(required)
If section-name
is omitted, the content will be created in the same section specified by the archetype-name
.
Step 1: Require Arguments
First, ensure that at least 2 arguments are provided via stdin
. If not, output usage instructions and terminate the script.
1# Require at least 2 arguments
2if [ $# -lt 2 ]; then
3 echo "Usage: $0 <archetype-name> <section-name> <post-name>"
4 exit 1
5fi
Step 2: Conditional Variables
Next, store the first argument in a variable and conditionally omit the section if only 2 arguments are provided. Note that, if included, the section name is read in before the post name.
shell 1# Store the 1st argument in a variable
2archetype="$1"
3
4# Check if there are 3 arguments
5# If so, store both the post and section in variables
6if [ $# -eq 3 ]; then
7 section="$2"
8 post="$3"
9else
10 section=""
11 post="$2"
12fi
Step 3: Conditional Creation
Lastly, use the hugo new
command to generate the content based on the existence of the section.
1# Use the variables in the hugo new command
2if [ -n "$section" ]; then
3 hugo new --kind "$archetype" "$section/$post"
4else
5 hugo new --kind "$archetype" "$archetype/$post"
6fi
- The
$archetype
argument passed to the--kind
flag specifies the archetype used to create the bundle. - The
$section
can be a top-level section (e.g.posts
output tocontent/posts/
) or a subsection (e.g.posts/tutorials
output tocontent/posts/tutorials/
). - The
$post
is the name of your post. When creating a bundle, the output file will beindex.md
, but the directory name andtitle
itself will take on this argument’s value, assuming thetitle
field in your archetype is set to the default or similar:title: {{ replace .Name "-" " " | title }}
.
Sample Usage
Before running the script, give it executable permissions.
shell1chmod +x new.sh
To run it from the current directory:
shell1# Creates `content/posts/post/index.md`
2./new.sh posts post
3# Creates `content/blog/post/index.md` (using the posts archetype)
4./new.sh posts blog post
5# Creates `content/posts/tutorials/post/index.md`
6./new.sh posts posts/tutorials post
The Full Hugo Page Bundle Shell Script
That’s it! Like I said, nice and simple.
Feel free to copy the full script below and use it in your Hugo projects.
shell 1# Require at least 2 arguments
2if [ $# -lt 2 ]; then
3 echo "Usage: $0 <archetype-name> <section-name> <post-name>"
4 exit 1
5fi
6
7# Store the 1st argument in a variable
8archetype="$1"
9
10# Check if there are 3 arguments
11# If so, store both the post and section in variables
12if [ $# -eq 3 ]; then
13 section="$2"
14 post="$3"
15else
16 section=""
17 post="$2"
18fi
19
20# Use the variables in the hugo new command
21if [ -n "$section" ]; then
22 hugo new --kind "$archetype" "$section/$post"
23else
24 hugo new --kind "$archetype" "$archetype/$post"
25fi