Skip to content

Commit

Permalink
Migrate menus URLs under /crate/
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Sep 7, 2023
1 parent 42321b6 commit b2c47fa
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 34 deletions.
56 changes: 42 additions & 14 deletions src/web/crate_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1307,13 +1307,15 @@ mod tests {

fn run_check_links(
env: &TestEnvironment,
url: &str,
url_start: &str,
url_end: &str,
extra: &str,
should_contain_redirect: bool,
) {
run_check_links_redir(
env,
url,
url_start,
url_end,
extra,
should_contain_redirect,
should_contain_redirect,
Expand All @@ -1322,18 +1324,30 @@ mod tests {

fn run_check_links_redir(
env: &TestEnvironment,
url: &str,
url_start: &str,
url_end: &str,
extra: &str,
should_contain_redirect: bool,
ajax_should_contain_redirect: bool,
) {
let response = env.frontend().get(url).send().unwrap();
let response = env
.frontend()
.get(&format!("{url_start}{url_end}"))
.send()
.unwrap();
assert!(response.status().is_success());
let list1 = check_links(response.text().unwrap(), false, should_contain_redirect);
// Same test with AJAX endpoint.
let (start, extra_name) = if url_start.starts_with("/crate/") {
("", "/crate")
} else {
("/crate", "")
};
let response = env
.frontend()
.get(&format!("/-/menus/platforms{url}{extra}"))
.get(&format!(
"{start}{url_start}/menus/platforms{extra_name}{url_end}{extra}"
))
.send()
.unwrap();
assert!(response.status().is_success());
Expand Down Expand Up @@ -1365,17 +1379,31 @@ mod tests {

// FIXME: For some reason, there are target-redirects on non-AJAX lists on docs.rs
// crate pages other than the "default" one.
run_check_links_redir(env, "/crate/dummy/0.4.0/features", "", true, false);
run_check_links_redir(env, "/crate/dummy/0.4.0/builds", "", true, false);
run_check_links_redir(env, "/crate/dummy/0.4.0/source/", "", true, false);
run_check_links_redir(env, "/crate/dummy/0.4.0/source/README.md", "", true, false);

run_check_links(env, "/crate/dummy/0.4.0", "", false);
run_check_links(env, "/dummy/latest/dummy", "/", true);
run_check_links(env, "/dummy/0.4.0/x86_64-pc-windows-msvc/dummy", "/", true);
run_check_links_redir(env, "/crate/dummy/0.4.0", "/features", "", true, false);
run_check_links_redir(env, "/crate/dummy/0.4.0", "/builds", "", true, false);
run_check_links_redir(env, "/crate/dummy/0.4.0", "/source/", "", true, false);
run_check_links_redir(
env,
"/crate/dummy/0.4.0",
"/source/README.md",
"",
true,
false,
);

run_check_links(env, "/crate/dummy/0.4.0", "", "/", false);
run_check_links(env, "/dummy/latest", "/dummy", "/", true);
run_check_links(
env,
"/dummy/0.4.0",
"/x86_64-pc-windows-msvc/dummy",
"/",
true,
);
run_check_links(
env,
"/dummy/0.4.0/x86_64-pc-windows-msvc/dummy/struct.A.html",
"/dummy/0.4.0",
"/x86_64-pc-windows-msvc/dummy/struct.A.html",
"/",
true,
);
Expand Down
26 changes: 11 additions & 15 deletions src/web/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,51 +237,47 @@ pub(super) fn build_axum_routes() -> AxumRouter {
get_internal(super::source::source_browser_handler),
)
.route(
"/-/menus/platforms/crate/:name/:version",
"/crate/:name/:version/menus/platforms/crate/",
get_internal(super::crate_details::get_all_platforms_root),
)
.route(
"/-/menus/platforms/crate/:name/:version/features",
"/crate/:name/:version/menus/platforms/crate/features",
get_internal(super::crate_details::get_all_platforms_root),
)
.route(
"/-/menus/platforms/crate/:name/:version/builds",
"/crate/:name/:version/menus/platforms/crate/builds",
get_internal(super::crate_details::get_all_platforms_root),
)
.route(
"/-/menus/platforms/crate/:name/:version/builds/*path",
"/crate/:name/:version/menus/platforms/crate/builds/*path",
get_internal(super::crate_details::get_all_platforms_root),
)
.route(
"/-/menus/platforms/crate/:name/:version/source/",
"/crate/:name/:version/menus/platforms/crate/source/",
get_internal(super::crate_details::get_all_platforms_root),
)
.route(
"/-/menus/platforms/crate/:name/:version/source/*path",
"/crate/:name/:version/menus/platforms/crate/source/*path",
get_internal(super::crate_details::get_all_platforms_root),
)
.route(
"/-/menus/platforms/crate/:name/:version/:target",
"/crate/:name/:version/menus/platforms/:target",
get_internal(super::crate_details::get_all_platforms),
)
.route(
"/-/menus/platforms/crate/:name/:version/:target/*path",
"/crate/:name/:version/menus/platforms/:target/*path",
get_internal(super::crate_details::get_all_platforms),
)
.route(
"/-/menus/platforms/:name/:version/",
"/crate/:name/:version/menus/platforms/",
get_internal(super::crate_details::get_all_platforms),
)
.route(
"/-/menus/platforms/:name/:version/:target/",
"/crate/:name/:version/menus/platforms/:target/",
get_internal(super::crate_details::get_all_platforms),
)
.route(
"/-/menus/platforms/:name/:version/:target/*path",
get_internal(super::crate_details::get_all_platforms),
)
.route(
"/-/menus/releases/:name",
"/crate/:name/:version/menus/releases",
get_internal(super::crate_details::get_all_releases),
)
.route(
Expand Down
2 changes: 1 addition & 1 deletion src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2259,7 +2259,7 @@ mod test {
// test rustdoc pages stay on the documentation
let page = kuchikiki::parse_html().one(
env.frontend()
.get("/-/menus/releases/hexponent")
.get("/crate/hexponent/0.3.1/menus/releases")
.send()?
.text()?,
);
Expand Down
18 changes: 14 additions & 4 deletions static/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ function loadAjaxMenu(menu, id, msg, path, extra) {
// We're not in a documentation page, so no need to do anything.
return;
}
const crateName = window.location.pathname.split('/')[1];
const parts = window.location.pathname.split("/");
let crateName = parts[1];
let version = parts[2];
if (crateName === "crate") {
crateName = parts[2];
version = parts[3];
path += "/crate";
}
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState !== XMLHttpRequest.DONE) {
Expand All @@ -33,7 +40,8 @@ function loadAjaxMenu(menu, id, msg, path, extra) {
document.getElementById(id).innerHTML = `Failed to load ${msg}`;
}
};
xhttp.open("GET", `/-/menus/${path}/${crateName}${extra}`, true);
console.log(extra, path);
xhttp.open("GET", `/crate/${crateName}/${version}/menus/${path}${extra}`, true);
xhttp.send();
};

Expand Down Expand Up @@ -88,13 +96,15 @@ function loadAjaxMenu(menu, id, msg, path, extra) {
if (newMenu.querySelector("#releases-list")) {
loadAjaxMenu(newMenu, "releases-list", "release list", "releases", "");
} else if (newMenu.querySelector("#platforms")) {
const parts = window.location.pathname.split("/");
const startFrom = parts[1] === "crate" ? 4 : 3;
loadAjaxMenu(
newMenu,
"platforms",
"platforms list",
"platforms",
// We get everything except the first crate name.
"/" + window.location.pathname.split("/").slice(2).join("/")
// We get everything except the first crate name and the version.
"/" + parts.slice(startFrom).join("/")
);
}
}
Expand Down

0 comments on commit b2c47fa

Please sign in to comment.