Note from the future: A few months later, I came up with what is perhaps a better way to do this.
Perhaps you’ve just had some inspiration and want to get started with a new post in your favorite static site generator (SSG). In just about every SSG except Hugo, you usually end up doing the following:
- Finding an earlier post’s Markdown file.
- Copying the file’s front matter and perhaps a word or two.
- Creating the new post’s Markdown file.
- Pasting the copied text into the new post.
- Editing the copied front matter so you don’t accidentally leave it the same as the earlier post’s.
But, in Hugo, you can take advantage of its archetypes, about which I wrote for the CloudCannon Blog several months ago. With a Hugo archetype set correctly, you can turn those steps above into a simple one-line command:
hugo new posts/2022/09/hugo-like-archetypes-other-ssgs/index.md
. . . instantly creating:
content/
└─ posts/
└─ 2022/
└─ 09/
├── hugo-like-archetypes-other-ssgs/
│ ├── index.md
This works because, in this example, I already have an archetypes/posts.md
file with the following content:
posts.md
---
title: "TITLE TO COME"
description: "DESCRIPTION TO COME."
author: Bryce Wray
date: {{ .Date }}
draft: true
---
Text begins here.
However, for those of you using other SSGs, you can do something very similar to this neat shortcut, and without a lot of trouble.
Let’s say your SSG’s main files live in a src
directory at the project’s top level. This is a standard convention among SSGs. Create a top-level archetypes/
folder and put within it a file, posts.md
, with the following content:
posts.md
---
title: "TITLE TO COME"
description: "DESCRIPTION TO COME."
author: # fill in your name
draft: true
date: 2022-05-01T11:00:00-05:00
---
Text begins here.
- For
author
, substitute your name for my# fill in your name
comment. - The
draft
item may not necessarily mean anything to your SSG but, in case you are using an SSG which distinguishes between drafts and ready-to-publish files, I includeddraft
for safety’s sake (as a Hugo archetype typically does) so you don’t accidentally post anything before you’re ready. - As for
date
, use a placeholder that works for your date and time zone.
Note to Windows users: From here on, you’re probably best advised to use WSL.
Then, in your project’s top level, create a shell script:
newfile.sh
#!/bin/sh
echo "Provide path/filename in the format: posts/yyyy/mm/filename.md"
read filepath
cp -v "archetypes/posts.md" "src/$filepath"
Of course, feel free to make the cp -v
line conform to how your site and SSG arrange content. (For a typical Eleventy or Jekyll site, src/$filepath
should work fine. However, SSGs like Astro and Next.js probably expect src/pages/$filepath
. If necessary, consult your SSG’s documentation to make sure you know the right place to target.)
Finally, give newfile.sh
the necessary permissions:
chmod 0755 newfile.sh
Done! Now, when you want to create a new Markdown file, just enter:
./newfile.sh
The resulting prompt will ask for a path and filename in the proper format (which you can, of course, tailor to the arrangement of your own site’s content). If you then enter posts/2022/09/my-post.md
, the script will copy the contents of archetypes/post.md
to a new file, src/posts/2022/09/my-post.md
. Then, all you have to do is open that file, quickly edit the front matter as desired for the new post, and start writing!
Latest commit (caac9a97
) for page file:
2023-04-06 at 3:24:25 PM CDT.
Page history