website/articles/generate_listing.nu
2024-11-28 09:47:19 +01:00

33 lines
1.1 KiB
Plaintext
Executable file

#!/usr/bin/env nu
def get_metadata [path: string] {
let lines = open $path | lines
let anchors = $lines | enumerate | filter {|l| ($l.item | str starts-with "---")} | take 2
let header = $lines
| range (($anchors | first | get 'index') + 1)..(($anchors | last | get 'index') - 1)
let metadata = $header | split column -n 2 --regex '\s*:\s*' | rename key value
let record = $metadata | reduce -f {} {|it, acc| $acc | upsert $it.key ($it.value | str trim --char '"') }
$record |
}
let pages = (glob *.md) ++ (glob **/index.md)
let sorted_pages = $pages | wrap 'path'
| upsert metadata {|row| (get_metadata $row.path)}
| sort-by --reverse metadata.date
print "---
title: Articles
---
"
let _ = $sorted_pages | each {|p|
let rel_path = $p.path | path relative-to (pwd)
let html_path = $rel_path | path parse --extension md | upsert extension { 'html' } | path join
print --no-newline $"- ($p.metadata.date): [($p.metadata.title)]\(($html_path)\)"
if $p.metadata.update? != null {
print $" \(Updated: ($p.metadata.update)\)"
} else {
print ""
}
}