staticrustator/src/blog_entries.rs

40 lines
1.2 KiB
Rust

use std::{fs::read_dir, path::Path};
use markdown_parser::*;
use crate::structs::{BlogInfo, IndexPostEntry};
pub fn get_blog_entry_markdown(path:&String) -> Result<Markdown,Error> {
let location = format!("content/posts/{path}.md").to_string();
read_file(Path::new(&location))
}
pub fn get_all_markdowns() -> Vec<IndexPostEntry> {
let mut post_vec:Vec<IndexPostEntry> = Vec::new();
let mr_dir_iter = match read_dir("content/posts") {
Ok(iter) => iter,
Err(err) => panic!("couldnt ls files, err {err}")
};
for entry in mr_dir_iter {
if let Ok(entry) = entry {
let filename = entry.file_name().into_string().unwrap().split(".").collect::<Vec<_>>()[0].to_string();
let front_matter_string = get_blog_entry_markdown(&filename).unwrap();
let front_matter:BlogInfo = serde_yaml::from_str(&front_matter_string.front_matter()).unwrap();
post_vec.push(IndexPostEntry{
title: front_matter.title,
date: front_matter.date,
path: format!("/blog/{filename}.html"),
});
}
}
post_vec.sort_by_key(|e| e.path.clone() );
post_vec.reverse();
post_vec
}