Skip to main content

VSCode tasks for Hugo

·285 words·2 mins
Bemn
Author
Bemn
Hong Konger.
Table of Contents

This post shows some useful VSCode tasks I set up to run and build Hugo.

Create a tasks.json under .vscode. Launch .vscode/tasks.json and make sure that you have version, tasks and input :

{
    "version": "2.0.0",
    "tasks": [],
    "input": []
}

1. Launch Hugo Dev Server
#

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Serve Drafts",
            "type": "shell",
            "command": "hugo",
            "args": ["server", "-D"],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "isBackground": true,
            "options": {
                "cwd": "${workspaceFolder}/blog"
            }
        }
    ]
}

Note: change the working directory when necessary. ${workspaceFolder} is the directory contains .vscode folder

2. Launch a Mac Application
#

For example, I launch Mark Text in mac:

{
    "label": "Launch Mark Text",
    "type": "shell",
    "command": "open",
    "args": [
        "-a",
        "\"Mark Text\"",
        "${workspaceFolder}/blog/content/posts",
        "&"
    ],
    "group": {
        "kind": "build",
        "isDefault": false
    },
    "isBackground": true,
    "options": {
        "cwd": "${workspaceFolder}/blog"
    }
}

3. Launch Script Requires User Input
#

For example, creating a new post requires user input the file name:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "New Post",
            "type": "shell",
            "command": "hugo",
            "args": [
                "new",
                "posts/${input:postTitlePrompt}/index.md"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "options": {
                "cwd": "${workspaceFolder}/blog"
            },
            "isBackground": true,
            "problemMatcher": []
        },
    ],
    "input": [
        {
            "id": "postTitlePrompt",
            "description": "Title of the new post",
            "type": "promptString"
        }
    ]
}

In this case, we have to create a input task to collect user’s input first, and pass that value to the task.

4. Generate Hugo Static Site
#

Similar to the launc the dev task, we create a task to run hugo build:

{
    "label": "Generate Static Site",
    "type": "shell",
    "command": "hugo",
    "group": {
        "kind": "build"
    },
    "options": {
        "cwd": "${workspaceFolder}/blog"
    }
}

Choose the task to run by Command + Shift + B:

Related

Displaying the Full Content in Hugo's RSS feed
·481 words·3 mins
The default RSS feed in Hugo site shows article abstract only. But what should I do if I want to display full content of the articles in the feed?
How to add a new Hugo blog post
·43 words·1 min
Go to blog folder
🐞 Missing Cookie in iframe
·653 words·4 mins
Adding an config in IIS to fix my iframe content.