update article main page generator
This commit is contained in:
parent
70005c9228
commit
45ce87e6b1
3 changed files with 33 additions and 24 deletions
32
articles/generate_listing.nu
Executable file
32
articles/generate_listing.nu
Executable file
|
@ -0,0 +1,32 @@
|
|||
#!/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 ""
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue