(feat): use new util functions

This commit is contained in:
Triston Armstrong 2024-03-31 15:24:40 -05:00
parent 5330d5fab0
commit d668eb2d9d
2 changed files with 21 additions and 7 deletions

View File

@ -1,7 +1,10 @@
use axum::{routing::get, Router}; use axum::{routing::get, Router};
use std::{env, fs}; use std::{env, fs};
use crate::routes; use crate::{routes, utils};
const FILE_NAME_DELIMETER: &str = "_";
const FILE_NAME_DELIMETER_REPLACEMENT: &str = " ";
#[derive(Clone)] #[derive(Clone)]
pub struct AppState {} pub struct AppState {}
@ -11,12 +14,22 @@ pub fn init_notes_routes() -> Router {
Router::new().route("/", get(routes::notes)) 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); 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( router.route(
route.clone().as_str(), 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 = entry.file_name();
let file_name_str = file_name.to_str().unwrap().to_string(); let file_name_str = file_name.to_str().unwrap().to_string();
file_dir_vec.push(file_name_str.clone()); 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 = router.route("/", get(move || routes::tuts(file_dir_vec)));
router router

View File

@ -22,12 +22,12 @@ pub async fn tuts(paths: Vec<String>) -> impl IntoResponse {
templates::Tuts { tuts_list: paths } templates::Tuts { tuts_list: paths }
} }
pub async fn tuts_builder(path: String) -> Result<impl IntoResponse, ApiError> { pub async fn tuts_builder(path: String, tut_title: String) -> Result<impl IntoResponse, ApiError> {
let file_contents = std::fs::read_to_string(path).unwrap(); let file_contents = std::fs::read_to_string(path).unwrap();
let file_contents_to_html = let file_contents_to_html =
markdown_to_html(file_contents.as_str(), &comrak::Options::default()); markdown_to_html(file_contents.as_str(), &comrak::Options::default());
let file_template = Tut { let file_template = Tut {
title: "Note", title: &tut_title,
content: file_contents_to_html.as_str(), content: file_contents_to_html.as_str(),
}; };
let response = Response::builder() let response = Response::builder()