Begin add markdown parsing
Some checks failed
deploy / docker (push) Has been cancelled

This commit is contained in:
Florian RICHER 2023-11-26 15:36:01 +01:00
parent aca074ef01
commit 34e81035d0
9 changed files with 208 additions and 0 deletions

View file

@ -1,4 +1,8 @@
mod components;
pub mod models;
#[cfg(feature = "ssr")]
mod server;
use leptos::*;
use leptos_meta::*;

3
src/app/models/mod.rs Normal file
View file

@ -0,0 +1,3 @@
mod post;
pub use post::Post;

55
src/app/models/post.rs Normal file
View file

@ -0,0 +1,55 @@
pub struct PostMetadata {
image_path: Option<String>,
title: String,
date: String,
description: String,
project_link: Option<String>,
}
pub struct Post {
metadata: PostMetadata,
content: String,
}
cfg_if::cfg_if! {
if #[cfg(feature = "ssr")] {
impl From<String> for Post {
fn from(content: String) -> Self {
use pulldown_cmark::{Parser, Options, html};
let parser = Parser::new_ext(&content, Options::all());
let mut html_output = String::new();
html::push_html(&mut html_output, parser);
println!("\nHTML output:\n{}\n", &html_output);
Self {
metadata: PostMetadata {
image_path: None,
title: String::from(""),
date: String::from(""),
description: String::from(""),
project_link: None,
},
content: html_output,
}
}
}
impl Post {
pub fn get_all() -> Result<Vec<Self>, String> {
let files_content = super::super::server::utils::get_files_content_with_blob("posts/*.md")?;
let mut posts: Vec<Self> = Vec::new();
for (_filename, content) in files_content {
let post = Self::from(content);
posts.push(post);
}
Ok(posts)
}
}
}
}

1
src/app/server/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod utils;

37
src/app/server/utils.rs Normal file
View file

@ -0,0 +1,37 @@
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)
}

View file

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