Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix changing versions not keeping the paths #2355

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

66 changes: 57 additions & 9 deletions src/web/crate_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,8 @@ pub(crate) async fn crate_details_handler(
struct ReleaseList {
releases: Vec<Release>,
crate_name: String,
inner_path: String,
target: String,
}

impl_axum_webpage! {
Expand All @@ -452,28 +454,74 @@ impl_axum_webpage! {

#[tracing::instrument]
pub(crate) async fn get_all_releases(
Path(params): Path<CrateDetailHandlerParams>,
Path(params): Path<RustdocHtmlParams>,
mut conn: DbConnection,
) -> AxumResult<AxumResponse> {
let crate_id = sqlx::query_scalar!(
let req_path: String = params.path.clone().unwrap_or_default();
let req_path: Vec<&str> = req_path.split('/').collect();

let release_found = match_version(&mut conn, &params.name, Some(&params.version)).await?;
trace!(?release_found, "found release");

let (version, _) = match release_found.version {
MatchSemver::Exact((version, _)) => (version.clone(), version),
MatchSemver::Latest((version, _)) => (version, "latest".to_string()),
MatchSemver::Semver(_) => return Err(AxumNope::VersionNotFound),
};

let row = sqlx::query!(
"SELECT
crates.id AS crate_id
crates.id AS crate_id,
releases.doc_targets
FROM crates
WHERE crates.name = $1;",
INNER JOIN releases on crates.id = releases.crate_id
WHERE crates.name = $1 and releases.version = $2;",
params.name,
&version,
)
.fetch_optional(&mut *conn)
.await?;
.await?
.ok_or(AxumNope::CrateNotFound)?;

let releases: Vec<Release> = if let Some(crate_id) = crate_id {
// get releases, sorted by semver
releases_for_crate(&mut conn, crate_id).await?
// get releases, sorted by semver
let releases: Vec<Release> = releases_for_crate(&mut conn, row.crate_id).await?;

let doc_targets = MetaData::parse_doc_targets(row.doc_targets);

let inner;
let (target, inner_path) = {
let mut inner_path = req_path.clone();

let target = if inner_path.len() > 1
&& doc_targets
.iter()
.any(|s| Some(s) == params.target.as_ref())
{
inner_path.remove(0);
params.target.as_ref().unwrap()
} else {
""
};

inner = inner_path.join("/");
(target, inner.trim_end_matches('/'))
};
let inner_path = if inner_path.is_empty() {
format!("{}/index.html", params.name)
} else {
Vec::new()
format!("{}/{inner_path}", params.name)
};

let target = if target.is_empty() {
String::new()
} else {
format!("{target}/")
};

let res = ReleaseList {
releases,
target: target.to_string(),
inner_path,
crate_name: params.name,
};
Ok(res.into_response())
Expand Down
12 changes: 12 additions & 0 deletions src/web/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,22 @@ pub(super) fn build_axum_routes() -> AxumRouter {
"/crate/:name/:version/menus/platforms/:target/",
get_internal(super::crate_details::get_all_platforms),
)
.route(
"/crate/:name/:version/menus/releases/:target",
get_internal(super::crate_details::get_all_releases),
)
.route(
"/crate/:name/:version/menus/releases/:target/*path",
get_internal(super::crate_details::get_all_releases),
)
.route(
"/crate/:name/:version/menus/releases",
get_internal(super::crate_details::get_all_releases),
)
.route(
"/crate/:name/:version/menus/releases/:target/",
get_internal(super::crate_details::get_all_releases),
)
.route(
"/-/rustdoc.static/*path",
get_internal(super::rustdoc::static_asset_handler),
Expand Down
37 changes: 30 additions & 7 deletions src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2229,6 +2229,15 @@ mod test {
})
}

fn parse_release_links_from_menu(body: &str) -> Vec<String> {
kuchikiki::parse_html()
.one(body)
.select(r#"ul > li > a"#)
.expect("invalid selector")
.map(|elem| elem.attributes.borrow().get("href").unwrap().to_string())
.collect()
}

#[test_case(true)]
#[test_case(false)]
fn test_version_link_goes_to_docs(archive_storage: bool) {
Expand All @@ -2244,6 +2253,7 @@ mod test {
.version("0.3.1")
.archive_storage(archive_storage)
.rustdoc_file("hexponent/index.html")
.rustdoc_file("hexponent/something.html")
.create()?;

// test rustdoc pages stay on the documentation
Expand All @@ -2253,14 +2263,27 @@ mod test {
.send()?;
assert!(releases_response.status().is_success());
assert_cache_control(&releases_response, CachePolicy::ForeverInCdn, &env.config());
let page = kuchikiki::parse_html().one(releases_response.text()?);
let selector =
r#"ul > li a[href="/crate/hexponent/0.3.1/target-redirect/hexponent/index.html"]"#
.to_string();
assert_eq!(
page.select(&selector).unwrap().count(),
1,
"link to /target-redirect/ not found"
parse_release_links_from_menu(&releases_response.text()?),
vec![
"/crate/hexponent/0.3.1/target-redirect/hexponent/index.html".to_owned(),
"/crate/hexponent/0.3.0/target-redirect/hexponent/index.html".to_owned(),
]
);

// test if target-redirect inludes path
let releases_response = env
.frontend()
.get("/crate/hexponent/0.3.1/menus/releases/hexponent/something.html")
.send()?;
assert!(releases_response.status().is_success());
assert_cache_control(&releases_response, CachePolicy::ForeverInCdn, &env.config());
assert_eq!(
parse_release_links_from_menu(&releases_response.text()?),
vec![
"/crate/hexponent/0.3.1/target-redirect/hexponent/something.html".to_owned(),
"/crate/hexponent/0.3.0/target-redirect/hexponent/something.html".to_owned(),
]
);

// test /crate pages stay on /crate
Expand Down
20 changes: 15 additions & 5 deletions static/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,28 @@ function loadAjaxMenu(menu, id, msg, path, extra) {
currentMenu = newMenu;
newMenu.className += " pure-menu-active";
backdrop.style.display = "block";

const parts = window.location.pathname.split("/");
const startFrom = parts[1] === "crate" ? 4 : 3;
// We get everything except the first crate name and the version.
const innerPath = "/" + parts.slice(startFrom).join("/")

if (newMenu.querySelector("#releases-list")) {
loadAjaxMenu(newMenu, "releases-list", "release list", "releases", "");
loadAjaxMenu(
newMenu,
"releases-list",
"release list",
"releases",
innerPath,
);

} 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 and the version.
"/" + parts.slice(startFrom).join("/")
innerPath,
);
}
}
Expand Down
2 changes: 0 additions & 2 deletions templates/rustdoc/releases.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{% import "macros.html" as macros %}
{% set target = "" %}
{% set inner_path = releases[0].target_name ~ "/index.html" %}
<ul class="pure-menu-list">
{{ macros::releases_list(name=crate_name, releases=releases, target=target, inner_path=inner_path) }}
</ul>
Loading