Howto: Convert your front matter from TOML to YAML

… or the other way around.

TOML is great, but the big plus for YAML for front matter is that GitHub renders it nicely – so the content also looks good when browsing it there.

Of course, when I realized this I already had a site with lots of TOML.

Here is how I did the conversion. DISCLAIMER: Use as is, take backup of the content before running this (git …), but it worked great for me.

  1. Build https://github.com/dbohdan/remarshal and put it on your path (go get, go build …)
  2. From your site’s root, run the following hairy one-liner:

find content -name "*.md" | while read i; do TOML=`sed -n '/+++/,/+++/p' $i | sed '1d; $d' | remarshal -if toml -of yaml`; perl -pi -e "BEGIN{undef $/;} s/\+\+\+.*\+\+\+/---\n$TOML\n---/gs" $i; done
7 Likes

You can also do using the hugo convert command.

$ hugo help convert
Convert will modify your content to different formats

Usage: 
  hugo convert [command]

Available Commands: 
  toJSON                    Convert front matter to JSON
  toTOML                    Convert front matter to TOML
  toYAML                    Convert front matter to YAML

$ hugo convert toYAML --output content_as_yaml
processing 18 content files

However there appears to be a bug where it ignores how markdown files were organized in the content folder. Example:

$ ls content
about.md
bio.md
contact.md
gallery/
process/

$ ls content_as_yaml
about.md
contact.md
harvesting.md
fibre.md
bio.md
drying.md
weaving.md

I think it might have something to do with this line being commented out: https://github.com/spf13/hugo/blob/master/commands/convert.go#L131

1 Like

If the built in Convert converts AND moves my content around in a random fashion, it’s not really something I want to use.

I didn’t know of that built in feature, I have added this issue: https://github.com/spf13/hugo/issues/643

Thanks for sharing @bjornerik, might be a good idea to add a note about converting archtypes also to YAML in the example in your OP. :slight_smile:

And, yeah the default frontmatter format to be used in config.toml should also be changed for those switching to YAML:

MetaDataFormat = "yaml"

I tried both command but config.toml not converted to YAML. What’s wrong with it

>hugo convert toYAML --unsafe
>hugo convert toYAML --config string --unsafe

processing 3 content files
C:\Hugo\Sites\learn>dir
01/08/2018  16:22    <DIR>          .
01/08/2018  16:22    <DIR>          ..
27/07/2018  15:45    <DIR>          archetypes
27/07/2018  15:45                82 config.toml
27/07/2018  15:45    <DIR>          content
27/07/2018  15:45    <DIR>          data
27/07/2018  15:45    <DIR>          layouts
01/08/2018  16:22    <DIR>          resources
27/07/2018  15:45    <DIR>          static
01/08/2018  16:35    <DIR>          themes

Hi @Imjtrial, the Hugo subcommand convert toYAML converts only the front matter of your files under the content/ directory.

config.toml is left untouched.

Would it be a good new feature to add? I don’t know. :wink:

Many thanks I think this is left out for a purpose.

The cleanest, easiest and fastest SSG on the planet is still difficult for non-technical people XD.