feat: add sitemaps to documentation website

This commit is contained in:
Valerio Ageno
2024-08-18 12:25:12 +02:00
parent 3772e7307e
commit e7fcd64b3a
3 changed files with 64 additions and 1 deletions
+2
View File
@@ -9,5 +9,7 @@ path = ".tuono/main.rs"
[dependencies]
tuono_lib = "0.10.0"
glob = "0.3.1"
time = { version = "0.3", features = ["macros"] }
serde = { version = "1.0.202", features = ["derive"] }
@@ -35,7 +35,7 @@ export default function Hero(): JSX.Element {
size="lg"
style={{ border: 'solid 1px var(--mantine-color-violet-1)' }}
color="gray"
leftSection="cargo install tuono"
leftSection="$ cargo install tuono"
rightSection={
copied ? (
<IconCheck style={{ width: rem(20) }} />
@@ -0,0 +1,61 @@
use tuono_lib::reqwest::{Client, StatusCode};
use tuono_lib::{Request, Response};
use tuono_lib::axum::http::{header, HeaderMap};
use glob::glob;
use time::OffsetDateTime;
const FILE_TO_EXCLUDE: [&str; 2] = ["sitemap.xml", "__root"];
const SITEMAP: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
[PLACEHOLDER]
</urlset>"#;
fn load_routes() -> Vec<String> {
let mut paths: Vec<String> = vec![];
for entry in glob("./src/routes/**/*").expect("Failed to glob src/routes folder").flatten() {
if !entry.is_dir() {
let path = clean_path(format!("/{}", entry.to_string_lossy()));
if !FILE_TO_EXCLUDE.iter().any(|exclude| path.ends_with(exclude)) {
paths.push(path)
}
}
}
paths
}
fn clean_path(value: String) -> String {
value
.replace("src/routes/", "")
.replace(".mdx", "")
.replace(".tsx", "")
.replace(".rs", "")
.replace("index", "")
}
#[tuono_lib::handler]
async fn generate_sitemap(_req: Request, _fetch: Client) -> Response {
let mut headers = HeaderMap::new();
headers.insert(header::CONTENT_TYPE, "text/xml".parse().unwrap());
let routes = load_routes();
let mut sitemaps = String::new();
for path in routes {
let mut url = format!("https://tuono.dev{}", path);
if url.ends_with('/') {
url.pop();
}
sitemaps.push_str(
&format!(r#"<url><loc>{}</loc><lastmod>{}</lastmod></url>"#,url, OffsetDateTime::now_utc().date())
)
}
Response::Custom((StatusCode::OK, headers, SITEMAP.replace("[PLACEHOLDER]", &sitemaps)))
}