Hugo Helpers




I have four bash scripts that I use when working with my Hugo-based website.

drafts

Displays all content that have the keyword draft:

function drafts() {
  grep -ril content/ -e "^draft:"
}

deploy

Uses rsync to transfer the contents of public/ to my host
deploy defaults to running rsync with the --dry-run flag set. In order to actually deploy the site the --go flag has to be included.

function deploy() {
  local dry_run="--dry-run"
  if [[ "$1" == "--go" ]]; then
      dry_run=""
  fi
  rsync -azr --delete --include='.*' --info=name,stats2 "$dry_run" public/ pair:~/public_html/zanshin
}

build

Rebuilds the site into public/

function build() {
  hugo --cleanDestinationDir
}

create

Creates new content
create is by far the most complicated of these helper scripts. With four archetypes active on my site, the create command expects a type as well as a title. The type is converted to the correct archetype before it is used. The title is formatted to be all lowercase, and to replace alphanumeric words with a dash, and to compress repeated dashes to a single instance.

function create() {
    # create [post|link|video|note] <title>
    local type="$1"
    local title="$2"

    case "$type" in
        post)  local archetype="posts" ;;
        link)  local archetype="links" ;;
        video) local archetype="videos" ;;
        note)  local archetype="notes" ;;
        *)     echo "Unknown type: $type"; return 1 ;;
    esac

    # from https://onebite.dev/create-new-hugo-post-faster-with-bash/
    # replace alphanumeric words with a dash, compress repeated dashes to a single
    # dash, replace capital letters with lower case
    post=$(echo "$title" | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr '[:upper:]' '[:lower:]').md
    hugo new "$archetype"/"$post"
    nvim content/"$archetype"/"$post"
}