A script to add front matter to pages

#1

I recently had to add toc: false to all my blog posts. Scripted it to save time. Sharing in case useful for others :man_shrugging:

Notes:

  • It works by finding the 2nd occurrence of the front matter separator and then inserting the param right above it
  • It’s run from the root of your site
  • Set front_matter to the param you want to add
  • Set dir to your blog posts location
  • If using TOML, find-n-replace all occurrences of --- with +++
#!/bin/bash

front_matter="toc: false"
dir="content/post"
pages="$(find $dir -name "*.md")"
tmp="tmp.md"

for page in $pages; do 
  awk -v front_matter="$front_matter" '/---/{
    c++;
    if (c==2) {
      sub("---",front_matter "\n---")
    }
  } 1' $page > $tmp
  mv $tmp $page
done

8 Likes
#2

good very good