fix cmd
This commit is contained in:
@@ -2,6 +2,7 @@ use anyhow::Result;
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use serde_json;
|
||||||
|
|
||||||
/// Rebuild index.json files for all collections in content directory
|
/// Rebuild index.json files for all collections in content directory
|
||||||
pub fn run(content_dir: &Path) -> Result<()> {
|
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)
|
// Collect all rkeys from .json files (excluding special files)
|
||||||
let mut rkeys: Vec<String> = Vec::new();
|
let mut rkeys: Vec<String> = Vec::new();
|
||||||
let mut rkey_times: HashMap<String, std::time::SystemTime> = HashMap::new();
|
let mut rkey_dates: HashMap<String, String> = HashMap::new();
|
||||||
|
|
||||||
for file_entry in fs::read_dir(&col_path)? {
|
for file_entry in fs::read_dir(&col_path)? {
|
||||||
let file_entry = file_entry?;
|
let file_entry = file_entry?;
|
||||||
@@ -71,10 +72,15 @@ pub fn run(content_dir: &Path) -> Result<()> {
|
|||||||
// Extract rkey from filename
|
// Extract rkey from filename
|
||||||
let rkey = filename.trim_end_matches(".json").to_string();
|
let rkey = filename.trim_end_matches(".json").to_string();
|
||||||
|
|
||||||
// Get file modification time for sorting
|
// Read publishedAt from JSON for chronological sorting
|
||||||
if let Ok(metadata) = file_entry.metadata() {
|
if let Ok(content) = fs::read_to_string(&file_path) {
|
||||||
if let Ok(modified) = metadata.modified() {
|
if let Ok(json) = serde_json::from_str::<serde_json::Value>(&content) {
|
||||||
rkey_times.insert(rkey.clone(), modified);
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort by modification time (oldest first) or alphabetically
|
// Sort by publishedAt (oldest first), fallback to rkey string comparison
|
||||||
rkeys.sort_by(|a, b| {
|
rkeys.sort_by(|a, b| {
|
||||||
match (rkey_times.get(a), rkey_times.get(b)) {
|
match (rkey_dates.get(a), rkey_dates.get(b)) {
|
||||||
(Some(ta), Some(tb)) => ta.cmp(tb),
|
(Some(da), Some(db)) => da.cmp(db),
|
||||||
_ => a.cmp(b),
|
(Some(_), None) => std::cmp::Ordering::Less,
|
||||||
|
(None, Some(_)) => std::cmp::Ordering::Greater,
|
||||||
|
(None, None) => a.cmp(b),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user