From ddbb60cd48d94aa65e67572b9596a5fccf7b59b1 Mon Sep 17 00:00:00 2001 From: syui Date: Sun, 22 Mar 2026 09:50:41 +0900 Subject: [PATCH] fix cmd --- src/commands/index.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/commands/index.rs b/src/commands/index.rs index 419b57e..11b0ba2 100644 --- a/src/commands/index.rs +++ b/src/commands/index.rs @@ -2,6 +2,7 @@ use anyhow::Result; use std::collections::HashMap; use std::fs; use std::path::Path; +use serde_json; /// Rebuild index.json files for all collections in content directory pub fn run(content_dir: &Path) -> Result<()> { @@ -46,7 +47,7 @@ pub fn run(content_dir: &Path) -> Result<()> { // Collect all rkeys from .json files (excluding special files) let mut rkeys: Vec = Vec::new(); - let mut rkey_times: HashMap = HashMap::new(); + let mut rkey_dates: HashMap = HashMap::new(); for file_entry in fs::read_dir(&col_path)? { let file_entry = file_entry?; @@ -71,10 +72,15 @@ pub fn run(content_dir: &Path) -> Result<()> { // Extract rkey from filename let rkey = filename.trim_end_matches(".json").to_string(); - // Get file modification time for sorting - if let Ok(metadata) = file_entry.metadata() { - if let Ok(modified) = metadata.modified() { - rkey_times.insert(rkey.clone(), modified); + // Read publishedAt from JSON for chronological sorting + if let Ok(content) = fs::read_to_string(&file_path) { + if let Ok(json) = serde_json::from_str::(&content) { + if let Some(date) = json.pointer("/value/publishedAt") + .or_else(|| json.get("publishedAt")) + .and_then(|v| v.as_str()) + { + rkey_dates.insert(rkey.clone(), date.to_string()); + } } } @@ -85,11 +91,13 @@ pub fn run(content_dir: &Path) -> Result<()> { continue; } - // Sort by modification time (oldest first) or alphabetically + // Sort by publishedAt (oldest first), fallback to rkey string comparison rkeys.sort_by(|a, b| { - match (rkey_times.get(a), rkey_times.get(b)) { - (Some(ta), Some(tb)) => ta.cmp(tb), - _ => a.cmp(b), + match (rkey_dates.get(a), rkey_dates.get(b)) { + (Some(da), Some(db)) => da.cmp(db), + (Some(_), None) => std::cmp::Ordering::Less, + (None, Some(_)) => std::cmp::Ordering::Greater, + (None, None) => a.cmp(b), } });