blog post display and metadata

This commit is contained in:
2024-01-11 22:16:38 +01:00
parent ae1b65957d
commit 03fefda519
5 changed files with 72 additions and 21 deletions

View File

@ -1 +1,2 @@
pub mod index;
pub mod post;

View File

@ -0,0 +1,34 @@
use askama::Template;
use axum::{extract::Path, http::StatusCode};
use chrono::{DateTime, Utc};
use serde::Deserialize;
use crate::post_parser::{deserialize_date, parse_post};
#[derive(Deserialize, Debug)]
pub struct PostMetadata {
pub layout: String,
pub title: String,
pub segments: Vec<String>,
pub published: bool,
#[serde(deserialize_with = "deserialize_date")]
pub date: DateTime<Utc>,
pub thumbnail: String,
pub tags: Vec<String>,
}
#[derive(Template)]
#[template(path = "post.html")]
pub struct PostTemplate {
pub title: String,
pub content: String,
}
pub async fn render_post(Path(post_id): Path<String>) -> Result<PostTemplate, StatusCode> {
let path = format!("../_posts/blog/{}.md", post_id);
let parsed = parse_post::<PostMetadata>(&path).await?;
Ok(PostTemplate {
title: parsed.metadata.title,
content: parsed.content,
})
}