diff --git a/src/routes/portfolio.rs b/src/routes/portfolio.rs index a01cfdb..347476a 100644 --- a/src/routes/portfolio.rs +++ b/src/routes/portfolio.rs @@ -26,22 +26,32 @@ pub fn get_page(config: Config) -> String { let ext = ".md"; // Get bots apps - let mut bots_apps = Vec::new(); - for entry in glob(&format!("{projects_dir}/bots/*{ext}")).unwrap() { - bots_apps.push(read_md_file(&entry.unwrap().to_string_lossy())); - } + let bots_apps_loc = format!("{projects_dir}/bots"); + let bots_apps = glob(&format!("{bots_apps_loc}/*{ext}")) + .unwrap() + .map(|e| read_md_file(&e.unwrap().to_string_lossy())) + .collect::>(); // Get perso apps - let mut perso_apps = Vec::new(); - for entry in glob(&format!("{projects_dir}/perso/*{ext}")).unwrap() { - perso_apps.push(read_md_file(&entry.unwrap().to_string_lossy())); - } + let perso_apps_loc = format!("{projects_dir}/perso"); + let perso_apps = glob(&format!("{perso_apps_loc}/*{ext}")) + .unwrap() + .map(|e| read_md_file(&e.unwrap().to_string_lossy())) + .collect::>(); config.tmpl.render( "portfolio.html", PortfolioTemplate { - bots_app: bots_apps, - persos_app: perso_apps, + bots_app: if bots_apps.is_empty() { + vec![read_md_file(&bots_apps_loc)] + } else { + bots_apps + }, + persos_app: if perso_apps.is_empty() { + vec![read_md_file(&perso_apps_loc)] + } else { + perso_apps + }, univ_content: read_md_file(&format!("{projects_dir}/univ{ext}")).content, }, Infos { diff --git a/src/template.rs b/src/template.rs index f9d0b3d..dc0edb5 100644 --- a/src/template.rs +++ b/src/template.rs @@ -73,7 +73,7 @@ pub struct File { pub fn read_md_file(filename: &str) -> File { // Read markdown file - let text = fs::read_to_string(filename).unwrap(); + let text = fs::read_to_string(filename).unwrap_or(format!("{filename} is empty")); read_md(&text) } @@ -97,12 +97,34 @@ pub fn read_md(raw_text: &str) -> File { let md_tree = markdown::to_mdast(&text, &parse_option).unwrap(); let md_nodes = md_tree.children().unwrap(); - let metadata = match &md_nodes[0] { - markdown::mdast::Node::Yaml(v) => FrontMatter::Yaml(&v.value).parse(), - markdown::mdast::Node::Toml(v) => FrontMatter::Toml(&v.value).parse(), - markdown::mdast::Node::MdxjsEsm(v) => FrontMatter::Json(&v.value).parse(), - _ => FileMetadata::default(), - }; + let metadata; + let presence_mermaid; + let presence_code; + if md_nodes.is_empty() { + metadata = FileMetadata::default(); + presence_mermaid = false; + presence_code = false; + } else { + metadata = match &md_nodes[0] { + markdown::mdast::Node::Yaml(v) => FrontMatter::Yaml(&v.value).parse(), + markdown::mdast::Node::Toml(v) => FrontMatter::Toml(&v.value).parse(), + markdown::mdast::Node::MdxjsEsm(v) => FrontMatter::Json(&v.value).parse(), + _ => FileMetadata::default(), + }; + + // Find if document contains mermaid diagram + let mermaid = Some(String::from("mermaid")); + presence_mermaid = md_nodes.iter().any(|x| match x { + markdown::mdast::Node::Code(code) => code.lang == mermaid, + _ => false, + }); + + // Find if document contains code to highlight + presence_code = md_nodes.iter().any(|x| match x { + markdown::mdast::Node::Code(code) => code.lang != mermaid, + _ => false, + }); + } // Convert to HTML let html = markdown::to_html_with_options( @@ -114,19 +136,6 @@ pub fn read_md(raw_text: &str) -> File { ) .unwrap(); - // Find if document contains mermaid diagram - let mermaid = Some(String::from("mermaid")); - let presence_mermaid = md_nodes.iter().any(|x| match x { - markdown::mdast::Node::Code(code) => code.lang == mermaid, - _ => false, - }); - - // Find if document contains code to highlight - let presence_code = md_nodes.iter().any(|x| match x { - markdown::mdast::Node::Code(code) => code.lang != mermaid, - _ => false, - }); - File { metadata: Metadata { info: metadata,