add: markdown docs directory

This commit is contained in:
trisua 2025-04-05 17:28:51 -04:00
parent b31d8c38b9
commit a167da017e
8 changed files with 69 additions and 6 deletions

View file

@ -1,12 +1,14 @@
use super::{PaginatedQuery, render_error};
use crate::{State, assets::initial_context, get_lang, get_user_from_token};
use axum::{
Extension,
extract::Query,
extract::{Path, Query},
response::{Html, IntoResponse},
Extension,
};
use axum_extra::extract::CookieJar;
use tetratto_core::model::Error;
use std::fs::read_to_string;
use pathbufd::PathBufD;
pub async fn not_found(jar: CookieJar, Extension(data): Extension<State>) -> impl IntoResponse {
let data = data.read().await;
@ -112,3 +114,37 @@ pub async fn notifications_request(
data.1.render("misc/notifications.html", &context).unwrap(),
))
}
/// `/doc/{file_name}`
pub async fn markdown_document_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path(name): Path<String>,
) -> impl IntoResponse {
let data = data.read().await;
let user = get_user_from_token!(jar, data.0);
if name.contains("//") | name.contains("..") | name.starts_with("/") {
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &user).await,
));
}
let path = PathBufD::current().extend(&[&data.0.0.dirs.docs, &name]);
let file = match read_to_string(&path) {
Ok(f) => f,
Err(e) => {
return Err(Html(
render_error(Error::MiscError(e.to_string()), &jar, &data, &user).await,
));
}
};
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &user).await;
context.insert("file", &file);
context.insert("file_name", &name);
// return
Ok(Html(data.1.render("misc/markdown.html", &context).unwrap()))
}

View file

@ -20,6 +20,7 @@ pub fn routes() -> Router {
.route("/", get(misc::index_request))
.route("/popular", get(misc::popular_request))
.route("/notifs", get(misc::notifications_request))
.route("/doc/{*file_name}", get(misc::markdown_document_request))
.fallback_service(get(misc::not_found))
// mod
.route("/mod_panel/audit_log", get(mod_panel::audit_log_request))