fixed code highlight

This commit is contained in:
2026-03-26 01:32:17 +01:00
parent 7296638091
commit 53db329cc5
5 changed files with 116 additions and 54 deletions

View File

@@ -27,7 +27,17 @@ pub async fn create_post(
let file_path = state.data_dir.join("posts").join(format!("{}.md", payload.slug));
fs::write(&file_path, &payload.content).map_err(|e| {
let mut file_content = String::new();
if let Some(ref summary) = payload.summary {
if !summary.trim().is_empty() {
file_content.push_str("---\nsummary: ");
file_content.push_str(&summary.replace('\n', " "));
file_content.push_str("\n---\n");
}
}
file_content.push_str(&payload.content);
fs::write(&file_path, &file_content).map_err(|e| {
error!("Write error for post {}: {}", payload.slug, e);
AppError::Internal("Write error".to_string(), Some(e.to_string()))
})?;
@@ -35,6 +45,7 @@ pub async fn create_post(
info!("Post created/updated: {}", payload.slug);
Ok(Json(PostDetail {
slug: payload.slug,
summary: payload.summary,
content: payload.content,
}))
}
@@ -74,10 +85,27 @@ pub async fn list_posts(State(state): State<Arc<AppState>>) -> impl IntoResponse
if let Some(slug) = path.file_stem().and_then(|s| s.to_str()) {
let mut excerpt = String::new();
if let Ok(content) = fs::read_to_string(&path) {
let clean_content = content.replace("#", "").replace("\n", " ");
excerpt = clean_content.chars().take(200).collect::<String>();
if clean_content.len() > 200 {
excerpt.push_str("...");
if content.starts_with("---\n") {
let parts: Vec<&str> = content.splitn(3, "---\n").collect();
if parts.len() == 3 {
let frontmatter = parts[1];
for line in frontmatter.lines() {
if line.starts_with("summary: ") {
excerpt = line.trim_start_matches("summary: ").to_string();
break;
}
}
if excerpt.is_empty() {
let body = parts[2];
let clean_content = body.replace("#", "").replace("\n", " ");
excerpt = clean_content.chars().take(200).collect::<String>();
if clean_content.len() > 200 { excerpt.push_str("..."); }
}
}
} else {
let clean_content = content.replace("#", "").replace("\n", " ");
excerpt = clean_content.chars().take(200).collect::<String>();
if clean_content.len() > 200 { excerpt.push_str("..."); }
}
}
posts.push(PostInfo {
@@ -99,7 +127,26 @@ pub async fn get_post(
let file_path = state.data_dir.join("posts").join(format!("{}.md", slug));
match fs::read_to_string(&file_path) {
Ok(content) => Ok(Json(PostDetail { slug, content })),
Ok(raw_content) => {
let mut summary = None;
let mut content = raw_content.clone();
if raw_content.starts_with("---\n") {
let parts: Vec<&str> = raw_content.splitn(3, "---\n").collect();
if parts.len() == 3 {
let frontmatter = parts[1];
for line in frontmatter.lines() {
if line.starts_with("summary: ") {
summary = Some(line.trim_start_matches("summary: ").to_string());
break;
}
}
content = parts[2].to_string();
}
}
Ok(Json(PostDetail { slug, summary, content }))
},
Err(_) => Err(AppError::NotFound("Post not found".to_string())),
}
}

View File

@@ -36,12 +36,14 @@ pub struct PostInfo {
#[derive(Serialize)]
pub struct PostDetail {
pub slug: String,
pub summary: Option<String>,
pub content: String,
}
#[derive(Deserialize)]
pub struct CreatePostRequest {
pub slug: String,
pub summary: Option<String>,
pub content: String,
}