From d668eb2d9dcd6302ac8c6e7a933891b9887ad8e3 Mon Sep 17 00:00:00 2001 From: Triston Armstrong Date: Sun, 31 Mar 2024 15:24:40 -0500 Subject: [PATCH] (feat): use new util functions --- src/router.rs | 24 +++++++++++++++++++----- src/routes.rs | 4 ++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/router.rs b/src/router.rs index 33849fb..5499b21 100644 --- a/src/router.rs +++ b/src/router.rs @@ -1,7 +1,10 @@ use axum::{routing::get, Router}; use std::{env, fs}; -use crate::routes; +use crate::{routes, utils}; + +const FILE_NAME_DELIMETER: &str = "_"; +const FILE_NAME_DELIMETER_REPLACEMENT: &str = " "; #[derive(Clone)] pub struct AppState {} @@ -11,12 +14,22 @@ pub fn init_notes_routes() -> Router { Router::new().route("/", get(routes::notes)) } -pub fn update_route(router: Router, file_name_str: String, file_path_str: String) -> Router { +pub fn update_tuts_route(router: Router, file_name_str: String, file_path_str: String) -> Router { let route = format!("/{}", file_name_str); - println!("{}", route); + let tut_title = utils::rm_path_ext(utils::rm_delimeter( + file_name_str, + FILE_NAME_DELIMETER.to_string(), + FILE_NAME_DELIMETER_REPLACEMENT.to_string(), + )); + router.route( route.clone().as_str(), - get(move || routes::tuts_builder(file_path_str)), + get(move || { + routes::tuts_builder( + file_path_str, + utils::upper_all_words_first_letters(tut_title).unwrap(), + ) + }), ) } @@ -34,8 +47,9 @@ pub fn init_tuts_routes() -> Router { let file_name = entry.file_name(); let file_name_str = file_name.to_str().unwrap().to_string(); file_dir_vec.push(file_name_str.clone()); - router = update_route(router, file_name_str, file_path); + router = update_tuts_route(router, file_name_str, file_path); } + router = router.route("/", get(move || routes::tuts(file_dir_vec))); router diff --git a/src/routes.rs b/src/routes.rs index 3ea6fbf..3b35b6a 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -22,12 +22,12 @@ pub async fn tuts(paths: Vec) -> impl IntoResponse { templates::Tuts { tuts_list: paths } } -pub async fn tuts_builder(path: String) -> Result { +pub async fn tuts_builder(path: String, tut_title: String) -> Result { let file_contents = std::fs::read_to_string(path).unwrap(); let file_contents_to_html = markdown_to_html(file_contents.as_str(), &comrak::Options::default()); let file_template = Tut { - title: "Note", + title: &tut_title, content: file_contents_to_html.as_str(), }; let response = Response::builder()