Skip to content

Commit

Permalink
feat: add sitemaps to documentation website
Browse files Browse the repository at this point in the history
  • Loading branch information
Valerioageno committed Aug 18, 2024
1 parent 3772e73 commit e7fcd64
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 2 additions & 0 deletions apps/documentation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

2 changes: 1 addition & 1 deletion apps/documentation/src/components/hero/hero.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) }} />
Expand Down
61 changes: 61 additions & 0 deletions apps/documentation/src/routes/sitemap.xml.rs
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)))
}

0 comments on commit e7fcd64

Please sign in to comment.