Title predicament

Hi there!

I have used Jekyll in the past, so all my blog entries are in this form (example):

2017-12-10-this-is-an-example.md

Is there a way to use hugo new that will only use (still using example above) This is an example as title, while preserving the current filename? Right now, it will place "2017-12-10-This-Is-an-Example" as title.

I’m not sure if this is completely relevant to your problem. The hugo new command uses archetypes (you can search the docs site or this forum for various pages and posts on this), into which you can insert template codes.

Here’s an example. In /my/project/archetypes I have a post.md that looks like:

+++
date = {{ .Date }}
title = "{{ replace .TranslationBaseName "-" " " | title }}"
slug = "{{ .TranslationBaseName | lower }}"
tags = ["x", "y"]
draft = "true"
+++

Intro

<!--more-->

Body

I use a zsh function to script creation of a post so that it looks how I want:

hugogenpost () {
	if [[ -z $1 ]]
	then
		echo "A double-quoted post title is required"
		exit 1
	fi
	_hugobin="$HOME/path/to/my/hugo"
	_workingdir="$HOME/hugo/projects/projectA"
	_contentdir="${_workingdir}/content/post"
	_hytitle=$(echo $1 | tr ' ' '-')
	_hytitlelower=$(echo ${_hytitle} | tr "[:upper:]" "[:lower:]")
	_hytitlelowerquoted=$(echo \"$_hytitlelower\")
	_date=$(date +'%Y-%m-%d')
	_datetime=$(date +'%Y-%m-%d %H:%M:%S')
	_contentfile="${_date}-${_hytitle}.md"
	cd ${_workingdir}
	${_hugobin} new post/${_hytitle}.md
	mv -i ${_contentdir}/${_hytitle}.md ${_contentdir}/${_contentfile}
	$EDITOR ${_contentdir}/${_contentfile}
}

I use it like this:

 ~> hugogenpost "My Latest Pithy Title"

… and the post will open in my editor, set in the $EDITOR env var.

You might be able to adapt this approach.

FYI my hugo env, noting that the above is still working in this environment:

Hugo Static Site Generator v0.32-DEV-8A8BA63C darwin/amd64 BuildDate: 2017-11-27T21:40:02+09:00
GOOS="darwin"
GOARCH="amd64"
GOVERSION="go1.9.2"
2 Likes

Excellent. I can use your approach, and modify it to fit my needs. Thanks!

1 Like

@Fastidious You can also change the auto-generated templating that Hugo provides in archetypes/default.md. For your use case, change the following:

title: "{{ replace .TranslationBaseName "-" " " | title }}"

To…

title: "{{(replace (slicestr .TranslationBaseName 11) "-" " ") | title}}"

@RickCogley 's example works too, but you don’t need to get into an BASH/ZSH scripting to get the desired result.

2 Likes

Oh, that is cleaner, and so easy! Thanks a bunch mate!

Thanks @rdwatters. I’ll have to learn more about those native functions like slicestr and see if I cannot do it all within Hugo.

1 Like