[CLI] Ability to manipulate frontmatter for a document

Use case or problem

I would like to be able to add not just tags, but manipulate the frontmatter to documents directly via the CLI. I am trying to automate some features for the dataview.

Proposed solution

Having a set of new commands on the CLI tools similar to the current tags tool, that let’s me:

  • Add new fields
  • Assign values to fields
  • view field types as a schema
  • Upsert to fields that are lists
  • trigger reformat

Current workaround (optional)

Currently, I just find the first two occurrences of ---, load with a yaml library, then manipulate the file directly. It’s doable, but a pain when I could just upsert the new fields I’d like directly.

Read this

EDIT: I just saw WhiteNoise’s reply above; I didn’t realize the CLI could already do this. Neat!

A workaround I use is yq, a command line tool for manipulating YAML files (and JSON, XML and more).

It’s relatively easy to use yq to, say, mass-update a frontmatter field, delete a field, or manipulate list fields.

For example, here’s a bash command to update all the md files in a directory and rename a field from “topics” to “related”. It does this by setting the “related” field and then deleting the “topics” field.

find . -name "*.md" \
    -exec yq --inplace --prettyPrint --front-matter=process \
             '.related = .topics | del(.topics)' {} \;