Fixup of Missing Markdown Date Front Matter From Git with Nushell
Fixup of Missing Markdown Date Front Matter From Git with Nushell
My website is implemented through Hugo, with content sources in Markdown. Metadata is added through a so-called "front matter" header within Markdown files. I noticed date metadata (front matter) was missing on content pages and consequently had 2001 on RSS feeds. I used Nushell to en-mass add page dates after-the-fact, with date values determined through Git.
nu
# Determine pages with missing date front matter (may be missing pages that have `date = ` as content)
glob **/*.md | where {|x| $x | open | not ($in | str contains 'date = ') } | save missing.json
# Determine content creation dates through Git add authoring date
open missing.json | wrap path | upsert date {|x| git log '--follow' '--diff-filter=A' '--format=%ad' '--date=iso' '--' $x.path | into datetime } | save dates.json
# Prepend date TOML front matter to closing fence
open dates.json | each {|x| $x.path | open --raw | str replace "\r\n+++\r\n" $"\r\ndate = \"($x.date | format date '%Y-%m-%d %H:%M:%S')\"\r\n+++\r\n" | collect | save -f $x.path }Example [inline] result/fixup:
TOML
date = "2022-08-07 18:31:18" +++
Some work details and manual cleanup (e.g. pages with resource front matter where the date declaration must be placed before them) omitted.
Comments