post listing with some more metadata

This commit is contained in:
Michal Vanko 2024-01-18 20:31:25 +01:00
parent c44c2243de
commit 35e3d595df
6 changed files with 53 additions and 9 deletions

View File

@ -27,4 +27,4 @@ async fn main() {
} }
// TODO Port from env variable // TODO Port from env variable
// TODO templating system // TODO displaying Image from netlify CDN

View File

@ -10,6 +10,7 @@ use super::post::PostMetadata;
#[derive(Template)] #[derive(Template)]
#[template(path = "post_list.html")] #[template(path = "post_list.html")]
pub struct PostListTemplate { pub struct PostListTemplate {
pub title: String,
pub posts: Vec<ParseResult<PostMetadata>>, pub posts: Vec<ParseResult<PostMetadata>>,
pub tag: Option<String>, pub tag: Option<String>,
} }
@ -51,9 +52,12 @@ pub async fn render_post_list(tag: Option<Path<String>>) -> Result<PostListTempl
None => posts, None => posts,
}; };
Ok(PostListTemplate { posts, tag }) Ok(PostListTemplate {
title: "Posts".to_owned(),
posts,
tag,
})
} }
// TODO Do we want pagination or not? Ask designer // TODO Do we want pagination or not? Ask designer/ We don't want itt
// TODO How are we going to implement tags? The path extractor would have to make decision on wether we have a path or a blog post // TODO when tags are true render different "see all post" message
// TODO Refactor `?` with `.map_err`

View File

@ -1,3 +1,5 @@
use std::path::Path;
use axum::http::StatusCode; use axum::http::StatusCode;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use gray_matter::{engine::YAML, Matter}; use gray_matter::{engine::YAML, Matter};
@ -22,6 +24,7 @@ where
pub struct ParseResult<Metadata> { pub struct ParseResult<Metadata> {
pub body: String, pub body: String,
pub metadata: Metadata, pub metadata: Metadata,
pub slug: String,
} }
pub async fn parse_post<'de, Metadata: DeserializeOwned>( pub async fn parse_post<'de, Metadata: DeserializeOwned>(
@ -60,8 +63,16 @@ pub async fn parse_post<'de, Metadata: DeserializeOwned>(
return StatusCode::INTERNAL_SERVER_ERROR; return StatusCode::INTERNAL_SERVER_ERROR;
})?; })?;
let filename = Path::new(path)
.file_stem()
.ok_or(StatusCode::INTERNAL_SERVER_ERROR)?
.to_str()
.ok_or(StatusCode::INTERNAL_SERVER_ERROR)?
.to_owned();
return Ok(ParseResult { return Ok(ParseResult {
body, body,
metadata: metadata.data, metadata: metadata.data,
slug: filename,
}); });
} }

View File

@ -32,5 +32,6 @@
</head> </head>
<body> <body>
{% block content %} Placeholder {% endblock %} {% block content %} Placeholder {% endblock %}
{# footer, should be not dependant on the each individual handler but it should have it's own handler #}
</body> </body>
</html> </html>

View File

@ -1,4 +1,4 @@
{% if posts.len() == 0 %} {% extends "base.html" %} {% block content %} {% if posts.len() == 0 %}
<p class="no-posts">You've found void in the space.</p> <p class="no-posts">You've found void in the space.</p>
{% else %} {% else %}
<h1>{# if filters.tags #} {# <em>{filters.tags}</em> #} Blog posts</h1> <h1>{# if filters.tags #} {# <em>{filters.tags}</em> #} Blog posts</h1>
@ -12,10 +12,11 @@
<ul> <ul>
{% for post in posts %} {% for post in posts %}
<li> <li>
{# <ArticlePreviewCard {article} {segment} /> #} {# {% include "post_preview_card.html" %} {#
<ArticleFooter {article} {segment} /> #} {{post.metadata.title}} <ArticlePreviewCard {article} {segment} /> #} {#
<ArticleFooter {article} {segment} /> #}
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
{# <ArticlePreviewList {...data} segment="blog" /> #} {# <ArticlePreviewList {...data} segment="blog" /> #} {% endblock %}

View File

@ -0,0 +1,27 @@
<article>
<header>
<h2>
<a rel="prefetch" href="/blog/{{post.slug}}">{{post.metadata.title}}</a>
</h2>
</header>
{# article preview, maybe implement as a filter? #}
</article>
<footer>
<div class="article-tags">
{% if post.metadata.tags.len() > 0 %}
<span class="lighten">Tags:</span>
<ul>
{% for tag in post.metadata.tags %}
<li class="{tagsListLiClass}">
<a href="/blog/tags/{{tag}}">{{tag}}</a>
</li>
{% endfor %}
</ul>
{% endif %}
</div>
<div class="created-at">
<span>Published on</span>
<time datetime="{post.metadata.date}"> {{post.metadata.date}} </time>
</div>
</footer>