Featured image of post Defining A Task for Quickly Creating Hugo Posts In Visual Studio Code

Defining A Task for Quickly Creating Hugo Posts In Visual Studio Code

Using Visual Studio custom tasks, it is possible and easy to create tasks for quickly creating new content.

Now that I have custom tasks for building and serving my Hugo site in Visual Studio Code, I wanted to create posts quickly without having to remember the hugo command. So, this is what I did:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{
	"version": "2.0.0",
	"tasks": [
		{
			"label": "New Post",
			"type": "shell",
			"command": "title=\"${input:title}\" && slug=\"${title// /-}\" && hugo new content/post/${slug,,}/index.md --source ./src",
			"problemMatcher": []
		}
	],
	"inputs": [
		{
			"id": "title",
			"description": "Enter the title of the new post",
			"type": "promptString"
		}
	]
}

The Variable Substitution section of the Visual Studio docs provides information on getting input from the user.

First, I defined the inputs by providing the id, description, and type. Then the most interesting part:

The command:

1
title="${input:title}" && slug="${title// /-}" && hugo new content/post/${slug,,}/index.md --source ./src

does three (3) things using bash:

  1. Save the input text into a variable named title

    1
    
    title="${input:title}"
    
  2. Replace spaces in the title variable with dashes and store the output in another variable named slug:

    1
    
    slug="${title// /-}"
    
  3. Execute hugo command to create the content file:

    1
    
    hugo new content/post/${slug,,}/index.md --source ./src
    

    This last command converts the contents of the slug variable to lowercase. Pretty neat!

So far I’m pretty pleased with my experience with the Hugo static site generator.

Licensed under CC BY-NC-SA 4.0
Last updated on Mar 17, 2022 21:28 EST
Built with Hugo
Theme Stack designed by Jimmy