diff --git a/src/app/mod.rs b/src/app/mod.rs index 87881e6..790043f 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -4,7 +4,6 @@ pub mod models; mod pages; -#[cfg(feature = "ssr")] mod utils; use leptos::*; diff --git a/src/app/pages/posts.rs b/src/app/pages/posts.rs index c2d899f..9dde85c 100644 --- a/src/app/pages/posts.rs +++ b/src/app/pages/posts.rs @@ -4,6 +4,10 @@ use crate::app::{ models::Post, components::{ Loading, Nav + }, + utils::{ + datetime::human_datetime, + reading_time::human_reading_time } }; @@ -86,8 +90,8 @@ pub fn PostListCard(

{post.metadata.title.clone()}

{post.metadata.description.clone()}

- {post.metadata.date.format("%d-%m-%Y").to_string()} - {post.metadata.reading_time} + {human_datetime(&post.metadata.date)} + {human_reading_time(post.metadata.reading_time)} } @@ -146,8 +150,8 @@ pub fn PostElement() -> impl IntoView { format!("Image

{post.metadata.title.clone()}

{post.metadata.description.clone()}

- {post.metadata.date.format("%d-%m-%Y").to_string()} - {post.metadata.reading_time} + {human_datetime(&post.metadata.date)} + {human_reading_time(post.metadata.reading_time)} diff --git a/src/app/utils/datetime.rs b/src/app/utils/datetime.rs new file mode 100644 index 0000000..37e8293 --- /dev/null +++ b/src/app/utils/datetime.rs @@ -0,0 +1,28 @@ +use chrono::{Datelike, DateTime, Local}; + +pub fn human_datetime(datetime: &DateTime) -> String { + format!( + "{day} {month} {year}", + day = datetime.day(), + month = human_month(datetime.month()), + year = datetime.year() + ) +} + +fn human_month<'a>(month: u32) -> &'a str { + match month { + 1 => "Jan.", + 2 => "Feb.", + 3 => "Mar.", + 4 => "Avr.", + 5 => "Mai", + 6 => "Juin", + 7 => "Juillet", + 8 => "Août", + 9 => "Sept.", + 10 => "Oct.", + 11 => "Nov.", + 12 => "Dec.", + _ => "Invalide" + } +} \ No newline at end of file diff --git a/src/app/utils/mod.rs b/src/app/utils/mod.rs index 2a025b1..c7f923a 100644 --- a/src/app/utils/mod.rs +++ b/src/app/utils/mod.rs @@ -1,2 +1,5 @@ +#[cfg(feature = "ssr")] pub(crate) mod data_src; + +pub(crate) mod datetime; pub(crate) mod reading_time; \ No newline at end of file diff --git a/src/app/utils/reading_time.rs b/src/app/utils/reading_time.rs index b82b00e..ac8fee3 100644 --- a/src/app/utils/reading_time.rs +++ b/src/app/utils/reading_time.rs @@ -1,3 +1,4 @@ +#[cfg(feature = "ssr")] pub fn calculate_reading_time(content: &String) -> u64 { let doc_read_time = estimated_read_time::Options::new() .word_length(5) @@ -7,4 +8,15 @@ pub fn calculate_reading_time(content: &String) -> u64 { .unwrap_or_default(); estimated_read_time::text(&content, &doc_read_time).seconds() +} + +pub fn human_reading_time(reading_time: u64) -> String { + match reading_time { + 0..=60 => format!("{reading_time}s"), + _ => { + let min = reading_time / 60; + let secs = reading_time - min * 60; + format!("{min}min {secs}s") + } + } } \ No newline at end of file