mirror of
https://github.com/RGBCube/Site
synced 2025-08-01 13:37:49 +00:00
Add Markdown page creator
This commit is contained in:
parent
dc306f1ed7
commit
7010360f04
5 changed files with 72 additions and 20 deletions
|
@ -41,6 +41,16 @@ impl Page {
|
|||
Self::Other => "other",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_str(s: &str) -> Self {
|
||||
match s {
|
||||
"home" => Self::Home,
|
||||
"about" => Self::About,
|
||||
"blog" => Self::Blog,
|
||||
"contact" => Self::Contact,
|
||||
_ => Self::Other,
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Creates a page with the given head and body.
|
||||
///
|
||||
|
|
|
@ -13,7 +13,7 @@ use crate::page::{
|
|||
};
|
||||
|
||||
/// Creates a simple text page.
|
||||
pub fn create(title: Option<&str>, page: Page, body: Markup) -> Markup {
|
||||
pub fn create(title: Option<&str>, page: Page, body: &Markup) -> Markup {
|
||||
crate::page::create(
|
||||
title,
|
||||
html! {
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
use maud::Markup;
|
||||
|
||||
use crate::{
|
||||
markdown,
|
||||
page::{
|
||||
text,
|
||||
Page,
|
||||
},
|
||||
};
|
||||
|
||||
pub async fn handler() -> Markup {
|
||||
text::create(
|
||||
Some("About"),
|
||||
Page::About,
|
||||
markdown::parse(embed::string!("about.md").as_ref()),
|
||||
)
|
||||
}
|
59
src/routes/mdpage.rs
Normal file
59
src/routes/mdpage.rs
Normal file
|
@ -0,0 +1,59 @@
|
|||
use std::{
|
||||
collections::HashMap,
|
||||
path,
|
||||
sync::LazyLock,
|
||||
};
|
||||
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::Path,
|
||||
http::{
|
||||
Response,
|
||||
StatusCode,
|
||||
},
|
||||
response::{
|
||||
Html,
|
||||
IntoResponse,
|
||||
},
|
||||
};
|
||||
use maud::Markup;
|
||||
|
||||
use crate::{
|
||||
markdown,
|
||||
page::{
|
||||
text,
|
||||
Page,
|
||||
},
|
||||
};
|
||||
|
||||
static PAGES: LazyLock<HashMap<String, Markup>> = LazyLock::new(|| {
|
||||
let mut pages = HashMap::new();
|
||||
|
||||
for file in embed::dir!(".").flatten() {
|
||||
let path = path::Path::new(file.path().as_ref())
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap();
|
||||
|
||||
if !path.ends_with(".md") {
|
||||
continue;
|
||||
}
|
||||
|
||||
log::info!("Adding page {path}");
|
||||
pages.insert(
|
||||
path.to_string().strip_suffix(".md").unwrap().to_string(),
|
||||
markdown::parse(&String::from_utf8(file.content().to_vec()).unwrap()),
|
||||
);
|
||||
}
|
||||
|
||||
pages
|
||||
});
|
||||
|
||||
pub async fn handler(Path(path): Path<String>) -> Response<Body> {
|
||||
if let Some(body) = PAGES.get(&path) {
|
||||
Html(text::create(Some("test"), Page::from_str(&path), &body).into_string()).into_response()
|
||||
} else {
|
||||
StatusCode::NOT_FOUND.into_response()
|
||||
}
|
||||
}
|
|
@ -3,13 +3,13 @@ use axum::{
|
|||
Router,
|
||||
};
|
||||
|
||||
mod about;
|
||||
mod assets;
|
||||
mod index;
|
||||
mod mdpage;
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route("/", get(index::handler))
|
||||
.route("/about", get(about::handler))
|
||||
.route("/:page", get(mdpage::handler))
|
||||
.route("/assets/*path", get(assets::handler))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue