Remove glob and move to markdowns folder
This commit is contained in:
parent
4a5e2cd99b
commit
c2df86fc33
8 changed files with 27 additions and 58 deletions
|
@ -1,9 +1,6 @@
|
|||
mod components;
|
||||
pub mod models;
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
mod server;
|
||||
|
||||
use leptos::*;
|
||||
use leptos_meta::*;
|
||||
use leptos_router::*;
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[derive(Debug, Clone)]
|
||||
#[cfg_attr(feature = "ssr", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct PostMetadata {
|
||||
image_path: Option<String>,
|
||||
title: String,
|
||||
|
@ -9,7 +8,8 @@ pub struct PostMetadata {
|
|||
project_link: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[derive(Debug, Clone)]
|
||||
#[cfg_attr(feature = "ssr", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct Post {
|
||||
metadata: PostMetadata,
|
||||
content: String,
|
||||
|
@ -42,12 +42,31 @@ cfg_if::cfg_if! {
|
|||
}
|
||||
|
||||
impl Post {
|
||||
pub fn get_all() -> Result<Vec<Self>, String> {
|
||||
let files_content = super::super::server::utils::get_files_content_with_blob("posts/*.md")?;
|
||||
pub fn get_all(folder: &str) -> Result<Vec<Self>, String> {
|
||||
use std::{path::Path, fs::read_dir};
|
||||
|
||||
let mut posts: Vec<Self> = Vec::new();
|
||||
|
||||
for (_filename, content) in files_content {
|
||||
let folder_path = Path::new("markdowns").join(folder);
|
||||
let paths = read_dir(folder_path).map_err(|e| e.to_string())?;
|
||||
|
||||
for path_result in paths {
|
||||
let path = match path_result {
|
||||
Ok(path) => path.path(),
|
||||
Err(e) => {
|
||||
eprintln!("{:?}", e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
pub mod utils;
|
|
@ -1,37 +0,0 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
pub fn get_files_content_with_blob(blob: &str) -> Result<HashMap<String, String>, String> {
|
||||
let mut files_content: HashMap<String, String> = HashMap::new();
|
||||
|
||||
use glob::glob;
|
||||
|
||||
for entry in glob(blob).map_err(|e| e.to_string())? {
|
||||
match entry {
|
||||
Ok(path) => {
|
||||
if path.is_dir() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let filename = path.to_str();
|
||||
let filename = match filename {
|
||||
Some(filename) => filename.to_string(),
|
||||
None => continue,
|
||||
};
|
||||
|
||||
let file_content = std::fs::read_to_string(path);
|
||||
let file_content = match file_content {
|
||||
Ok(file_content) => file_content,
|
||||
Err(e) => {
|
||||
println!("{:?}", e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
files_content.insert(filename, file_content.to_string());
|
||||
},
|
||||
Err(e) => println!("{:?}", e),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(files_content)
|
||||
}
|
|
@ -11,7 +11,7 @@ cfg_if! {
|
|||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let posts = app::models::Post::get_all();
|
||||
let posts = app::models::Post::get_all("posts");
|
||||
for post in posts.unwrap() {
|
||||
println!("{:?}", post);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue