Begin add PostList routes + 404

This commit is contained in:
Florian RICHER 2023-11-26 17:17:31 +01:00
parent c2df86fc33
commit 224b5d4832
6 changed files with 54 additions and 34 deletions

View file

@ -1,5 +1,6 @@
mod components;
pub mod models;
mod pages;
use leptos::*;
use leptos_meta::*;
@ -14,18 +15,10 @@ pub fn App() -> impl IntoView {
<Link rel="shortcut icon" type_="image/ico" href="/favicon.ico"/>
<Router>
<Routes>
<Route path="" view= move || view! { <Home/> }/>
<Route path="" view=pages::Home />
<Route path="posts" view=|| view!{ <pages::PostList folder="posts".to_string() /> } />
<Route path="/*any" view=|| view! { <h1>"Not Found"</h1> }/>
</Routes>
</Router>
}
}
#[component]
fn Home() -> impl IntoView {
view! {
<main class="m-0 p-0 bg-surface dark:bg-dark_surface text-on_surface dark:text-dark_on_surface">
<components::TopComponent/>
<components::MonParcours/>
</main>
}
}

View file

@ -17,14 +17,13 @@ pub struct Post {
cfg_if::cfg_if! {
if #[cfg(feature = "ssr")] {
impl From<String> for Post {
fn from(content: String) -> Self {
impl Post {
fn from_content(content: String) -> Option<Self> {
use gray_matter::{Matter, engine::YAML};
let matter = Matter::<YAML>::new();
let post_data = matter
.parse_with_struct::<PostMetadata>(&content)
.expect("Unable to parse md frontmatter");
.parse_with_struct::<PostMetadata>(&content)?;
let metadata = post_data.data;
let content = post_data.content;
@ -34,14 +33,25 @@ cfg_if::cfg_if! {
let mut html_output = String::new();
html::push_html(&mut html_output, parser);
Self {
Some(Self {
metadata,
content: html_output,
}
})
}
fn from_path(path: &std::path::Path) -> Option<Self> {
let content = std::fs::read_to_string(path);
let content = match content {
Ok(content) => content,
Err(e) => {
eprintln!("{:?}", e);
return None;
}
};
Self::from_content(content)
}
}
impl Post {
pub fn get_all(folder: &str) -> Result<Vec<Self>, String> {
use std::{path::Path, fs::read_dir};
@ -59,16 +69,9 @@ cfg_if::cfg_if! {
}
};
let content = std::fs::read_to_string(path);
let content = match content {
Ok(content) => content,
Err(e) => {
eprintln!("{:?}", e);
continue;
}
};
let post = Self::from(content);
posts.push(post);
if let Some(post) = Self::from_path(&path) {
posts.push(post);
}
}
Ok(posts)

11
src/app/pages/home.rs Normal file
View file

@ -0,0 +1,11 @@
use leptos::*;
use super::super::components::{TopComponent, MonParcours};
#[component]
pub fn Home() -> impl IntoView {
view! {
<main class="m-0 p-0 bg-surface dark:bg-dark_surface text-on_surface dark:text-dark_on_surface">
<TopComponent/>
<MonParcours/>
</main>
}
}

5
src/app/pages/mod.rs Normal file
View file

@ -0,0 +1,5 @@
mod home;
pub use home::Home;
mod posts;
pub use posts::PostList;

13
src/app/pages/posts.rs Normal file
View file

@ -0,0 +1,13 @@
use leptos::*;
use leptos_router::*;
#[component]
pub fn PostList(
folder: String
) -> impl IntoView {
view! {
<main class="m-0 p-0 bg-surface dark:bg-dark_surface text-on_surface dark:text-dark_on_surface">
Page List
</main>
}
}

View file

@ -11,11 +11,6 @@ cfg_if! {
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let posts = app::models::Post::get_all("posts");
for post in posts.unwrap() {
println!("{:?}", post);
}
// Setting this to None means we'll be using cargo-leptos and its env vars.
let conf = get_configuration(None).await.unwrap();