How to Create Hugo Posts
Creating a New Post File
Website posts published with Hugo are created as Markdown files in the content directory, for example sample.md. You can create a post file from an empty text file, but by using the hugo new command, you can automatically generate a Markdown file based on a post archetype file (archetypes/default.md).
Create a post file (Markdown file).
% hugo new sample.md
Content "/Users/user/my-site/content/sample.md" created
When you run it as above, the sample.md file is created in the content directory. At the beginning of the created file, a header called front matter is written as shown below. This header is automatically generated based on archetypes/default.md.
content/sample.md
---
title: "Sample"
date: 2023-05-05T23:50:09+09:00
draft: true
---
Then rewrite the title (title) as you like, and write the article body after the front matter header. When the article has reached a publishable level, delete the draft: true line in the header.
Archetypes
Now let’s look at the archetypes/default.md file that serves as the basis.
archetypes/default.md
---
title: {{ replace .TranslationBaseName "-" " " | title }}"
date: {{ .Date }}
draft: true
---
The title field is automatically filled with the name specified by the hugo new command, and the date field is automatically filled with the current time. From this, you can see that the post file is created based on the contents of this file.
Start Editing in an Editor While Creating a Post
When creating a post file with the hugo new command, specifying the --editor option opens the file in the specified editor at the same time the post file is created.
Example: Create an article file and open it in vim
% hugo new sample.md --editor vim
When you run it for the first time, you may see an error like the following.
% hugo new sample.md --editor vi
Content "/Users/user/hugo-tutorial/my-site/content/sample.md" created
Editing "/Users/user/hugo-tutorial/my-site/content/sample.md" with "vi" ...
Error: access denied: "vi" is not whitelisted in policy "security.exec.allow"; the current security configuration is:
[security]
enableInlineShortcodes = false
[security.exec]
allow = ['^dart-sass-embedded$', '^go$', '^npx$', '^postcss$']
osEnv = ['(?i)^((HTTPS?|NO)_PROXY|PATH(EXT)?|APPDATA|TE?MP|TERM)$']
[security.funcs]
getenv = ['^HUGO_']
[security.http]
methods = ['(?i)GET|POST']
urls = ['.*']
As the message indicates, copy the default policy into config.toml and add '^vim$' to the allow array.
[security]
enableInlineShortcodes = false
[security.exec]
allow = ['^dart-sass-embedded$', '^go$', '^npx$', '^postcss$', '^vim$']
osEnv = ['(?i)^((HTTPS?|NO)_PROXY|PATH(EXT)?|APPDATA|TE?MP|TERM)$']
[security.funcs]
getenv = ['^HUGO_']
[security.http]
methods = ['(?i)GET|POST']
urls = ['.*']
Then try again and it should run. However, because the post file is created before the editor opens, you may see a message saying the file already exists.
% hugo new sample.md --editor vim
Error: /Users/user/hugo-tutorial/my-site/content/sample.md already exists
In that case, delete the existing file and run the command again.