diff --git a/src/api/posts/create.rs b/src/api/posts/create.rs index bea9767..1446be3 100644 --- a/src/api/posts/create.rs +++ b/src/api/posts/create.rs @@ -61,6 +61,11 @@ pub async fn create_post( let final_slug = crate::api::slug::ensure_unique_slug(&client, &base_slug, None).await?; let rendered = crate::api::markdown::render_markdown_enhanced(&content_md); let content_html = rendered.html; + let toc_html = if rendered.toc_html.is_empty() { + None:: + } else { + Some(rendered.toc_html) + }; let summary = summary .filter(|s| !s.trim().is_empty()) .unwrap_or_else(|| crate::utils::text::auto_summary(&content_md)); @@ -77,8 +82,8 @@ pub async fn create_post( let row = tx .query_one( - "INSERT INTO posts (author_id, title, slug, summary, content_md, content_html, status, published_at, cover_image) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) + "INSERT INTO posts (author_id, title, slug, summary, content_md, content_html, toc_html, status, published_at, cover_image) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING id", &[ &user.id, @@ -87,6 +92,7 @@ pub async fn create_post( &summary, &content_md, &content_html, + &toc_html, &post_status.as_str(), &published_at, &cover_image, diff --git a/src/api/posts/update.rs b/src/api/posts/update.rs index 97b4a0d..7c2b0ea 100644 --- a/src/api/posts/update.rs +++ b/src/api/posts/update.rs @@ -68,6 +68,11 @@ pub async fn update_post( let final_slug = crate::api::slug::ensure_unique_slug(&client, &base_slug, Some(post_id)).await?; let rendered = crate::api::markdown::render_markdown_enhanced(&content_md); let content_html = rendered.html; + let toc_html = if rendered.toc_html.is_empty() { + None:: + } else { + Some(rendered.toc_html) + }; let summary = summary .filter(|s| !s.trim().is_empty()) .unwrap_or_else(|| crate::utils::text::auto_summary(&content_md)); @@ -116,14 +121,15 @@ pub async fn update_post( }; tx.execute( - "UPDATE posts SET title = $1, slug = $2, summary = $3, content_md = $4, content_html = $5, status = $6, published_at = $7, cover_image = $8, updated_at = NOW() - WHERE id = $9", + "UPDATE posts SET title = $1, slug = $2, summary = $3, content_md = $4, content_html = $5, toc_html = $6, status = $7, published_at = $8, cover_image = $9, updated_at = NOW() + WHERE id = $10", &[ &title.trim(), &final_slug, &summary, &content_md, &content_html, + &toc_html, &post_status.as_str(), &published_at, &cover_image,