Front matter order is always randomized

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. :stuck_out_tongue:

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! :slightly_smiling:

@Hash_Borgir this is a known issue and called out specifically in the new docs concept site as well:

https://hugodocs.info/content-management/archetypes/#using-the-default-archetype

I would search around the forums as well, but just for context. To my knowledge, there is no current workaround for the order of FM in archetypes.

There is an issue about fixing it; we will, but someone has to do it.

In this file, just to test for myself, (I’m really new to golang, and I am having a problem w/ types here)

https://gist.github.com/HashBorgir/99d85e30f10ff617270c894f50110ea7

I have added the following bit:

    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! :slightly_smiling:

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.).

Why would it still re-arrange the items? Can the final result not be sorted?

If I have a archetype file:

title = "asdf"
date = "2018-01-01"

And you sort it by key, it will be re-arranged.

So, there are two issues here:

  1. The randomness
  2. That we rearrange the items

If we fix 2), we also fix 1)