Fix all page

This commit is contained in:
2025-10-24 23:17:26 +08:00
parent db628efba8
commit 167ca8c811
6 changed files with 29 additions and 18 deletions

View File

@@ -57,7 +57,7 @@ async fn main() {
}
async fn index_handler(State(state): State<AppState>) -> impl IntoResponse {
match render_template("index.html", &state, Some(50)).await {
match render_template("index.html", &state).await {
Ok(html) => Html(html).into_response(),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e).into_response(),
}
@@ -67,7 +67,7 @@ async fn all_handler(State(state): State<AppState>, Path(page): Path<String>) ->
if page.contains("..") {
return (StatusCode::NOT_FOUND, "Invalid path").into_response();
}
match render_template(&format!("page_{}.html", page), &state, None).await {
match render_template(&format!("page_{}.html", page), &state).await {
Ok(html) => Html(html).into_response(),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e).into_response(),
}
@@ -97,14 +97,13 @@ async fn post_handler(
async fn render_template(
template_name: &str,
state: &AppState,
limit: Option<usize>,
) -> Result<String, String> {
let template_path = format!("templates/{}", template_name);
let template_content = std::fs::read_to_string(&template_path)
.map_err(|e| format!("Failed to read template: {}", e))?;
let manager = state.post_manager.read().await;
template_engine::render_template(&template_content, &*manager, None, limit)
template_engine::render_template(&template_content, &*manager, None)
}
async fn render_post_template(state: &AppState, post_name: String) -> Result<String, String> {
@@ -112,5 +111,5 @@ async fn render_post_template(state: &AppState, post_name: String) -> Result<Str
.map_err(|e| format!("Failed to read post template: {}", e))?;
let manager = state.post_manager.read().await;
template_engine::render_template(&template_content, &*manager, Some(&post_name), None)
template_engine::render_template(&template_content, &*manager, Some(&post_name))
}

View File

@@ -137,6 +137,9 @@ impl PostManager {
fn refresh_posts(&mut self) -> Result<(), String> {
self.posts.clear();
// Get timestamps from git history
let git_timestamps = self.get_post_timestamps()?;
let entries = fs::read_dir(&self.posts_dir)
.map_err(|e| format!("Failed to read posts directory: {}", e))?;
@@ -162,14 +165,19 @@ impl PostManager {
let html_content = markdown_to_html(&markdown_content);
// Mock creation date for now
let metadata = fs::metadata(&path)
.map_err(|e| format!("Failed to read file metadata: {}", e))?;
let created_at = metadata
// Use git timestamp if available, otherwise fall back to file metadata
let created_at = git_timestamps
.get(&name)
.copied()
.or_else(|| {
let metadata = fs::metadata(&path).ok()?;
metadata
.created()
.or_else(|_| metadata.modified())
.ok()
.map(|t| DateTime::<Utc>::from(t))
.unwrap_or_else(|_| Utc::now());
})
.unwrap_or_else(|| Utc::now());
let post = Post {
name: name.clone(),

View File

@@ -6,7 +6,6 @@ pub fn render_template(
template: &str,
post_manager: &PostManager,
current_post: Option<&str>,
limit_override: Option<usize>,
) -> Result<String, String> {
let mut result = template.to_string();
@@ -19,7 +18,7 @@ pub fn render_template(
}
// Process post-list tag
result = process_post_list(&result, post_manager, limit_override)?;
result = process_post_list(&result, post_manager)?;
Ok(result)
}
@@ -75,7 +74,6 @@ fn process_post_html(
fn process_post_list(
template: &str,
post_manager: &PostManager,
limit_override: Option<usize>,
) -> Result<String, String> {
let post_list_regex = Regex::new(r#"\$<post-list(?:\s+limit=(\d+))?\s*/>"#).unwrap();
@@ -85,7 +83,7 @@ fn process_post_list(
let full_match = cap.get(0).unwrap().as_str();
let limit_attr = cap.get(1).and_then(|m| m.as_str().parse::<usize>().ok());
let limit = limit_override.or(limit_attr);
let limit = limit_attr;
let posts = if let Some(l) = limit {
post_manager.get_posts_limited(l)

View File

@@ -18,6 +18,11 @@
padding-bottom: 10px;
margin-bottom: 30px;
}
nav span {
font-size: 1.2em;
font-weight: bold;
margin-right: 20px;
}
nav a {
margin-right: 20px;
text-decoration: none;
@@ -73,6 +78,7 @@
</head>
<body>
<nav>
<span>(dis)gus' things</span>
<a href="/">Home</a>
<a href="/all">All Posts</a>
</nav>

View File

@@ -1,4 +1,4 @@
$<include src="header.html"/>
<h1>Recent Posts</h1>
$<post-list limit=50/>
$<post-list limit=2/>
$<include src="footer.html"/>