Remove glob and move to markdowns folder

This commit is contained in:
Florian RICHER 2023-11-26 16:35:11 +01:00
parent 4a5e2cd99b
commit c2df86fc33
8 changed files with 27 additions and 58 deletions

View file

@ -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);
}