Is this a bug? Every time I use the command “hugo new blog/newpost.md” the order of the front matter is always different for each article. It’s a bit annoying.
For example:
stoned@stoned-desktop:~/media/websites/psychedelicsdaily.com$ hugo new news/Test_frontmatter_order.md
ERROR 2017/03/25 09:48:32 No 'baseURL' set in configuration or as a flag. Features like page menus will not work without one.
/home/stoned/media/websites/psychedelicsdaily.com/content/news/Test_frontmatter_order.md created
stoned@stoned-desktop:~/media/websites/psychedelicsdaily.com$ hugo new news/Test_frontmatter_order_2.md
ERROR 2017/03/25 09:50:09 No 'baseURL' set in configuration or as a flag. Features like page menus will not work without one.
/home/stoned/media/websites/psychedelicsdaily.com/content/news/Test_frontmatter_order_2.md created
stoned@stoned-desktop:~/media/websites/psychedelicsdaily.com$ hugo new news/Test_frontmatter_order_3.md
ERROR 2017/03/25 09:50:13 No 'baseURL' set in configuration or as a flag. Features like page menus will not work without one.
/home/stoned/media/websites/psychedelicsdaily.com/content/news/Test_frontmatter_order_3.md created
stoned@stoned-desktop:~/media/websites/psychedelicsdaily.com$ cat /home/stoned/media/websites/psychedelicsdaily.com/content/news/Test_frontmatter_order.md /home/stoned/media/websites/psychedelicsdaily.com/content/news/Test_frontmatter_order_2.md /home/stoned/media/websites/psychedelicsdaily.com/content/news/Test_frontmatter_order_3.md
+++
title = "Test_frontmatter_order"
banner = ""
description = ""
featured = ""
author = ""
images = ["https://i.imgur.com/"]
tags = [""]
categories = ["Psychedelics"]
authors = ""
date = "2017-03-25T09:48:32-06:00"
type = "article"
news_keywords = [""]
+++
+++
tags = [""]
date = "2017-03-25T09:50:09-06:00"
title = "Test_frontmatter_order_2"
type = "article"
banner = ""
news_keywords = [""]
images = ["https://i.imgur.com/"]
description = ""
categories = ["Psychedelics"]
featured = ""
author = ""
authors = ""
+++
+++
featured = ""
images = ["https://i.imgur.com/"]
description = ""
type = "article"
banner = ""
author = ""
news_keywords = [""]
tags = [""]
date = "2017-03-25T09:50:13-06:00"
title = "Test_frontmatter_order_3"
categories = ["Psychedelics"]
authors = ""
+++
stoned@stoned-desktop:~/media/websites/psychedelicsdaily.com$
Is there any way to have them in any certain order? Please advice, thank you!
keys := make([]string, 0, len(metadata))
for key := range metadata {
keys = append(keys, key)
}
sort.Strings(keys)
return keys, nil
And upon rebuilding the hugo program, I get the following output:
stoned@stoned-desktop:~/code/golang/src/github.com/spf13/hugo$ make hugo
go get github.com/kardianos/govendor
govendor sync github.com/spf13/hugo
go build -ldflags "-X github.com/spf13/hugo/hugolib.CommitHash=`git rev-parse --short HEAD 2>/dev/null` -X github.com/spf13/hugo/hugolib.BuildDate=`date +%FT%T%z`" github.com/spf13/hugo
# github.com/spf13/hugo/create
create/content.go:151: cannot use keys (type []string) as type map[string]interface {} in return argument
Makefile:17: recipe for target 'hugo' failed
make: *** [hugo] Error 2
stoned@stoned-desktop:~/code/golang/src/github.com/spf13/hugo$
If I understand golang so far, the function has a type of “map[string]interface” and metadata has been casted to a ToStringMapE, and that the keys slice/array I am using to sort the metadata, is a type string, so returning type string where the function type is stringmap interface…
I’m a little confused here about the typing. Could you clarify further.
I would love to contribute to Hugo and offer this patch. Thank you, sir.
I have forked and pushed my changed up to the HashBorgir branch. I’m just testing the sorting for now, but I’m sure I’ll end up creating a sort function and calling that within createMetadata and if I need to make a unit test for it, I’ll do so as well.
For now, I’m learning golang and trying to figure all this out.
Please advise about the return type mismatch. Thank you!
Sorting will give constistent ordering, but it will still re-arrange the items. So the proposed solution is to just use the archetype frontmatter as-is, and insert missing pieces where they belong (date etc.).