Update content files based on new fields in archetype

I was wondering if it is possible to automatically update my content (Markdown) files whenever I add new fields to its correspondig archetype.

A simple example: I have blog/post123.md with this content:

title: Blah
date: 2018-02-12T16:52:50+01:00
draft: true

# Hello! This is a blog post

Then, later on, I decide to add an extra field to archetypes/blog.md:

title: Blah
date: 2018-02-12T16:52:50+01:00
draft: true
newsletter: true

In layouts/blog.html I use this boolean value to decide whether I want to show my newsletter signup form on that specific blog post or not.

Now, I wish this new ‘default’ field would be added to all existing content automatically (with its default value of false) — not only to new content that I create with hugo new blog/another.md), but also for existing content. Now I manually add these new fields for each content file, but I’m not sure if I’m supposed to be doing that.

This isn’t an inbuilt way to do this, but you can rely on command-line “magic”… Use sed / python / perl / etc. to batch-add that new front-matter to all your posts. I do something like that using Emacs-Lisp… I manage the content in Org mode, and batch write all the Markdown files.

Okay, thanks. That doesn’t sound easy. My regex skills are very rusty, and I think I really need them for this sort of file modification ‘magic’, as you call it.

I wish this functionality was built-in, indeed.

I think we need a tool that can manipulate (and in bulk) front matter. But I don’t think Hugo is the program for it.

We ought to keep Hugo focused on parsing and handling advanced content layouts.

Lots of projects use front matter, so a general tool, which lightens the load on regex research before using, that would be great! :slight_smile:

You convinced me; this should not be part of Hugo itself. I found this project and I think it might do just want I need. Has anyone experience with this tool?

When converting my content to page bundles I had this problem. Solved it mostly by find-and-replace on sublimeText, worked well enough for over 1000 files.

I hate it when people answer to my problem with a totally different approach, so I guess i’m just curious.

As I understand the need to be able to update mutliple front matter with a script, regarding your current predicament (providing it was not just an illustrative exemple), do you really need that key on false ? Can’t its mere absence suffice?

Well, the true / false example was just an example, but in reality my archetypes are more complex and also hold other default values. For example:

title                : {{ replace .TranslationBaseName "-" " " | title }}
categories           : ["default"]
date                 : {{ .Date }}
draft                : true
weight               : 0
author               : Roel Van Gils
teaser               : This page doesn't have a teaser
illustration         : 
    name             : desciption
    content          : 
header               :
callout              :
    heading          :
    body             : 
background           :
    image            : default.jpg
    position         : center center
    size             : cover

The front matter is either TOML, YAML or JSON which can be manipulated in many scripting / programming languages (although TOML support is lacking in most). I’ve recently converted all my front matter from TOML to YAML for that very reason.

Once it’s converted it’s easy to script any changes. But find and replace is just as quick for small changes :slight_smile: