-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sitemaps to documentation website
- Loading branch information
1 parent
3772e73
commit e7fcd64
Showing
3 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))) | ||
} | ||
|