website/articles/generate_listing.nu
2024-11-27 10:09:08 +01:00

41 lines
1.2 KiB
Plaintext
Executable file

#!/usr/bin/env nu
#
def title [path: string] -> string {
open $path | lines | filter {|l| (str starts-with "title:")} | first | split row --regex 'title\s*:\s*' | get 1 | str trim --char "\""
}
def creation_date [path: string] -> string {
open $path | lines | filter {|l| (str starts-with "date:")} | first | split row --regex '\s*:\s*' | get 1
}
def update_date [path: string] -> string? {
let updates = open $path | lines | filter {|l| (str starts-with "update:")}
if ($updates | length) > 0 {
return ($updates | first | split row --regex '\s*:\s*' | get 1)
} else {
return null
}
}
let pages = (glob *.md) ++ (glob **/index.md)
let sorted_pages = $pages | wrap 'path'
| upsert title {|row| (title $row.path)}
| upsert creation {|row| (creation_date $row.path)}
| upsert update {|row| (update_date $row.path)}
| sort-by --reverse creation
print "---
title: Articles
---
"
$sorted_pages | each {|p|
let rel_path = $p.path | path relative-to (pwd)
print --no-newline $"- ($p.creation): [($p.title)]\(($rel_path)\)"
if $p.update? != null {
print $" \(Updated: ($p.update)\)"
} else {
print ""
}
}