use super::*;
fn decode_anchor(encoded: &str) -> String {
if !encoded.as_bytes().contains(&b'%') {
return encoded.to_string();
}
match decode_uri_component(encoded) {
Ok(value) => value.as_string().unwrap_or_else(|| encoded.to_string()),
Err(_) => encoded.to_string(),
}
}
pub(crate) fn parse_route(raw: &str) -> (String, Option<String>) {
match raw.split_once('#') {
Some((path, anchor)) if !anchor.is_empty() => {
(path.to_string(), Some(decode_anchor(anchor)))
}
Some((path, _)) => (path.to_string(), None),
None => (raw.to_string(), None),
}
}
pub(crate) fn locale_of(route: &str) -> &'static DocsLocale {
let site: &DocsSite = &crate::generated::SITE;
site.locales
.iter()
.filter(|locale| locale.prefix != "/")
.find(|locale| route.starts_with(locale.prefix))
.or_else(|| site.locales.iter().find(|locale| locale.prefix == "/"))
.unwrap_or(&site.locales[0])
}
pub(crate) fn find_page(route: &str) -> Option<&'static DocsPage> {
let site: &DocsSite = &crate::generated::SITE;
site.pages
.iter()
.find(|page| page.route == route)
.or_else(|| {
if route.ends_with('/') || route.ends_with(".html") {
None
} else {
let with_slash: String = format!("{route}/");
site.pages.iter().find(|page| page.route == with_slash)
}
})
}
pub(crate) fn flat_sidebar_links(items: &'static [EuvSidebarItem]) -> Vec<&'static EuvSidebarItem> {
let mut out: Vec<&'static EuvSidebarItem> = Vec::new();
for item in items {
if item.children.is_empty() {
if item.link.is_some() {
out.push(item);
}
} else {
out.extend(flat_sidebar_links(item.children));
}
}
out
}
pub(crate) fn route_in_locale(route: &str, target: &'static DocsLocale) -> String {
let current: &DocsLocale = locale_of(route);
let suffix: &str = route
.strip_prefix(current.prefix.trim_end_matches('/'))
.unwrap_or(route);
let suffix: &str = if suffix.is_empty() { "/" } else { suffix };
let candidate: String = if target.prefix == "/" {
suffix.to_string()
} else {
format!("{}{}", target.prefix.trim_end_matches('/'), suffix)
};
if find_page(&candidate).is_some() {
candidate
} else {
target.prefix.to_string()
}
}